-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
1312 lines (1077 loc) · 48.2 KB
/
main.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# from tkinter import *
from tkinter import StringVar
from tkinter import Label
from tkinter import messagebox
from tkinter import simpledialog
import pandas as pd
from tkinter.filedialog import askopenfile
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
from tkinter import colorchooser
from tkinter import ttk
from ttkthemes import themed_tk as tk
from pandas.api.types import is_numeric_dtype
from pandas.api.types import is_string_dtype
# SINGLE LINED GRAPH
# single lined graph section is purposely seperated in order to prevent hassle of selecting graph title everytime to edit.
# attributes of SLG
x_points = []
y_points = []
line_style = 'solid'
marker_style = '.'
marker_size = 10
graph_color = 'Black'
graph_title = 'Line Graph'
s_graph_canvas_title = ''
xs_label = "X Axis"
y_label = "Y Axis"
# update
def update_graph():
fig.clear()
g = fig.add_subplot(title=s_graph_canvas_title, label=graph_title, xlabel=xs_label, ylabel=y_label)
g.plot(x_points, y_points, linestyle=line_style, markersize=marker_size, markeredgecolor='black',
marker=marker_style, color=graph_color, label=graph_title)
g.legend()
canvas.draw()
# change title
def change_axes_title():
global xs_label, y_label
xs_label = simpledialog.askstring("X axis", "Enter X axis title")
y_label = simpledialog.askstring("Y axis", "Enter Y axis title")
update_graph()
# add single point
def add_point():
try:
global x_points
global y_points
a, b = float(x.get()), float(y.get())
x.delete(0, 'end')
y.delete(0, 'end')
x_points.append(a)
y_points.append(b)
xy = list(zip(x_points, y_points))
xy.sort(key=lambda x: x[0])
x_points = [x[0] for x in xy]
y_points = [x[1] for x in xy]
update_graph()
except:
messagebox.showwarning("ERROR", "Enter a Number")
# set title of graph
def set_title():
global graph_title
graph_title = simpledialog.askstring("Title", "Enter Title")
update_graph()
# set attributes
def default_line():
global line_style
line_style = 'solid'
update_graph()
def dashed_line():
global line_style
line_style = 'dashed'
update_graph()
def dotted_line():
global line_style
line_style = 'dotted'
update_graph()
def default_marker():
global marker_style
marker_style = '.'
update_graph()
def plus_marker():
global marker_style
marker_style = '+'
update_graph()
def x_marker():
global marker_style
marker_style = 'x'
update_graph()
# clear canvas
def clear_graph():
global x_points, y_points, line_style, marker_style, marker_size, graph_color, graph_title, xs_label, y_label, s_graph_canvas_title
ans = messagebox.askyesno("Are You Sure ?", "Do You Want To Clear The Canvas ?")
if (ans != True):
return
fig.clear()
line_style = 'solid'
marker_style = '.'
marker_size = 10
graph_color = 'Black'
graph_title = 'Line Graph'
xs_label = "X Axis"
y_label = "Y Axis"
s_graph_canvas_title = ""
curr_graph_color.config(bg=graph_color)
x_points, y_points = [], []
canvas.draw()
# pick graph color
def pick_color():
global graph_color
try:
graph_color = colorchooser.askcolor()[1]
curr_graph_color.config(bg=graph_color)
except:
update_graph()
update_graph()
def csv_single():
try:
file = askopenfile(mode='r', filetypes=[('CSV FILES', '*.csv')])
if file is not None:
rd = pd.read_csv(file)
wm = tk.ThemedTk()
wm.set_theme('radiance')
wm.title('Import Graph From CSV')
lst = list(rd.columns)
ttk.Label(master=wm, text="Select attribute for X Axis ").pack(padx=10, pady=15)
x = StringVar()
y = StringVar()
op1 = ttk.OptionMenu(wm, x, "DEFAULT VALUE", *lst)
op1.config(width=20)
op1.pack(padx=15, pady=15)
ttk.Label(master=wm, text="Select attribute for Y Axis ").pack(padx=10, pady=15)
op2 = ttk.OptionMenu(wm, y, "DEFAULT VALUE", *lst)
op2.config(width=20)
op2.pack(padx=15, pady=15)
def plot_csv():
try:
if not (is_numeric_dtype(rd[x.get()]) and is_numeric_dtype(rd[y.get()])):
raise Exception
x_points.extend(rd[x.get()])
y_points.extend(rd[y.get()])
update_graph()
wm.destroy()
except:
messagebox.showerror("Error", "Enter Data Correctly.")
wm.destroy()
update_graph()
pass
ttk.Button(master=wm, text="PLOT GRAPH", command=plot_csv).pack(padx=15, pady=15)
except:
wm.destroy()
update_graph()
messagebox.showerror("ERROR", "Enter Data Correctly.")
# add multiple points
def add_multi_points():
try:
s = simpledialog.askstring("Add Multiple Points",
"Multiple points should be separated by a comma\nX and Y coordinates are separated using space\n\nFor example 2 3,8 9 will plot points as (2,3) & (8,9)")
global x_points, y_points
l = s.split(",")
for i in l:
j = i.split()
a, b = float(j[0]), float(j[1])
x_points.append(a)
y_points.append(b)
xy = list(zip(x_points, y_points))
xy.sort(key=lambda x: x[0])
x_points = [x[0] for x in xy]
y_points = [x[1] for x in xy]
update_graph()
except:
pass
def set_title_canvas():
global s_graph_canvas_title
x = simpledialog.askstring("Title", "Enter Graph Canvas Title")
s_graph_canvas_title = x
update_graph()
def delete_spoint():
try:
x = simpledialog.askfloat("X Axis", "Enter X co-ordinate of point to be deleted")
y = simpledialog.askfloat("Y Axis", "Enter Y co-ordinate of point to be deleted")
print(x)
print(y)
if x_points.index(x) == y_points.index(y):
x_points.pop(x_points.index(x))
y_points.pop(y_points.index(y))
else:
raise Exception
update_graph()
except:
messagebox.showerror("ERROR", "No such point exists")
root = tk.ThemedTk()
root.title("Plotix 1.0")
width = root.winfo_screenwidth()
height = root.winfo_screenheight()
root.tk.call('tk', 'scaling', 1.5)
root.geometry("%dx%d" % (width, height))
try:
root.iconbitmap('Plotix_logo.ico')
except:
pass
root.set_theme('radiance')
my_notebook = ttk.Notebook(master=root)
my_notebook.pack(expand=1, fill="both")
# canvass and buttons
line_frame = ttk.LabelFrame(master=root, text="Single Line Graph")
ttk.Label(text="Edit Graph", width='50', master=line_frame).grid(row=0, column=0, columnspan=3)
fig = Figure(figsize=(6, 4), dpi=170)
canvas = FigureCanvasTkAgg(fig, master=line_frame) # DrawingArea.
canvas.draw()
canvas.get_tk_widget().grid(column=7, columnspan=3, rowspan=100)
# toolbar
toolbar = NavigationToolbar2Tk(canvas, line_frame, pack_toolbar=False)
toolbar.update()
toolbar.grid(column=7, columnspan=3)
ttk.Label(text="Add Point", master=line_frame, borderwidth=2, relief="solid").grid(row=1, column=0, columnspan=6)
x = ttk.Entry(master=line_frame)
x.insert(0, '')
y = ttk.Entry(master=line_frame)
y.insert(0, '')
add_point_button = ttk.Button(master=line_frame, text='Add Point', width=40, command=add_point)
x.grid(row=2, column=0, padx=(15, 0))
y.grid(row=2, column=2, padx=(0, 20))
add_point_button.grid(row=3, columnspan=6)
ttk.Label(text="Line Style", master=line_frame).grid(row=5, column=0, columnspan=6, pady=(10, 5))
ttk.Button(master=line_frame, text='Default', command=default_line).grid(row=6, column=0, padx=5)
ttk.Button(master=line_frame, text='Dashed', command=dashed_line).grid(row=6, column=1, padx=5)
ttk.Button(master=line_frame, text='Dotted', command=dotted_line).grid(row=6, column=2, padx=5)
ttk.Label(text="Marker Style ", master=line_frame).grid(row=7, column=0, columnspan=6, pady=(10, 5))
ttk.Button(master=line_frame, text='Default Sign', command=default_marker).grid(row=8, column=0, padx=5)
ttk.Button(master=line_frame, text='Plus Sign', command=plus_marker).grid(row=8, column=1, padx=5)
ttk.Button(master=line_frame, text='X Sign', command=x_marker).grid(row=8, column=2, padx=5)
ttk.Label(text="Line Graph Color ", master=line_frame).grid(row=9, column=0, columnspan=6, pady=(10, 5))
curr_graph_color = Label(text=" ", width='10', master=line_frame, background=graph_color)
curr_graph_color.grid(row=10, column=0, pady=5)
ttk.Button(master=line_frame, text="Choose a color", command=pick_color).grid(row=10, column=1, columnspan=2, pady=5)
ttk.Button(master=line_frame, text='Set Graph Line Label', width=40, command=set_title).grid(row=14, columnspan=6,
padx=5, pady=(20, 5))
ttk.Button(master=line_frame, text='Set Axes Titles', width=40, command=change_axes_title).grid(row=15, columnspan=6,
padx=5, pady=5)
ttk.Button(master=line_frame, text='Set Graph Canvas Title', width=40, command=set_title_canvas).grid(row=16,
columnspan=6,
padx=5, pady=5)
ttk.Button(master=line_frame, text='Add Multiple Points', width=40, command=add_multi_points).grid(row=12, columnspan=6,
padx=5, pady=5)
ttk.Button(master=line_frame, text='Add Points From CSV File', width=40, command=csv_single).grid(row=13, columnspan=6,
padx=5, pady=(5, 10))
ttk.Button(master=line_frame, text='Delete A Point', width=40, command=delete_spoint).grid(row=99, columnspan=6, padx=5,
pady=5)
ttk.Button(master=line_frame, text='Clear Graph Canvas', width=40, command=clear_graph).grid(row=100, columnspan=6,
padx=5, pady=5)
line_frame.pack(expand=1, fill='both', padx=15, pady=5)
my_notebook.add(line_frame, text='Single Line Graph')
# Multi Line Graph
# multi line graph array format =[title,[x],[y],[linecolor],[marker_style],[line_style]]
mline = []
mline_canvas_title = " "
mline_frame = ttk.LabelFrame(master=my_notebook, text="Multi Line Graph")
ttk.Label(text="Edit MultiLine Graph", width='50', master=mline_frame).grid(row=0, column=0, columnspan=3)
mline_fig = Figure(figsize=(6, 4), dpi=170)
mline_canvas = FigureCanvasTkAgg(mline_fig, master=mline_frame) # A tk.DrawingArea.
mline_canvas.get_tk_widget().grid(padx=10, column=7, rowspan=100)
mline_canvas.draw()
toolbar = NavigationToolbar2Tk(mline_canvas, mline_frame, pack_toolbar=False)
toolbar.update()
toolbar.grid(column=7)
x_axis, y_axis = 'X-Axis', 'Y-Axis'
# graph=[title,[x],[y],[linecolor],[marker_style],[line_style]]
def update_mline():
mline_fig.clear()
g = mline_fig.add_subplot(title=mline_canvas_title, xlabel=x_axis, ylabel=y_axis)
for i in mline:
xy = list(zip(i[1], i[2]))
xy.sort(key=lambda x: x[0])
x = [x[0] for x in xy]
y = [x[1] for x in xy]
# xlabel=x_label, ylabel=y_label
g.plot(x, y, linestyle=i[5], markeredgecolor='black', marker=i[4], color=i[3], label=i[0])
g.legend()
mline_canvas.draw()
# set canvas title
def set_mtitle_canvas():
global mline_canvas_title
x = simpledialog.askstring("Title", "Enter Graph Canvas Title")
mline_canvas_title = x
update_mline()
# create new graph line
def add_mline_graph():
# graph=[title,[x],[y],[linecolor],[marker_style],[line_style]]
try:
new_graph = tk.ThemedTk()
new_graph.set_theme('radiance')
new_graph.title('Create New Graph')
ttk.Label(master=new_graph, text="Enter Graph Line Title ").pack(padx=20)
mline_title = ttk.Entry(master=new_graph)
mline_title.config(width=25)
mline_title.pack(padx=20, pady=(5, 20))
mline_x, mline_y = [], []
ttk.Label(master=new_graph,
text="Enter co-ordinates in x1 y1,x2 y2...format\nFor example. 1 3,8 7 will plot points at(1,3) and (8,7)").pack(
padx=20)
mline_xy = ttk.Entry(master=new_graph)
mline_xy.config(width=25)
mline_xy.pack(padx=20, pady=(5, 20))
mline_color = StringVar()
mline_color.set('blue')
ttk.Label(master=new_graph, text="Pick Graph Line Color").pack(padx=20)
opc = ttk.OptionMenu(new_graph, mline_color, "black",
*['Red', 'Orange', 'Yellow', 'Black', 'Gray', 'Blue', 'Green'])
opc.config(width=25)
opc.pack(padx=20, pady=(5, 20))
mline_marker = StringVar()
mline_marker.set(".")
ttk.Label(master=new_graph, text="Pick a Marker Style").pack(padx=20)
opm = ttk.OptionMenu(new_graph, mline_marker, ".", *['o', '.', '+', '*'])
opm.config(width=25)
opm.pack(padx=20, pady=(5, 20))
ttk.Label(master=new_graph, text="Pick a Line Style").pack(padx=20)
mline_linestyle = StringVar()
mline_linestyle.set("solid")
opl = ttk.OptionMenu(new_graph, mline_linestyle, "solid", *['solid', 'dotted', 'dashed'])
opl.config(width=25)
opl.pack(padx=20, pady=(5, 20))
def add_line_graph():
for x in mline:
if x[0] == mline_title.get():
messagebox.showerror("ERROR", 'Please Pick A Unique Title')
new_graph.destroy()
add_mline_graph()
return
try:
xy = mline_xy.get().split(',')
for i in xy:
j = i.split()
xp = float(j[0])
yp = float(j[1])
mline_x.append(xp)
mline_y.append(yp)
mline.append(
[mline_title.get(), mline_x, mline_y, mline_color.get(), mline_marker.get(), mline_linestyle.get()])
update_mline()
new_graph.destroy()
except:
new_graph.destroy()
messagebox.showerror("ERROR", "Please input correct data")
update_mline()
ttk.Button(master=new_graph, text="Add Line Graph", width=25, command=add_line_graph).pack(padx=10, pady=15)
except:
update_mline()
# add point to existing line
def add_point_ex():
try:
ex = tk.ThemedTk()
ex.set_theme('radiance')
ex.title('Add New Points To Existing Graph Line')
ttk.Label(master=ex, text="Select Graph Line Title").pack(padx=20)
existing_g = StringVar()
titles = [x[0] for x in mline]
o = ttk.OptionMenu(ex, existing_g, "DEFAULT", *titles)
o.config(width=20)
o.pack(padx=10, pady=(5, 15))
ttk.Label(master=ex,
text="Multiple points should be separated by a comma\nX and Y coordinates are separated using space\n\nFor example 2 3,8 9 will plot points as (2,3) & (8,9)").pack(
padx=20)
xy = ttk.Entry(master=ex, width=20)
xy.pack(padx=20, pady=(10, 20))
def add_p():
try:
index = titles.index(existing_g.get())
xy_p = xy.get().split(",")
xc, yc = [], []
for i in xy_p:
z = i.split()
a = float(z[0])
b = float(z[1])
mline[index][1].append(a)
mline[index][2].append(b)
update_mline()
ex.destroy()
except:
messagebox.showerror("ERROR", "Please Enter Data Correctly.")
ex.destroy()
pass
ttk.Button(master=ex, text="Add Point", command=add_p, width=20).pack(padx=20, pady=(20, 20))
except:
messagebox.showerror("ERROR", "Please Enter Data Correctly")
ex.destroy()
# change attributes
def change_mline_att():
wm = tk.ThemedTk()
wm.set_theme('radiance')
wm.title('Set Attributes')
ttk.Label(master=wm, text="Select Graph Line Title").pack(padx=20)
existing_g = StringVar()
titles = [x[0] for x in mline]
o = ttk.OptionMenu(wm, existing_g, "DEFAULT", *titles)
o.config(width=20)
o.pack(padx=10, pady=(5, 15))
def change_att():
# graph=[title,[x],[y],[linecolor],[marker_style],[line_style]]
try:
wm.destroy()
ex = tk.ThemedTk()
ex.set_theme('radiance')
ex.title('Set Attributes')
index = titles.index(existing_g.get())
mline_color = StringVar()
mline_color.set('blue')
ttk.Label(master=ex, text="Pick Graph Line Color").pack(padx=20)
opc = ttk.OptionMenu(ex, mline_color, mline[index][3],
*['Red', 'Orange', 'Yellow', 'Black', 'Gray', 'Blue', 'Green'])
opc.config(width=25)
opc.pack(padx=20, pady=(5, 20))
mline_marker = StringVar()
mline_marker.set(".")
ttk.Label(master=ex, text="Pick a Marker Style").pack(padx=20)
opm = ttk.OptionMenu(ex, mline_marker, mline[index][4], *['o', '.', '+', '*'])
opm.config(width=25)
opm.pack(padx=20, pady=(5, 20))
ttk.Label(master=ex, text="Pick a Line Style").pack(padx=20)
mline_linestyle = StringVar()
mline_linestyle.set("solid")
opl = ttk.OptionMenu(ex, mline_linestyle, mline[index][5], *['solid', 'dotted', 'dashed'])
opl.config(width=25)
opl.pack(padx=20, pady=(5, 20))
def done():
mline[index][3] = mline_color.get()
mline[index][4] = mline_marker.get()
mline[index][5] = mline_linestyle.get()
update_mline()
ex.destroy()
ttk.Button(master=ex, text="Save Changes", command=done, width=20).pack(padx=10, pady=20)
except:
messagebox.showerror("ERROR", "Select Title")
ex.destroy()
ttk.Button(master=wm, text="Select Graph Title", command=change_att, width=20).pack(padx=10, pady=20)
# axes kabek
def axes_label():
x = simpledialog.askstring("X Axis", "Enter Label for X Axis")
y = simpledialog.askstring("Y Axis", "Enter Label for Y Axis")
global x_axis, y_axis
x_axis = x
y_axis = y
update_mline()
# delete graph
def del_graph():
t = tk.ThemedTk()
t.set_theme('radiance')
t.title('Delete Graph Line')
title = StringVar()
titles = [x[0] for x in mline]
ttk.Label(master=t, text="Select A Graph From DropDown Below").pack(padx=25, pady=10)
op = ttk.OptionMenu(t, title, "DEFAULT", *titles)
op.config(width=20)
op.pack(padx=10, pady=20)
def delete():
try:
ans = messagebox.askyesno("Are You Sure ?", "Do You Want to Delete The Graph Line ?")
if ans != True:
return
index = titles.index(title.get())
mline.pop(index)
update_mline()
t.destroy()
except:
messagebox.showerror("ERROR", "Select a title")
t.destroy()
ttk.Button(master=t, text="Delete Graph", command=delete).pack(padx=10, pady=20)
# del point
def del_point():
try:
t = tk.ThemedTk()
t.set_theme('radiance')
t.title('Delete Point')
title = StringVar()
titles = [x[0] for x in mline]
ttk.Label(master=t, text="Select A Graph From DropDown Below").pack(padx=25, pady=10)
op = ttk.OptionMenu(t, title, "DEFAULT", *titles)
op.config(width=20)
op.pack(padx=10, pady=20)
def delete():
try:
t.destroy()
index = titles.index(title.get())
x = simpledialog.askfloat("X Axis", "Enter X co-ordinate of point to be deleted")
y = simpledialog.askfloat("Y Axis", "Enter Y co-ordinate of point to be deleted")
print(x)
print(y)
if mline[index][1].index(x) == mline[index][2].index(y):
mline[index][1].pop(mline[index][1].index(x))
mline[index][2].pop(mline[index][2].index(y))
if len(mline[index][1]) == 0:
mline.pop(index)
update_mline()
else:
raise Exception
except:
messagebox.showerror("ERROR", "No such point exists")
ttk.Button(master=t, text="Select Graph", command=delete).pack(padx=10, pady=20)
except:
messagebox.showerror("ERROR", "Select a title")
# clear graph
def clear_graph():
global mline_canvas_title
ans = messagebox.askyesno("Are You Sure ?", "Do You Want to Clear The Canvas ?")
if ans != True:
return
global mline
mline = []
mline_canvas_title = ''
update_mline()
# csv import
def add_csv():
try:
file = askopenfile(mode='r', filetypes=[('CSV FILES', '*.csv')])
if file is not None:
rd = pd.read_csv(file)
wm = tk.ThemedTk()
wm.set_theme('radiance')
wm.title('Import Graph From CSV')
lst = list(rd.columns)
ttk.Label(master=wm, text="Select attribute for X Axis ").pack(padx=10, pady=15)
x = StringVar()
y = StringVar()
op1 = ttk.OptionMenu(wm, x, "DEFAULT VALUE", *lst)
op1.config(width=20)
op1.pack(padx=15, pady=15)
ttk.Label(master=wm, text="Select attribute for Y Axis ").pack(padx=10, pady=15)
op2 = ttk.OptionMenu(wm, y, "DEFAULT VALUE", *lst)
op2.config(width=20)
op2.pack(padx=15, pady=15)
ttk.Label(master=wm, text="Enter Graph Line Title ").pack(padx=20)
mline_title = ttk.Entry(master=wm)
mline_title.config(width=25)
mline_title.pack(padx=20, pady=(5, 20))
mline_color = StringVar()
mline_color.set('blue')
ttk.Label(master=wm, text="Pick Graph Line Color").pack(padx=20)
opc = ttk.OptionMenu(wm, mline_color, "black",
*['Red', 'Orange', 'Yellow', 'Black', 'Gray', 'Blue', 'Green'])
opc.config(width=25)
opc.pack(padx=20, pady=(5, 20))
mline_marker = StringVar()
mline_marker.set(".")
ttk.Label(master=wm, text="Pick a marker style").pack(padx=20)
opm = ttk.OptionMenu(wm, mline_marker, ".", *['o', '.', '+', '*'])
opm.config(width=25)
opm.pack(padx=20, pady=(5, 20))
ttk.Label(master=wm, text="Pick a line style").pack(padx=20)
mline_linestyle = StringVar()
mline_linestyle.set("solid")
opl = ttk.OptionMenu(wm, mline_linestyle, "solid", *['solid', 'dotted', 'dashed'])
opl.config(width=25)
opl.pack(padx=20, pady=(5, 20))
def plot_csv():
try:
if not (is_numeric_dtype(rd[x.get()]) and is_numeric_dtype(rd[y.get()])):
raise Exception
for i in mline:
if mline_title.get() == i[0]:
wm.destroy()
messagebox.showerror("ERROR", "Enter Unique Title.")
return
mline.append([mline_title.get(), rd[x.get()], rd[y.get()], mline_color.get(), mline_marker.get(),
mline_linestyle.get()])
update_mline()
wm.destroy()
except:
messagebox.showerror("Error", "Enter Details Correctly")
wm.destroy()
ttk.Button(master=wm, text="PLOT GRAPH", command=plot_csv).pack(padx=15, pady=15)
except:
wm.destroy()
messagebox.showerror("ERROR", "Enter Data Correctly.")
ttk.Button(master=mline_frame, text='Add New Graph Line', width=40, command=add_mline_graph).grid(row=2, columnspan=6,
padx=5, pady=5)
ttk.Button(master=mline_frame, text='Add New Points to Existing Line', width=40, command=add_point_ex).grid(row=4,
columnspan=6,
padx=5,
pady=5)
ttk.Button(master=mline_frame, text='Add Graph Line Using CSV File', width=40, command=add_csv).grid(row=5,
columnspan=6,
padx=5,
pady=(5, 20))
ttk.Button(master=mline_frame, text='Change Graph Line Attributes', width=40, command=change_mline_att).grid(row=6,
columnspan=6,
padx=5,
pady=(
15, 5))
ttk.Button(master=mline_frame, text='Set Axes Labels', width=40, command=axes_label).grid(row=8, columnspan=6, padx=5,
pady=5)
ttk.Button(master=mline_frame, text='Set Graph Canvas Title', width=40, command=set_mtitle_canvas).grid(row=9,
columnspan=6,
padx=5, pady=5)
ttk.Button(master=mline_frame, text='Delete A Graph Line', width=40, command=del_graph).grid(row=99, columnspan=6,
padx=5, pady=5)
ttk.Button(master=mline_frame, text='Delete A Point ', width=40, command=del_point).grid(row=97, columnspan=6, padx=5,
pady=5)
ttk.Button(master=mline_frame, text='Clear Graph Canvas', width=40, command=clear_graph).grid(row=100, columnspan=6,
padx=5, pady=5)
my_notebook.add(mline_frame, text="Multi Line Graph")
# Bar Graph
xb_axis, yb_axis = 'X-Axis', 'Y-Axis'
x_label = []
y_value = []
hatch = []
colors = []
bar_title = " "
# update bar
def update_bargraph():
bar_fig.clear()
bars = bar_fig.add_subplot(title=bar_title, xlabel=xb_axis, ylabel=yb_axis).bar(x_label, y_value)
for i in range(len(x_label)):
bars[i].set_facecolor(colors[i])
bars[i].set_hatch(hatch[i])
bar_canvas.draw()
# add bar
def add_bar():
global x_label, y_label
try:
a = simpledialog.askstring("Title", 'Enter Bar Title')
if a not in x_label:
x_label.append(a)
else:
messagebox.showerror("Enter A Unique Title", "Title should always be unique")
add_bar()
pass
b = simpledialog.askfloat("Value", "Enter Value for the Bar")
b = float(b)
y_value.append(b)
hatch.append(' ')
colors.append('Blue')
except:
messagebox.showerror("ERROR", "Please input correct data")
update_bargraph()
# add hatch
def add_hatching():
try:
hatch_window = tk.ThemedTk()
hatch_window.set_theme('radiance')
hatch_window.title('Add Hatching')
hatch_title = StringVar()
hatch_title.set("DEFAULT")
ttk.Label(master=hatch_window, text="Pick a bar from Dropdown Below").pack()
op = ttk.OptionMenu(hatch_window, hatch_title, "DEFAULT VALUE", *x_label)
op.config(width=25)
op.pack()
hatch_op = ['/', r'\\', '|', '-', '+', 'x', 'o', 'O', '.', '*']
selected_hatch = StringVar()
ttk.Label(master=hatch_window, text="Pick a Hatching Style from Dropdown Below").pack(pady=(15, 5))
h_op = ttk.OptionMenu(hatch_window, selected_hatch, *hatch_op)
h_op.config(width=25)
h_op.pack(padx=20, pady=(0, 20))
def add_hatch_button():
try:
global x_label, hatch
bar_fig.clear()
hatch[x_label.index(hatch_title.get())] = selected_hatch.get()
update_bargraph()
hatch_window.destroy()
except:
pass
ttk.Button(master=hatch_window, text="Add Hatch", command=add_hatch_button).pack(padx=10, pady=10)
op.pack(padx=10, pady=10)
except:
pass
# set color
def set_color():
try:
clr = tk.ThemedTk()
clr.set_theme('radiance')
clr.title('Set Color')
color_title = StringVar()
ttk.Label(master=clr, text="Pick A Bar From Dropdown Below").pack(padx=20, pady=(15, 10))
op = ttk.OptionMenu(clr, color_title, "DEFAULT VALUE", *x_label)
op.config(width=25)
op.pack(padx=20, pady=(10, 20))
ttk.Label(master=clr, text="Pick A Color From Dropdown Below").pack()
color = StringVar()
color.set("Blue")
opc = ttk.OptionMenu(clr, color, "DEFAULT VALUE",
*['Red', 'Orange', 'Yellow', 'Black', 'Gray', 'Blue', 'Green'])
opc.config(width=25)
opc.pack(pady=(10, 20))
def set_bar_color():
try:
colors[x_label.index(color_title.get())] = color.get()
update_bargraph()
clr.destroy()
except:
pass
ttk.Button(master=clr, text="Set Color", command=set_bar_color).pack(padx=10, pady=10)
except:
pass
def delete_bar():
try:
clr = tk.ThemedTk()
clr.set_theme('radiance')
clr.title('Delete Bar Stick')
del_title = StringVar()
ttk.Label(master=clr, text="Pick A Bar To Delete ").pack()
op = ttk.OptionMenu(clr, del_title, "DEFAULT VALUE", *x_label)
op.config(width=25)
op.pack(padx=15, pady=(5, 15))
def del_bar():
try:
ans = messagebox.askyesno("Are You Sure ?", "Do you want to delete the bar stick ?")
if (ans == True):
y_value.pop(x_label.index(del_title.get()))
colors.pop(x_label.index(del_title.get()))
hatch.pop(x_label.index(del_title.get()))
x_label.pop(x_label.index(del_title.get()))
clr.destroy()
else:
pass
update_bargraph()
except:
clr.destroy()
pass
ttk.Button(master=clr, text="DELETE BAR STICK", command=del_bar).pack(padx=10, pady=10)
except:
clr.destroy()
pass
def change_bar_title():
try:
clr = tk.ThemedTk()
clr.set_theme('radiance')
clr.title('Change Bar Title')
del_title = StringVar()
ttk.Label(master=clr, text="Pick A Bar To Change Title of ").pack()
op = ttk.OptionMenu(clr, del_title, "DEFAULT VALUE", *x_label)
op.config(width=25)
op.pack(padx=15, pady=(5, 15))
ttk.Label(master=clr, text=" Type New Title below ").pack()
e = ttk.Entry(master=clr)
e.config(width=20)
e.pack()
def tit_bar():
try:
x_label[x_label.index(del_title.get())] = e.get()
update_bargraph()
clr.destroy()
except:
clr.destroy()
pass
ttk.Button(master=clr, text="Change Title", command=tit_bar).pack(padx=10, pady=10)
except:
clr.destroy()
def add_bar_csv():
try:
file = askopenfile(mode='r', filetypes=[('CSV FILES', '*.csv')])
if file is not None:
rd = pd.read_csv(file, nrows=50)
wm = tk.ThemedTk()
wm.title("Add Bar From CSV")
wm.set_theme('radiance')
lst = list(rd.columns)
ttk.Label(master=wm, text="Select attribute for Labels ").pack(padx=10, pady=15)
x = StringVar()
y = StringVar()
op1 = ttk.OptionMenu(wm, x, "DEFAULT VALUE", *lst)
op1.config(width=20)
op1.pack(padx=15, pady=15)
ttk.Label(master=wm, text="Select attribute for Values ").pack(padx=10, pady=15)
op2 = ttk.OptionMenu(wm, y, "DEFAULT VALUE", *lst)
op2.config(width=20)
op2.pack(padx=15, pady=15)
def plot_csv():
try:
if not (is_numeric_dtype(rd[y.get()])):
raise Exception
c = len(rd.index)
x_label.extend(rd[x.get()])
y_value.extend(rd[y.get()])
colors.extend(['blue'] * c)
hatch.extend([' '] * c)
update_bargraph()
wm.destroy()
except:
messagebox.showerror("ERROR", "Enter Data correctly")
wm.destroy()
ttk.Button(master=wm, text="PLOT GRAPH", command=plot_csv).pack(padx=20, pady=15)
except:
messagebox.showerror("ERROR", "Enter Data correctly")
wm.destroy()
return
def edit_bar_val():
try:
wm = tk.ThemedTk()
wm.set_theme('radiance')
wm.title('Edit Bar Stick')
ttk.Label(master=wm, text="Select A Bar Stick From Dropdown Below ").pack(padx=15, pady=(15, 5))
edit_label = StringVar()
ope = ttk.OptionMenu(wm, edit_label, 'default', *x_label)
ope.config(width=25)
ope.pack(padx=15, pady=(5, 15))
ttk.Label(master=wm, text="Enter Value For The Stick ").pack(padx=15, pady=(15, 5))
e = ttk.Entry(master=wm)
e.config(width=25)
e.pack(padx=15, pady=(15, 5))
def edit_p():
try:
index1 = x_label.index(edit_label.get())
y_value[index1] = float(e.get())
wm.destroy()
if y_value[index1] == 0:
x_label.pop(index1)
y_value.pop(index1)
colors.pop(index1)
hatch.pop(index1)
update_bargraph()
except:
messagebox.showerror("Error", "Please Enter Details Correctly")
ttk.Button(master=wm, text='Set Bar Stick Value', command=edit_p, width=25).pack(padx=15, pady=15)
except:
pass
def clear_bargraph():
ans = messagebox.askyesno("Are You Sure ?", "Do You Want to Clear The Canvas ?")
if ans != True:
return
global x, x_label, y_value, hatch, colors, bar_title
x_label = []
y_value = []
hatch = []
colors = []
bar_title = ''
update_bargraph()