Skip to content

Commit 6562073

Browse files
Merge pull request #372 from Charu19awasthi/patch-1
Update report.py
2 parents a587b9a + 325c8e6 commit 6562073

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

software/report.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
from datetime import date
55
import os
66
import seaborn as sns
7+
import matplotlib.dates as mdates
78

8-
9-
# Connect to MySQL database
9+
# Function to fetch data from the database
1010
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-
1911
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+
2021
# Fetch user data
2122
cursor.execute("SELECT user_id FROM user WHERE username = %s", (username,))
2223
user = cursor.fetchone()
@@ -54,13 +55,18 @@ def fetch_data_from_db(username):
5455

5556
except mysql.connector.Error as err:
5657
print(f"Error: {err}")
58+
return None
59+
5760
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
6065

6166

6267
# Function to create a combined pie and line chart for the income data
6368
def create_combined_chart(income_data, plot_path="combined_plot.png"):
69+
# Prepare labels and amounts for the pie chart
6470
labels = list(income_data.keys())
6571
amounts = [sum(data["amounts"]) for data in income_data.values()]
6672
colors = sns.color_palette("pastel", len(labels))
@@ -94,6 +100,9 @@ def create_combined_chart(income_data, plot_path="combined_plot.png"):
94100
axs[1].set_ylabel("Amount ($)")
95101
axs[1].grid(True)
96102
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)
97106

98107
# Adjust layout and save the combined plot
99108
plt.tight_layout()
@@ -133,6 +142,8 @@ def main():
133142

134143
# Generate and save the PDF report
135144
save_report(username)
145+
else:
146+
print(f"No income data found for {username}.")
136147

137148

138149
# Run the main function when the script is executed

0 commit comments

Comments
 (0)