forked from baronleonardo/Serial
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchartview.py
More file actions
140 lines (109 loc) · 3.6 KB
/
chartview.py
File metadata and controls
140 lines (109 loc) · 3.6 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
from PyQt5.QtChart import QChartView, QChart, QLineSeries
from PyQt5.QtGui import QPen, QPainter
from PyQt5.QtCore import Qt, QEvent
class ChartView(QChartView):
"""docstring for ChartView"""
x = 0
is_chart_drawing = True
max_y = 0
min_y = 0
counter_X = 0
counter_Y = 0
def __init__(self, parent=None):
super(ChartView, self).__init__(parent)
self.parent = parent
self.chart = Chart()
self.setChart(self.chart)
self.setRenderHint(QPainter.Antialiasing)
def draw_point(self, y):
if self.is_chart_drawing is True:
self.chart.add_point(self.x, y)
self.x += 1
# scroll and remove old points to keep always seen points = MAX_X
if self.x >= self.chart.MAX_X:
self.chart.scroll(self.chart.plotArea().width() / self.chart.MAX_X, 0)
self.chart.remove_point(0)
self.__calculate_peak(y)
def __calculate_peak(self, y):
if self.counter_X == self.chart.MAX_X:
self.max_y = 0
self.min_y = 0
self.counter_X = 0
elif self.counter_Y == self.chart.MAX_X:
self.max_y = 0
self.min_y = 0
self.counter_Y = 0
if self.max_y < y:
self.max_y = y
self.chart.axisY().setMax(y + (0.1 * y))
self.counter_X = 0
elif self.min_y > y:
self.min_y = y
self.chart.axisY().setMin(y + (0.1 * y))
self.counter_Y = 0
self.counter_X += 1
self.counter_Y += 1
def on_pause_chart(self, state:bool):
if state is True:
self.is_chart_drawing = False
else:
self.is_chart_drawing = True
def reset(self):
del(self.chart)
self.chart = Chart()
self.setChart(self.chart)
self.x = 0
self.max_y = 0
self.min_y = 0
self.counter_X = 0
self.counter_Y = 0
class Chart(QChart):
MIN_X = 0
MAX_X = 750
MIN_Y = -1
MAX_Y = 1
TICKS = 5
PENCOLOR = Qt.red
PENWIDTH = 1
def __init__(self, parent=None):
super(Chart, self).__init__(parent)
self.parent = parent
# we will draw lines
self.series = QLineSeries(parent)
# color and pen-width
self.series.setPen(QPen(self.PENCOLOR, self.PENWIDTH))
self.addSeries(self.series)
self.legend().hide()
self.__construct_axises()
def setRange_X_axis(self, min_X, max_X):
self.MIN_X = min_X
self.MAX_X = max_X
self.axisX().setRange(self.MIN_X, self.MAX_X)
def setRange_Y_axis(self, min_Y, max_Y):
self.MIN_Y = min_Y
self.MAX_Y = max_Y
self.axisY().setRange(self.MIN_Y, self.MAX_Y)
def add_point(self, x ,y):
self.series.append(x, y)
def get_series(self) -> list:
return self.series.pointsVector()
def remove_point(self, index):
self.series.remove(index)
def remove_points(self, index, count):
self.series.removePoints(index, count)
def replace_point(self, index, x, y):
self.series.replace(index , x , y)
def replace_series(self, lst):
self.series.replace(lst)
def get_series_count(self):
return self.series.count()
def __construct_axises(self):
self.createDefaultAxes()
# X-Axis
x_axis = self.axisX()
x_axis.hide()
x_axis.setRange(self.MIN_X, self.MAX_X)
# Y-axis
y_axis = self.axisY()
y_axis.setRange(self.MIN_Y, self.MAX_Y)
y_axis.setTickCount(self.TICKS)