-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVizFile.py
More file actions
143 lines (117 loc) · 6.14 KB
/
VizFile.py
File metadata and controls
143 lines (117 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinter import ttk
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import json
class FileToGraphApp:
def __init__(self, root):
self.root = root
self.root.title("VizFile")
self.root.geometry("800x600")
self.label = tk.Label(root, text="VizFile", padx=10, pady=10, font=("Verdana", 40))
self.label.pack()
self.label = tk.Label(root, text="Select a file type and load the file to generate a graph", padx=10, pady=10)
self.label.pack()
self.file_type = tk.StringVar(value="CSV")
self.radio_csv = tk.Radiobutton(root, text="CSV", variable=self.file_type, value="CSV")
self.radio_json = tk.Radiobutton(root, text="JSON", variable=self.file_type, value="JSON")
self.radio_excel = tk.Radiobutton(root, text="Excel", variable=self.file_type, value="Excel")
self.radio_csv.pack()
self.radio_json.pack()
self.radio_excel.pack()
self.load_button = tk.Button(root, text="Load File", command=self.load_file)
self.load_button.pack(pady=10)
self.graph_type_label = tk.Label(root, text="Select Graph Type", padx=10, pady=10)
self.graph_type_label.pack()
self.graph_type = tk.StringVar(value="line")
self.graph_line = tk.Radiobutton(root, text="Line", variable=self.graph_type, value="line")
self.graph_bar = tk.Radiobutton(root, text="Bar", variable=self.graph_type, value="bar")
self.graph_scatter = tk.Radiobutton(root, text="Scatter", variable=self.graph_type, value="scatter")
self.graph_line.pack()
self.graph_bar.pack()
self.graph_scatter.pack()
self.column_frame = tk.Frame(root)
self.column_frame.pack(pady=10)
self.column_label = tk.Label(self.column_frame, text="Select Columns for X and Y axes")
self.column_label.pack()
self.x_column_label = tk.Label(self.column_frame, text="X-axis Column")
self.x_column_label.pack(side=tk.LEFT, padx=5)
self.x_column_combo = ttk.Combobox(self.column_frame, state="readonly")
self.x_column_combo.pack(side=tk.LEFT, padx=5)
self.y_column_label = tk.Label(self.column_frame, text="Y-axis Column")
self.y_column_label.pack(side=tk.LEFT, padx=5)
self.y_column_combo = ttk.Combobox(self.column_frame, state="readonly")
self.y_column_combo.pack(side=tk.LEFT, padx=5)
self.generate_button = tk.Button(root, text="Generate Graph", command=self.generate_graph)
self.generate_button.pack(pady=10)
self.download_button = tk.Button(root, text="Download Graph", command=self.download_graph)
self.download_button.pack(pady=10)
self.canvas = None
def load_file(self):
file_type = self.file_type.get()
try:
if file_type == "CSV":
file_path = filedialog.askopenfilename(filetypes=[("CSV files", "*.csv")])
if file_path:
self.data_frame = pd.read_csv(file_path)
elif file_type == "JSON":
file_path = filedialog.askopenfilename(filetypes=[("JSON files", "*.json")])
if file_path:
with open(file_path, 'r') as file:
data = json.load(file)
self.data_frame = pd.json_normalize(data)
elif file_type == "Excel":
file_path = filedialog.askopenfilename(filetypes=[("Excel files", "*.xls *.xlsx")])
if file_path:
self.data_frame = pd.read_excel(file_path)
if file_path:
self.update_column_options()
else:
messagebox.showwarning("Warning", "No file selected")
except Exception as e:
messagebox.showerror("Error", f"Failed to load file: {str(e)}")
def update_column_options(self):
if self.data_frame is not None:
columns = self.data_frame.columns.tolist()
self.x_column_combo['values'] = columns
self.y_column_combo['values'] = columns
def generate_graph(self):
if self.data_frame is not None:
x_column = self.x_column_combo.get()
y_column = self.y_column_combo.get()
graph_type = self.graph_type.get()
if not x_column or not y_column:
messagebox.showwarning("Warning", "Please select both X and Y columns")
return
try:
fig, ax = plt.subplots(figsize=(6, 4)) # Reduced size of the graph
if graph_type == "line":
self.data_frame.plot(kind='line', x=x_column, y=y_column, ax=ax)
elif graph_type == "bar":
self.data_frame.plot(kind='bar', x=x_column, y=y_column, ax=ax)
elif graph_type == "scatter":
self.data_frame.plot(kind='scatter', x=x_column, y=y_column, ax=ax)
if self.canvas:
self.canvas.get_tk_widget().pack_forget()
self.canvas = FigureCanvasTkAgg(fig, master=self.root)
self.canvas.draw()
self.canvas.get_tk_widget().pack()
except Exception as e:
messagebox.showerror("Error", f"Failed to generate graph: {str(e)}")
def download_graph(self):
if self.canvas:
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png"), ("All files", "*.*")])
if file_path:
try:
self.canvas.figure.savefig(file_path)
messagebox.showinfo("Success", f"Graph saved as {file_path}")
except Exception as e:
messagebox.showerror("Error", f"Failed to save graph: {str(e)}")
else:
messagebox.showwarning("Warning", "No graph to save")
if __name__ == "__main__":
root = tk.Tk()
app = FileToGraphApp(root)
root.mainloop()