MCPcopy Index your code
hub / github.com/PyQt5/PyQt / CpuLineChart

Class CpuLineChart

QtChart/CpuLineChart.py:22–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

20
21
22class CpuLineChart(QChart):
23
24 def __init__(self, *args, **kwargs):
25 super(CpuLineChart, self).__init__(*args, **kwargs)
26 self.m_count = 10
27 # 隐藏图例
28 self.legend().hide()
29 self.m_series = QSplineSeries(self)
30 # 设置画笔
31 self.m_series.setPen(QPen(QColor('#3B8CFF'), 2, Qt.SolidLine, Qt.RoundCap, Qt.RoundJoin))
32 self.addSeries(self.m_series)
33 # x轴
34 self.m_axisX = QDateTimeAxis(self)
35 self.m_axisX.setTickCount(self.m_count + 1) # 设置刻度数量
36 self.m_axisX.setFormat('hh:mm:ss') # 设置时间显示格式
37 now = QDateTime.currentDateTime() # 前10秒到现在
38 self.m_axisX.setRange(now.addSecs(-self.m_count), now)
39 self.addAxis(self.m_axisX, Qt.AlignBottom)
40 self.m_series.attachAxis(self.m_axisX)
41 # y轴
42 self.m_axisY = QValueAxis(self)
43 self.m_axisY.setLabelFormat('%d') # 设置文本格式
44 self.m_axisY.setMinorTickCount(4) # 设置小刻度线的数目
45 self.m_axisY.setTickCount(self.m_count + 1)
46 self.m_axisY.setRange(0, 100)
47 self.addAxis(self.m_axisY, Qt.AlignLeft)
48 self.m_series.attachAxis(self.m_axisY)
49
50 # 填充11个初始点,注意x轴 需要转为秒的时间戳
51 self.m_series.append(
52 [QPointF(now.addSecs(-i).toMSecsSinceEpoch(), 0) for i in range(self.m_count, -1, -1)])
53
54 # 定时器获取数据
55 self.m_timer = QTimer()
56 self.m_timer.timeout.connect(self.update_data)
57 self.m_timer.start(1000)
58
59 def update_data(self):
60 value = cpu_percent()
61 now = QDateTime.currentDateTime()
62 self.m_axisX.setRange(now.addSecs(-self.m_count), now) # 重新调整x轴的时间范围
63 # 获取原来的所有点,去掉第一个并追加新的一个
64 points = self.m_series.pointsVector()
65 points.pop(0)
66 points.append(QPointF(now.toMSecsSinceEpoch(), value))
67 # 替换法速度更快
68 self.m_series.replace(points)
69
70
71if __name__ == '__main__':

Callers 1

CpuLineChart.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected