|
4 | 4 | from datetime import date |
5 | 5 | import os |
6 | 6 | import seaborn as sns |
| 7 | +import matplotlib.dates as mdates |
7 | 8 |
|
8 | | - |
9 | | -# Connect to MySQL database |
| 9 | +# Function to fetch data from the database |
10 | 10 | def fetch_data_from_db(username): |
11 | | - connection = mysql.connector.connect( |
12 | | - host="localhost", |
13 | | - user="root", |
14 | | - password="ananyavastare2345", |
15 | | - database="finance_data", |
16 | | - ) |
17 | | - cursor = connection.cursor(dictionary=True) |
18 | | - |
19 | 11 | try: |
| 12 | + # Connect to MySQL database |
| 13 | + connection = mysql.connector.connect( |
| 14 | + host="localhost", |
| 15 | + user="root", |
| 16 | + password="ananyavastare2345", |
| 17 | + database="finance_data", |
| 18 | + ) |
| 19 | + cursor = connection.cursor(dictionary=True) |
| 20 | + |
20 | 21 | # Fetch user data |
21 | 22 | cursor.execute("SELECT user_id FROM user WHERE username = %s", (username,)) |
22 | 23 | user = cursor.fetchone() |
@@ -54,13 +55,18 @@ def fetch_data_from_db(username): |
54 | 55 |
|
55 | 56 | except mysql.connector.Error as err: |
56 | 57 | print(f"Error: {err}") |
| 58 | + return None |
| 59 | + |
57 | 60 | finally: |
58 | | - cursor.close() # Close the cursor |
59 | | - connection.close() # Close the connection |
| 61 | + if cursor: |
| 62 | + cursor.close() # Close the cursor |
| 63 | + if connection: |
| 64 | + connection.close() # Close the connection |
60 | 65 |
|
61 | 66 |
|
62 | 67 | # Function to create a combined pie and line chart for the income data |
63 | 68 | def create_combined_chart(income_data, plot_path="combined_plot.png"): |
| 69 | + # Prepare labels and amounts for the pie chart |
64 | 70 | labels = list(income_data.keys()) |
65 | 71 | amounts = [sum(data["amounts"]) for data in income_data.values()] |
66 | 72 | colors = sns.color_palette("pastel", len(labels)) |
@@ -94,6 +100,9 @@ def create_combined_chart(income_data, plot_path="combined_plot.png"): |
94 | 100 | axs[1].set_ylabel("Amount ($)") |
95 | 101 | axs[1].grid(True) |
96 | 102 | axs[1].legend(loc="upper left") |
| 103 | + axs[1].xaxis.set_major_locator(mdates.MonthLocator()) |
| 104 | + axs[1].xaxis.set_major_formatter(mdates.DateFormatter('%b %d, %Y')) |
| 105 | + plt.xticks(rotation=45) |
97 | 106 |
|
98 | 107 | # Adjust layout and save the combined plot |
99 | 108 | plt.tight_layout() |
@@ -133,6 +142,8 @@ def main(): |
133 | 142 |
|
134 | 143 | # Generate and save the PDF report |
135 | 144 | save_report(username) |
| 145 | + else: |
| 146 | + print(f"No income data found for {username}.") |
136 | 147 |
|
137 | 148 |
|
138 | 149 | # Run the main function when the script is executed |
|
0 commit comments