|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +from database import get_connection |
| 3 | + |
| 4 | +def bar_chart_expense(): |
| 5 | + conn = get_connection() |
| 6 | + cursor = conn.cursor() |
| 7 | + |
| 8 | + cursor.execute("SELECT category, SUM(amount) FROM transactions WHERE type = 'Expense' GROUP BY category") |
| 9 | + rows = cursor.fetchall() |
| 10 | + conn.close() |
| 11 | + |
| 12 | + if rows: |
| 13 | + categories = [row[0] for row in rows] |
| 14 | + amounts = [row[1] for row in rows] |
| 15 | + |
| 16 | + plt.bar(categories, amounts) |
| 17 | + plt.xlabel('Category') |
| 18 | + plt.ylabel('Amount') |
| 19 | + plt.title('Spending by Category') |
| 20 | + plt.xticks(rotation=45) |
| 21 | + plt.tight_layout() |
| 22 | + plt.show() |
| 23 | + else: |
| 24 | + print("No expenses recorded yet.") |
| 25 | + |
| 26 | +def pie_chart_expense(): |
| 27 | + conn = get_connection() |
| 28 | + cursor = conn.cursor() |
| 29 | + |
| 30 | + cursor.execute("SELECT category, SUM(amount) FROM transactions WHERE type = 'Expense' GROUP BY category") |
| 31 | + rows = cursor.fetchall() |
| 32 | + conn.close() |
| 33 | + |
| 34 | + if rows: |
| 35 | + categories = [row[0] for row in rows] |
| 36 | + amounts = [row[1] for row in rows] |
| 37 | + |
| 38 | + plt.pie(amounts, labels=categories, autopct='%1.1f%%', startangle=90) |
| 39 | + plt.title('Spending by Category') |
| 40 | + plt.tight_layout() |
| 41 | + plt.show() |
| 42 | + else: |
| 43 | + print("No expenses recorded yet.") |
| 44 | + |
| 45 | +def line_chart_expense_over_time(): |
| 46 | + conn = get_connection() |
| 47 | + cursor = conn.cursor() |
| 48 | + |
| 49 | + cursor.execute("SELECT date, SUM(amount) FROM transactions WHERE type = 'Expense' GROUP BY date") |
| 50 | + rows = cursor.fetchall() |
| 51 | + conn.close() |
| 52 | + |
| 53 | + if rows: |
| 54 | + dates = [row[0] for row in rows] |
| 55 | + amounts = [row[1] for row in rows] |
| 56 | + |
| 57 | + plt.plot(dates, amounts, marker='o') |
| 58 | + plt.xlabel('Date') |
| 59 | + plt.ylabel('Amount') |
| 60 | + plt.title('Expenses Over Time') |
| 61 | + plt.xticks(rotation=45) |
| 62 | + plt.tight_layout() |
| 63 | + plt.show() |
| 64 | + else: |
| 65 | + print("No expenses recorded yet.") |
| 66 | + |
| 67 | +def stacked_bar_chart_income_expense(): |
| 68 | + conn = get_connection() |
| 69 | + cursor = conn.cursor() |
| 70 | + |
| 71 | + cursor.execute("SELECT date, SUM(amount) FROM transactions WHERE type = 'Income' GROUP BY date") |
| 72 | + income_rows = cursor.fetchall() |
| 73 | + |
| 74 | + cursor.execute("SELECT date, SUM(amount) FROM transactions WHERE type = 'Expense' GROUP BY date") |
| 75 | + expense_rows = cursor.fetchall() |
| 76 | + |
| 77 | + conn.close() |
| 78 | + |
| 79 | + if income_rows and expense_rows: |
| 80 | + income_dates = [row[0] for row in income_rows] |
| 81 | + income_amounts = [row[1] for row in income_rows] |
| 82 | + expense_dates = [row[0] for row in expense_rows] |
| 83 | + expense_amounts = [row[1] for row in expense_rows] |
| 84 | + |
| 85 | + plt.bar(income_dates, income_amounts, label='Income') |
| 86 | + plt.bar(expense_dates, expense_amounts, bottom=income_amounts, label='Expense') |
| 87 | + |
| 88 | + plt.xlabel('Date') |
| 89 | + plt.ylabel('Amount') |
| 90 | + plt.title('Income vs Expenses Over Time') |
| 91 | + plt.xticks(rotation=45) |
| 92 | + plt.legend() |
| 93 | + plt.tight_layout() |
| 94 | + plt.show() |
| 95 | + else: |
| 96 | + print("Not enough data to generate this chart.") |
| 97 | + |
| 98 | +def histogram_expense_distribution(): |
| 99 | + conn = get_connection() |
| 100 | + cursor = conn.cursor() |
| 101 | + |
| 102 | + cursor.execute("SELECT amount FROM transactions WHERE type = 'Expense'") |
| 103 | + rows = cursor.fetchall() |
| 104 | + conn.close() |
| 105 | + |
| 106 | + if rows: |
| 107 | + amounts = [row[0] for row in rows] |
| 108 | + |
| 109 | + plt.hist(amounts, bins=10) |
| 110 | + plt.xlabel('Expense Amount') |
| 111 | + plt.ylabel('Frequency') |
| 112 | + plt.title('Expense Distribution') |
| 113 | + plt.tight_layout() |
| 114 | + plt.show() |
| 115 | + else: |
| 116 | + print("No expenses recorded yet.") |
| 117 | + |
| 118 | +def visualize_data(): |
| 119 | + print("\n--- Visualization Menu ---") |
| 120 | + print("1. Bar Chart (Spending by Category)") |
| 121 | + print("2. Pie Chart (Spending by Category)") |
| 122 | + print("3. Line Chart (Expenses Over Time)") |
| 123 | + print("4. Stacked Bar Chart (Income vs Expenses)") |
| 124 | + print("5. Histogram (Expense Distribution)") |
| 125 | + |
| 126 | + choice = input("Select a visualization option (1-5): ") |
| 127 | + |
| 128 | + if choice == "1": |
| 129 | + bar_chart_expense() |
| 130 | + elif choice == "2": |
| 131 | + pie_chart_expense() |
| 132 | + elif choice == "3": |
| 133 | + line_chart_expense_over_time() |
| 134 | + elif choice == "4": |
| 135 | + stacked_bar_chart_income_expense() |
| 136 | + elif choice == "5": |
| 137 | + histogram_expense_distribution() |
| 138 | + else: |
| 139 | + print("Invalid choice. Please select a valid option.") |
0 commit comments