-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_screen.py
189 lines (162 loc) · 6.76 KB
/
graph_screen.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
import PySimpleGUI as sg
import matplotlib.pyplot as plt
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
# VARS CONSTS:
_VARS = {'window': False,
'fig_agg': False,
'pltFig': False}
# Theme for pyplot
plt.style.use('Solarize_Light2')
# Helper Functions
def update_annot(ind, annot, sc, names, im, ab, imgs):
pos = sc.get_offsets()[ind["ind"][0]]
annot.xy = pos
ab.xy = pos
annot.set_text(str(names[ind["ind"][0]]))
im.set_data(plt.imread(str(imgs[(ind["ind"][0])])))
annot.get_bbox_patch().set_alpha(0.4)
def hover(event, annot, sc, fig, ax, names, img, ab, imgs):
vis = annot.get_visible()
if event.inaxes == ax:
cont, ind = sc.contains(event)
if cont:
update_annot(ind, annot, sc, names, img, ab, imgs)
annot.set_visible(True)
ab.set_visible(True)
fig.canvas.draw_idle()
else:
if vis:
annot.set_visible(False)
ab.set_visible(False)
fig.canvas.draw_idle()
def draw_figure(canvas, figure):
figure_canvas_agg = FigureCanvasTkAgg(figure, canvas)
figure_canvas_agg.draw()
figure_canvas_agg.get_tk_widget().pack(side='top', fill='both', expand=1)
return figure_canvas_agg
def get_label(x_select, y_select):
xlabel = ylabel = ""
print(x_select)
print(y_select)
if x_select == 'time':
xlabel = 'Time (s)'
elif x_select == 'cost':
xlabel = 'Cost ($)'
elif x_select == 'mass':
xlabel = 'Mass (g)'
else:
xlabel = 'Displacement (mm)'
if y_select == 'time':
ylabel = 'Time (s)'
elif y_select == 'cost':
ylabel = 'Cost ($)'
elif y_select == 'mass':
ylabel = 'Mass (g)'
else:
ylabel = 'Displacement (mm)'
return xlabel, ylabel
def draw_figure_w_toolbar(canvas, fig, canvas_toolbar):
if canvas.children:
for child in canvas.winfo_children():
child.destroy()
if canvas_toolbar.children:
for child in canvas_toolbar.winfo_children():
child.destroy()
figure_canvas_agg = FigureCanvasTkAgg(fig, master=canvas)
figure_canvas_agg.draw()
toolbar = Toolbar(figure_canvas_agg, canvas_toolbar)
toolbar.update()
figure_canvas_agg.get_tk_widget().pack(side='right', fill='both', expand=1)
return figure_canvas_agg
class Toolbar(NavigationToolbar2Tk):
def __init__(self, *args, **kwargs):
super(Toolbar, self).__init__(*args, **kwargs)
# \\ -------- PYSIMPLEGUI -------- //
AppFont = 'Any 16'
sg.theme('black')
def getData(bids, x_select, y_select):
if x_select == 'Displacement':
x_select = 'disp'
if y_select == 'Displacement':
y_select = 'disp'
return [bid[x_select.lower()] for bid in bids], [bid[y_select.lower()] for bid in bids], \
[bid["link"] for bid in bids], [bid["color"] for bid in bids], [bid["pic"] for bid in bids]
def drawChart(bids, x_select, y_select):
layout = [[sg.T('Controls:'), sg.Canvas(key='controls_cv'), sg.Push(),
sg.Button('Back to Options List', enable_events=True, key='backtoresults')],
[sg.Column(
layout=[
[sg.Canvas(key='fig_cv',
# it's important that you set this size
size=(400 * 2, 400)
)]
],
background_color='#DAE0E6',
pad=(0, 0))],
[sg.Text("Select X"),
sg.OptionMenu(['Time', 'Cost', 'Mass', 'Displacement'], s=(15, 2), key='x_option', default_value='Time'),
sg.Text("Select Y"),
sg.OptionMenu(['Time', 'Cost', 'Mass', 'Displacement'], s=(15, 2), key='y_option', default_value='Cost'),
sg.Button('View Graph', font=AppFont, enable_events=True, key="viewgraph")]]
_VARS['window'] = sg.Window('Such Window',
layout,
finalize=True,
resizable=True,
location=(100, 100),
element_justification="center")
_VARS['pltFig'] = plt.figure()
dataXY = getData(bids, x_select, y_select)
x = dataXY[0]
y = dataXY[1]
names = dataXY[2]
colors = dataXY[3]
imgs = dataXY[4]
im = OffsetImage(plt.imread(str(imgs[0])))
fig, ax = plt.subplots()
sc = plt.scatter(x, y, c=colors, s=36, edgecolors='black')
annot = ax.annotate("", xy=(0, 0), xytext=(20, 20), textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
xybox = (75., -75.)
ab = AnnotationBbox(im, (0, 0), xybox=xybox, xycoords='data',
boxcoords="offset points", pad=0.3, arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
ab.set_visible(False)
annot.set_visible(False)
fig.canvas.mpl_connect("motion_notify_event", lambda event: hover(event, annot, sc, fig, ax, names, im, ab, imgs))
xlabel, ylabel = get_label(x_select.lower(), y_select.lower())
plt.xlabel(xlabel)
plt.ylabel(ylabel)
DPI = fig.get_dpi()
fig.set_size_inches(404 * 2 / float(DPI), 404 / float(DPI))
_VARS['fig_agg'] = draw_figure_w_toolbar(_VARS['window']['fig_cv'].TKCanvas, fig, _VARS['window']['controls_cv'].TKCanvas)
return _VARS['window']
def updateChart(bids, x_select, y_select):
_VARS['fig_agg'].get_tk_widget().forget()
plt.clf()
dataXY = getData(bids, x_select, y_select)
x = dataXY[0]
y = dataXY[1]
names = dataXY[2]
colors = dataXY[3]
imgs = dataXY[4]
im = OffsetImage(plt.imread(str(imgs[0])))
fig, ax = plt.subplots()
sc = plt.scatter(x, y, c=colors, s=36, edgecolors='black')
annot = ax.annotate("", xy=(0, 0), xytext=(20, 20), textcoords="offset points",
bbox=dict(boxstyle="round", fc="w"),
arrowprops=dict(arrowstyle="->"))
annot.set_visible(False)
xybox = (75., -75.)
ab = AnnotationBbox(im, (0, 0), xybox=xybox, xycoords='data',
boxcoords="offset points", pad=0.3, arrowprops=dict(arrowstyle="->"))
ax.add_artist(ab)
ab.set_visible(False)
fig.canvas.mpl_connect("motion_notify_event", lambda event: hover(event, annot, sc, fig, ax, names, im, ab, imgs))
xlabel, ylabel = get_label(x_select.lower(), y_select.lower())
plt.xlabel(xlabel)
plt.ylabel(ylabel)
DPI = fig.get_dpi()
fig.set_size_inches(404 * 2 / float(DPI), 404 / float(DPI))
_VARS['fig_agg'] = draw_figure_w_toolbar(_VARS['window']['fig_cv'].TKCanvas, fig, _VARS['window']['controls_cv'].TKCanvas)