152 lines
5.3 KiB
Plaintext
152 lines
5.3 KiB
Plaintext
import csv
|
|
import matplotlib.pyplot as plt
|
|
from datetime import datetime
|
|
import tkinter as tk
|
|
from tkinter import filedialog, ttk, messagebox
|
|
|
|
data = []
|
|
|
|
def create_tab(notebook, title):
|
|
tab = ttk.Frame(notebook)
|
|
notebook.add(tab, text=title)
|
|
return tab
|
|
|
|
def load_csv():
|
|
global data
|
|
filepath = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
|
|
if filepath:
|
|
data = []
|
|
try:
|
|
with open(filepath, newline='', encoding='utf-8') as csvfile:
|
|
reader = csv.DictReader(csvfile)
|
|
for row in reader:
|
|
row['Datums'] = datetime.strptime(row['Datums'], '%Y-%m-%d')
|
|
row['Ieņēmumi'] = float(row['Ieņēmumi'])
|
|
row['Izdevumi'] = float(row['Izdevumi'])
|
|
data.append(row)
|
|
display_data()
|
|
except Exception as e:
|
|
messagebox.showerror("Kļūda", f"Nevar nolasīt failu: {e}")
|
|
else:
|
|
messagebox.showerror("Kļūda", "Netika izvēlēts fails.")
|
|
|
|
def display_data():
|
|
for row in table.get_children():
|
|
table.delete(row)
|
|
categories = set()
|
|
|
|
for row in data:
|
|
table.insert("", "end", values=(row["Datums"].strftime('%Y-%m-%d'), row["Kategorija"], row["Ieņēmumi"], row["Izdevumi"], row["Komentārs"]))
|
|
categories.add(row["Kategorija"])
|
|
|
|
category_combo["values"] = list(categories)
|
|
if categories:
|
|
category_combo.current(0)
|
|
|
|
def analyze_data():
|
|
selected_category = category_combo.get()
|
|
category_data = [row for row in data if row["Kategorija"] == selected_category]
|
|
|
|
if not category_data:
|
|
result_label.config(text="Nav datu šai kategorijai.")
|
|
return
|
|
|
|
avg_income = sum(row["Ieņēmumi"] for row in category_data) / len(category_data)
|
|
avg_expense = sum(row["Izdevumi"] for row in category_data) / len(category_data)
|
|
max_income = max(row["Ieņēmumi"] for row in category_data)
|
|
min_income = min(row["Ieņēmumi"] for row in category_data)
|
|
max_expense = max(row["Izdevumi"] for row in category_data)
|
|
min_expense = min(row["Izdevumi"] for row in category_data)
|
|
total_income = sum(row["Ieņēmumi"] for row in category_data)
|
|
total_expense = sum(row["Izdevumi"] for row in category_data)
|
|
|
|
analysis_result = (
|
|
f"Vidējie ieņēmumi: {avg_income:.2f}\n"
|
|
f"Vidējie izdevumi: {avg_expense:.2f}\n"
|
|
f"Maksimālie ieņēmumi: {max_income}\n"
|
|
f"Minimālie ieņēmumi: {min_income}\n"
|
|
f"Maksimālie izdevumi: {max_expense}\n"
|
|
f"Minimālie izdevumi: {min_expense}\n"
|
|
f"Kopējie ieņēmumi: {total_income}\n"
|
|
f"Kopējie izdevumi: {total_expense}"
|
|
)
|
|
|
|
result_label.config(text=analysis_result)
|
|
|
|
def plot_data():
|
|
selected_category = category_combo.get()
|
|
category_data = [row for row in data if row["Kategorija"] == selected_category]
|
|
|
|
if not category_data:
|
|
messagebox.showinfo("Grafiks", "Nav datu šai kategorijai.")
|
|
return
|
|
|
|
yearly_data = {}
|
|
for row in category_data:
|
|
year = row["Datums"].year
|
|
if year not in yearly_data:
|
|
yearly_data[year] = {"Ieņēmumi": 0, "Izdevumi": 0}
|
|
yearly_data[year]["Ieņēmumi"] += row["Ieņēmumi"]
|
|
yearly_data[year]["Izdevumi"] += row["Izdevumi"]
|
|
|
|
years = sorted(yearly_data.keys())
|
|
income = [yearly_data[year]["Ieņēmumi"] for year in years]
|
|
expense = [yearly_data[year]["Izdevumi"] for year in years]
|
|
|
|
plt.figure(figsize=(10, 6))
|
|
plt.plot(years, income, label="Ieņēmumi", color="green", marker="o")
|
|
plt.plot(years, expense, label="Izdevumi", color="red", marker="o")
|
|
plt.title(f"{selected_category} - Ieņēmumi un izdevumi pa gadiem")
|
|
plt.xlabel("Gads")
|
|
plt.ylabel("Summa")
|
|
plt.legend()
|
|
plt.grid(True)
|
|
plt.show()
|
|
|
|
root = tk.Tk()
|
|
root.title("Finanšu datu analīze pēc kategorijām")
|
|
|
|
notebook = ttk.Notebook(root)
|
|
notebook.pack(fill='both', expand=True)
|
|
|
|
tab1 = create_tab(notebook, "Datu ielāde")
|
|
table = ttk.Treeview(tab1, columns=("Datums", "Kategorija", "Ieņēmumi", "Izdevumi", "Komentārs"), show="headings")
|
|
table.heading("Datums", text="Datums")
|
|
table.heading("Kategorija", text="Kategorija")
|
|
table.heading("Ieņēmumi", text="Ieņēmumi")
|
|
table.heading("Izdevumi", text="Izdevumi")
|
|
table.heading("Komentārs", text="Komentārs")
|
|
table.pack(fill='both', expand=True)
|
|
|
|
load_button = tk.Button(tab1, text="Ielādēt failu", command=load_csv)
|
|
load_button.pack()
|
|
|
|
category_label = tk.Label(tab1, text="Izvēlieties kategoriju:")
|
|
category_label.pack()
|
|
|
|
category_combo = ttk.Combobox(tab1)
|
|
category_combo.pack()
|
|
|
|
tab2 = create_tab(notebook, "Datu analīze")
|
|
|
|
selected_category_label = tk.Label(tab2, text="", justify="left")
|
|
selected_category_label.pack(pady=10)
|
|
|
|
analyze_button = tk.Button(tab2, text="Analizēt", command=lambda: analyze_data())
|
|
analyze_button.pack(pady=20)
|
|
|
|
result_label = tk.Label(tab2, text="", justify="left")
|
|
result_label.pack(pady=10)
|
|
|
|
def update_selected_category():
|
|
selected_category = category_combo.get()
|
|
selected_category_label.config(text=f"Izvēlētā kategorija: {selected_category}")
|
|
|
|
category_combo.bind("<<ComboboxSelected>>", lambda _: update_selected_category())
|
|
|
|
tab3 = create_tab(notebook, "Grafiks")
|
|
plot_button = tk.Button(tab3, text="Zīmēt grafiku", command=plot_data)
|
|
plot_button.pack(pady=20)
|
|
|
|
root.mainloop()
|