MCPcopy Create free account
hub / github.com/PyQt5/PyQt / __init__

Method __init__

QtChart/CpuLineChart.py:24–57  ·  view source on GitHub ↗
(self, *args, **kwargs)

Source from the content-addressed store, hash-verified

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()

Callers

nothing calls this directly

Calls 3

setPenMethod · 0.80
setRangeMethod · 0.45
startMethod · 0.45

Tested by

no test coverage detected