/
dynLineChart.py
163 lines (126 loc) · 3.71 KB
/
dynLineChart.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
# dynLineChart - application for showing pyGUI with Line Chart
# ============ with dynamic data
import random
from GUI import Window, View, Label, Button, Task, Application, Menu
from GUI.StdColors import red, green, black
# definitions of application information
windowTitle = "dynLineChart"
# view layout
polyViewWidth = 200
polyViewHeight = 200
pvheight = 150
# shared views, label, button
view = None;
labelNum = None
stater = None
buttonStop = None
# shared data
data = [];
delta = 5
postest = 0
max_data = 37
# DynLineChartView for dynamic line chart with data
class DynLineChartView(View):
def draw(self, c, r):
global data
c.forecolor = black
c.fill_rect(r)
c.forecolor = green
c.newpath()
c.moveto(0, pvheight)
left, top, right, bottom = r
h = bottom - top
# print "w=%d,h=%d" % (right - left, h)
nLen = len(data)
for i in range(nLen):
c.lineto(i * 5, h - data[i])
c.lineto((nLen-1) * 5, pvheight)
c.closepath()
c.fill()
c.forecolor = red
c.stroke()
def make_label(text, **kwds):
return Label(text = text, **kwds)
def do_task():
global view, data, postest, delta,labelNum
if len(data) == 0:
for i in range(max_data):
data.append(0)
if (postest <= 10):
delta = 5
elif (postest >= 100):
delta = -5
postest += delta;
num = postest + random.uniform(1,10)
data.append(num)
labelNum.text = "%.2f" % num
if (len(data) > max_data):
del(data[0])
view.invalidate();
task = Task(do_task, 0.5, repeat = 1, start = 0)
def enableButtons(started):
if (started):
buttonStart.enabled = False
buttonStop.enabled = True
else:
buttonStart.enabled = True
buttonStop.enabled = False
def start_task():
enableButtons(True)
task.start()
def stop_task():
enableButtons(False)
task.stop()
class MainApp(Application):
def __init__(self):
Application.__init__(self)
class MainWindow(Window):
def setup_menus(self, m):
m.start_cmd.enabled = buttonStart.enabled
m.stop_cmd.enabled = buttonStop.enabled
def start_cmd(self):
start_task()
def stop_cmd(self):
stop_task()
# Menu for Action
app_menus = [Menu("Action", [
("Start/^S", 'start_cmd'),
("-"),
("Stop/^O", 'stop_cmd'),
])]
def main():
global view,labelNum, buttonStart, buttonStop
app = MainApp()
buttonStart = Button("Start", action = start_task)
buttonStop = Button("Stop", action = stop_task)
splitSpace = 30
labelNum = make_label("0", just = 'center', color=red, width=buttonStart.width*2 + splitSpace);
labelNumCaption = make_label("Num:", just = 'center', color=red, width=buttonStart.width);
# set positions for the buttons and labels
buttonStart.position = (20, 20);
buttonStop.position = (20 + buttonStart.width + splitSpace, 20);
labelNum.position = (20, 0)
labelNumCaption.position = (20, 0)
# create new window and dynamic linechart view
view = DynLineChartView(width=polyViewWidth, height=polyViewHeight)
view.left = buttonStart.left
view.top = buttonStart.top + buttonStart.height + 5
win = MainWindow(title = windowTitle)
# custized layouts
windowWidth= 20 + polyViewWidth
windowHeight= 210
win.size = (windowWidth, windowHeight)
# add controls
win.add(labelNum);
win.add(labelNumCaption);
win.add(buttonStart);
win.add(buttonStop);
# add DynLineChart view
win.add(view);
win.menus = app_menus
# enabled on/off
enableButtons(False)
# show window
win.show()
app.run()
main()