Skip to content

Commit 8b9c8ec

Browse files
committed
CPU动态折线图
1 parent 76a4fd7 commit 8b9c8ec

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

QtChart/CpuLineChart.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2021/5/13
6+
@author: Irony
7+
@site: https://github.com/PyQt5
8+
@email: 892768447@qq.com
9+
@file: CpuLineChart
10+
@description:
11+
"""
12+
13+
import sys
14+
15+
from PyQt5.QtChart import QChartView, QChart, QSplineSeries, QDateTimeAxis, QValueAxis
16+
from PyQt5.QtCore import Qt, QTimer, QDateTime, QPointF
17+
from PyQt5.QtGui import QPainter, QPen, QColor
18+
from PyQt5.QtWidgets import QApplication
19+
from psutil import cpu_percent
20+
21+
22+
class 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+
71+
if __name__ == '__main__':
72+
app = QApplication(sys.argv)
73+
chart = CpuLineChart()
74+
chart.setTitle('cpu')
75+
# chart.setAnimationOptions(QChart.SeriesAnimations)
76+
77+
view = QChartView(chart)
78+
view.setRenderHint(QPainter.Antialiasing) # 抗锯齿
79+
view.resize(800, 600)
80+
view.show()
81+
sys.exit(app.exec_())

QtChart/README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- [横向百分比柱状图表](#13横向百分比柱状图表)
1717
- [散点图表](#14散点图表)
1818
- [图表主题动画](#15图表主题动画)
19+
- [CPU动态折线图](#16CPU动态折线图)
1920

2021
## 1、折线图
2122
[运行 LineChart.py](LineChart.py)
@@ -94,4 +95,11 @@
9495
## 15、图表主题动画
9596
[运行 ChartThemes.py](ChartThemes.py)
9697

97-
![ChartThemes](ScreenShot/ChartThemes.gif)
98+
![ChartThemes](ScreenShot/ChartThemes.gif)
99+
100+
## 16、CPU动态折线图
101+
[运行 CpuLineChart.py](CpuLineChart.py)
102+
103+
通过设置x轴的时间范围并替换y点达到动态移动效果
104+
105+
![CpuLineChart](ScreenShot/CpuLineChart.png)

QtChart/ScreenShot/CpuLineChart.png

25.8 KB
Loading

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分
174174
- [横向百分比柱状图表](QtChart/HorizontalPercentBarChart.py)
175175
- [散点图表](QtChart/ScatterChart.py)
176176
- [图表主题动画](QtChart/ChartThemes.py)
177+
- [CPU动态折线图](QtChart/CpuLineChart.py)
177178

178179
- [QtDataVisualization](QtDataVisualization)
179180
- [柱状图3D](QtDataVisualization/BarsVisualization.py)

0 commit comments

Comments
 (0)