|
| 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_()) |
0 commit comments