Anonymous
Невозможно создать диаграмму ERD с помощью кода Python.
Сообщение
Anonymous » 27 сен 2024, 18:12
Следующий Python предназначен для создания ERD с использованием кода Visual Studio.
Диаграмма создается локально с помощью matplotlib. Код выполняется без ошибок, однако диаграмма ERD пуста.
Код Python выглядит следующим образом:
Код: Выделить всё
import matplotlib.pyplot as plt
# Define the entities and their attributes for the ERD
entities = {
"Customer": ["CustomerID (PK)", "CustomerName", "ContactInfo"],
"CreditCardAccount": ["AccountID (PK)", "AccountStatus", "Balance", "CustomerID (FK)"],
"CreditCard": ["CardID (PK)", "CardNumber", "ExpiryDate", "AccountID (FK)", "BrandID (FK)"],
"CreditCardBrand": ["BrandID (PK)", "BrandName", "CardType"],
"SecondaryCardHolder": ["SecondaryHolderID (PK)", "HolderName", "RelationToPrimary", "AccountID (FK)"],
"PurchaseTransaction": ["TransactionID (PK)", "TransactionDate", "Amount", "CardID (FK)", "RetailerID (FK)"],
"Retailer": ["RetailerID (PK)", "RetailerName", "Location"],
"MonthlyStatement": ["StatementID (PK)", "StatementDate", "OutstandingBalance", "AccountID (FK)"],
"CustomerServiceInteraction": ["InteractionID (PK)", "InteractionDate", "Notes", "CustomerID (FK)"],
}
# Relationships between entities
relationships = [
("Customer", "CreditCardAccount", "1:M"),
("CreditCardAccount", "CreditCard", "1:M"),
("CreditCard", "CreditCardBrand", "M:1"),
("CreditCardAccount", "SecondaryCardHolder", "1:M"),
("CreditCard", "PurchaseTransaction", "1:M"),
("PurchaseTransaction", "Retailer", "M:1"),
("CreditCardAccount", "MonthlyStatement", "1:M"),
("Customer", "CustomerServiceInteraction", "1:M"),
]
# Plotting the ERD
fig, ax = plt.subplots(figsize=(12, 8))
# Define positions for the entities
positions = {
"Customer": (1, 5),
"CreditCardAccount": (4, 5),
"CreditCard": (7, 5),
"CreditCardBrand": (10, 5),
"SecondaryCardHolder": (4, 3),
"PurchaseTransaction": (7, 3),
"Retailer": (10, 3),
"MonthlyStatement": (4, 1),
"CustomerServiceInteraction": (1, 3),
}
# Draw entities as boxes
for entity, position in positions.items():
plt.text(position[0], position[1], f"{entity}\n" + "\n".join(entities[entity]),
ha='center', va='center', bbox=dict(facecolor='lightblue', edgecolor='black', boxstyle='round,pad=0.5'))
# Draw relationships as lines
for rel in relationships:
start_pos = positions[rel[0]]
end_pos = positions[rel[1]]
ax.annotate("",
xy=end_pos, xycoords='data',
xytext=start_pos, textcoords='data',
arrowprops=dict(arrowstyle="->", lw=1.5, color='black'),
)
# Add cardinality
midpoint = ((start_pos[0] + end_pos[0]) / 2, (start_pos[1] + end_pos[1]) / 2)
ax.text(midpoint[0], midpoint[1], rel[2], ha='center', va='center', fontsize=10)
# Hide axes
ax.set_axis_off()
# Show the ERD diagram
plt.title("Entity Relationship Diagram (ERD) for Credit Card Company", fontsize=16)
plt.show()
Вывод следующий:
Может кто-нибудь подсказать, почему не появляется ERD?
Подробнее здесь:
https://stackoverflow.com/questions/790 ... ython-code
1727449966
Anonymous
Следующий Python предназначен для создания ERD с использованием кода Visual Studio. Диаграмма создается локально с помощью matplotlib. Код выполняется без ошибок, однако диаграмма ERD пуста. Код Python выглядит следующим образом: [code]import matplotlib.pyplot as plt # Define the entities and their attributes for the ERD entities = { "Customer": ["CustomerID (PK)", "CustomerName", "ContactInfo"], "CreditCardAccount": ["AccountID (PK)", "AccountStatus", "Balance", "CustomerID (FK)"], "CreditCard": ["CardID (PK)", "CardNumber", "ExpiryDate", "AccountID (FK)", "BrandID (FK)"], "CreditCardBrand": ["BrandID (PK)", "BrandName", "CardType"], "SecondaryCardHolder": ["SecondaryHolderID (PK)", "HolderName", "RelationToPrimary", "AccountID (FK)"], "PurchaseTransaction": ["TransactionID (PK)", "TransactionDate", "Amount", "CardID (FK)", "RetailerID (FK)"], "Retailer": ["RetailerID (PK)", "RetailerName", "Location"], "MonthlyStatement": ["StatementID (PK)", "StatementDate", "OutstandingBalance", "AccountID (FK)"], "CustomerServiceInteraction": ["InteractionID (PK)", "InteractionDate", "Notes", "CustomerID (FK)"], } # Relationships between entities relationships = [ ("Customer", "CreditCardAccount", "1:M"), ("CreditCardAccount", "CreditCard", "1:M"), ("CreditCard", "CreditCardBrand", "M:1"), ("CreditCardAccount", "SecondaryCardHolder", "1:M"), ("CreditCard", "PurchaseTransaction", "1:M"), ("PurchaseTransaction", "Retailer", "M:1"), ("CreditCardAccount", "MonthlyStatement", "1:M"), ("Customer", "CustomerServiceInteraction", "1:M"), ] # Plotting the ERD fig, ax = plt.subplots(figsize=(12, 8)) # Define positions for the entities positions = { "Customer": (1, 5), "CreditCardAccount": (4, 5), "CreditCard": (7, 5), "CreditCardBrand": (10, 5), "SecondaryCardHolder": (4, 3), "PurchaseTransaction": (7, 3), "Retailer": (10, 3), "MonthlyStatement": (4, 1), "CustomerServiceInteraction": (1, 3), } # Draw entities as boxes for entity, position in positions.items(): plt.text(position[0], position[1], f"{entity}\n" + "\n".join(entities[entity]), ha='center', va='center', bbox=dict(facecolor='lightblue', edgecolor='black', boxstyle='round,pad=0.5')) # Draw relationships as lines for rel in relationships: start_pos = positions[rel[0]] end_pos = positions[rel[1]] ax.annotate("", xy=end_pos, xycoords='data', xytext=start_pos, textcoords='data', arrowprops=dict(arrowstyle="->", lw=1.5, color='black'), ) # Add cardinality midpoint = ((start_pos[0] + end_pos[0]) / 2, (start_pos[1] + end_pos[1]) / 2) ax.text(midpoint[0], midpoint[1], rel[2], ha='center', va='center', fontsize=10) # Hide axes ax.set_axis_off() # Show the ERD diagram plt.title("Entity Relationship Diagram (ERD) for Credit Card Company", fontsize=16) plt.show() [/code] Вывод следующий: [img]https://i.sstatic.net/8lYMYZTK.png[/img] Может кто-нибудь подсказать, почему не появляется ERD? Подробнее здесь: [url]https://stackoverflow.com/questions/79031970/unable-to-generate-erd-diagram-with-python-code[/url]