-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlineChart.py
303 lines (239 loc) · 10.6 KB
/
lineChart.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
#!/usr/bin/env python
#############################################################################
##
## Copyright (C) 2013 Riverbank Computing Limited
## Copyright (C) 2012 Digia Plc
## All rights reserved.
##
## This file is part of the PyQtChart examples.
##
## $QT_BEGIN_LICENSE$
## Licensees holding valid Qt Commercial licenses may use this file in
## accordance with the Qt Commercial License Agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and Digia.
## $QT_END_LICENSE$
##
#############################################################################
import random
import math
import time
import threading
from PyQt5.QtChart import (QAreaSeries, QBarSet, QChart, QChartView,
QLineSeries, QPieSeries, QScatterSeries, QSplineSeries,
QStackedBarSeries, QValueAxis)
from PyQt5.QtCore import pyqtSlot, QPoint, QPointF, Qt, QEvent, QRectF, QTimer
from PyQt5.QtGui import QColor, QPainter, QPalette
from PyQt5.QtWidgets import (QCheckBox, QComboBox, QGridLayout, QHBoxLayout,
QLabel, QSizePolicy, QWidget)
class TestChart(QChart):
def __init__(self, parent=None):
super(TestChart, self).__init__(parent)
self.xRange = 800
self.sampleRate = 5
self.counter = 0
self.seriesList = []
self.legend().show()
self.axisX = QValueAxis()
self.axisX.setRange(0, self.xRange)
self.addAxis(self.axisX, Qt.AlignBottom)
# self.setAxisX(self.axisX, series)
self.axisY = QValueAxis()
self.axisY.setRange(-1, 1)
self.addAxis(self.axisY, Qt.AlignLeft)
# self.setAxisY(self.axisY, series)
for i in range(24):
series = QLineSeries()
series.setName("Series" + str(i))
series.setUseOpenGL(True)
self.addSeries(series)
self.seriesList.append(series)
series.attachAxis(self.axisX)
series.attachAxis(self.axisY)
def handleUpdate(self):
for channel in range(24):
# print(channel)
if (self.counter < self.xRange):
for i in range(self.sampleRate):
self.seriesList[channel].append(self.counter+i, math.sin((self.counter+channel*5+i)*math.pi/180))
else:
points = self.seriesList[channel].pointsVector()
for i in range(len(points) - self.sampleRate):
points[i].setY(points[i + self.sampleRate].y())
for i in range(self.sampleRate):
points[len(points) - (self.sampleRate - i)].setY(math.sin((self.counter+channel*5+i)*math.pi/180))
self.seriesList[channel].replace(points)
self.counter += self.sampleRate
class ThemeWidget(QWidget):
def __init__(self, parent=None):
super(ThemeWidget, self).__init__(parent)
self.m_charts = []
self.timer = QTimer()
# self.timer.setInterval(25)
# self.timer.start()
self.m_listCount = 3
self.m_valueMax = 10
self.m_valueCount = 7
self.m_dataTable = self.generateRandomData(self.m_listCount,
self.m_valueMax, self.m_valueCount)
self.m_themeComboBox = self.createThemeBox()
self.m_antialiasCheckBox = QCheckBox("Anti-aliasing")
self.m_animatedComboBox = self.createAnimationBox()
self.m_legendComboBox = self.createLegendBox()
self.connectSignals()
# Create the layout.
baseLayout = QGridLayout()
settingsLayout = QHBoxLayout()
settingsLayout.addWidget(QLabel("Theme:"))
settingsLayout.addWidget(self.m_themeComboBox)
settingsLayout.addWidget(QLabel("Animation:"))
settingsLayout.addWidget(self.m_animatedComboBox)
settingsLayout.addWidget(QLabel("Legend:"))
settingsLayout.addWidget(self.m_legendComboBox)
settingsLayout.addWidget(self.m_antialiasCheckBox)
settingsLayout.addStretch()
baseLayout.addLayout(settingsLayout, 0, 0, 1, 3)
# Create the charts.
# chartView = QChartView(self.createLineChart())
self.myChart = TestChart()
chartView = QChartView(self.myChart)
baseLayout.addWidget(chartView)
self.m_charts.append(chartView)
self.setLayout(baseLayout)
# Set the defaults.
self.m_antialiasCheckBox.setChecked(True)
self.updateUI()
receiveProcess = threading.Thread(target=self.test)
receiveProcess.setDaemon(True)
receiveProcess.start()
def test(self):
while True:
self.onTimerOut()
time.sleep(0.025)
def connectSignals(self):
self.m_themeComboBox.currentIndexChanged.connect(self.updateUI)
self.m_antialiasCheckBox.toggled.connect(self.updateUI)
self.m_animatedComboBox.currentIndexChanged.connect(self.updateUI)
self.m_legendComboBox.currentIndexChanged.connect(self.updateUI)
self.timer.timeout.connect(self.onTimerOut)
def onTimerOut(self):
# print('time out')
start = time.clock()
self.myChart.handleUpdate()
QApplication.processEvents()
elapsed = (time.clock() - start)
print("Time used: %.3fs" % elapsed)
def generateRandomData(self, listCount, valueMax, valueCount):
random.seed()
dataTable = []
for i in range(listCount):
dataList = []
yValue = 0.0
f_valueCount = float(valueCount)
for j in range(valueCount):
yValue += random.uniform(0, valueMax) / f_valueCount
value = QPointF(
j + random.random() * self.m_valueMax / f_valueCount,
yValue)
label = "Slice " + str(i) + ":" + str(j)
dataList.append((value, label))
dataTable.append(dataList)
return dataTable
def createThemeBox(self):
themeComboBox = QComboBox()
themeComboBox.addItem("Light", QChart.ChartThemeLight)
themeComboBox.addItem("Blue Cerulean", QChart.ChartThemeBlueCerulean)
themeComboBox.addItem("Dark", QChart.ChartThemeDark)
themeComboBox.addItem("Brown Sand", QChart.ChartThemeBrownSand)
themeComboBox.addItem("Blue NCS", QChart.ChartThemeBlueNcs)
themeComboBox.addItem("High Contrast", QChart.ChartThemeHighContrast)
themeComboBox.addItem("Blue Icy", QChart.ChartThemeBlueIcy)
return themeComboBox
def createAnimationBox(self):
animationComboBox = QComboBox()
animationComboBox.addItem("No Animations", QChart.NoAnimation)
animationComboBox.addItem("GridAxis Animations", QChart.GridAxisAnimations)
animationComboBox.addItem("Series Animations", QChart.SeriesAnimations)
animationComboBox.addItem("All Animations", QChart.AllAnimations)
return animationComboBox
def createLegendBox(self):
legendComboBox = QComboBox()
legendComboBox.addItem("No Legend ", 0)
legendComboBox.addItem("Legend Top", Qt.AlignTop)
legendComboBox.addItem("Legend Bottom", Qt.AlignBottom)
legendComboBox.addItem("Legend Left", Qt.AlignLeft)
legendComboBox.addItem("Legend Right", Qt.AlignRight)
return legendComboBox
def createLineChart(self):
chart = QChart()
chart.setTitle("Line chart")
for i, data_list in enumerate(self.m_dataTable):
series = QLineSeries(chart)
for value, _ in data_list:
series.append(value)
series.setName("Series " + str(i))
series.setUseOpenGL(True)
chart.addSeries(series)
chart.createDefaultAxes()
return chart
@pyqtSlot()
def updateUI(self):
theme = self.m_themeComboBox.itemData(
self.m_themeComboBox.currentIndex())
if self.m_charts[0].chart().theme() != theme:
for chartView in self.m_charts:
chartView.chart().setTheme(theme)
pal = self.window().palette()
if theme == QChart.ChartThemeLight:
pal.setColor(QPalette.Window, QColor(0xf0f0f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeDark:
pal.setColor(QPalette.Window, QColor(0x121218))
pal.setColor(QPalette.WindowText, QColor(0xd6d6d6))
elif theme == QChart.ChartThemeBlueCerulean:
pal.setColor(QPalette.Window, QColor(0x40434a))
pal.setColor(QPalette.WindowText, QColor(0xd6d6d6))
elif theme == QChart.ChartThemeBrownSand:
pal.setColor(QPalette.Window, QColor(0x9e8965))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeBlueNcs:
pal.setColor(QPalette.Window, QColor(0x018bba))
pal.setColor(QPalette.WindowText, QColor(0x404044))
elif theme == QChart.ChartThemeHighContrast:
pal.setColor(QPalette.Window, QColor(0xffab03))
pal.setColor(QPalette.WindowText, QColor(0x181818))
elif theme == QChart.ChartThemeBlueIcy:
pal.setColor(QPalette.Window, QColor(0xcee7f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
else:
pal.setColor(QPalette.Window, QColor(0xf0f0f0))
pal.setColor(QPalette.WindowText, QColor(0x404044))
self.window().setPalette(pal)
checked = self.m_antialiasCheckBox.isChecked()
for chartView in self.m_charts:
chartView.setRenderHint(QPainter.Antialiasing, checked)
options = QChart.AnimationOptions(
self.m_animatedComboBox.itemData(
self.m_animatedComboBox.currentIndex()))
if self.m_charts[0].chart().animationOptions() != options:
for chartView in self.m_charts:
chartView.chart().setAnimationOptions(options)
alignment = self.m_legendComboBox.itemData(
self.m_legendComboBox.currentIndex())
for chartView in self.m_charts:
legend = chartView.chart().legend()
if alignment == 0:
legend.hide()
else:
legend.setAlignment(Qt.Alignment(alignment))
legend.show()
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
app = QApplication(sys.argv)
window = QMainWindow()
widget = ThemeWidget()
window.setCentralWidget(widget)
window.resize(900, 600)
window.show()
sys.exit(app.exec_())