import datetime
import time
import sys
import time
import random
from PyQt5.Qt import *
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtChart import *
class View_event(QWidget):
View_eventSignal = pyqtSignal()
# view 总窗口
def __init__(self):
super(View_event, self).__init__()
# self.setupUi(self)
self.i=0
# 执行折线视图函数
self.create_chart()
self.selfID = ""
self.datalist = []
self.datalist2 = []
for i in range(0,20):
self.datalist.append(0)
self.datalist2.append(0)
self.timer = QTimer(self) #更新时间戳,
self.timer.start(1000) #每隔x毫秒刷新数据
self.timer.timeout.connect(self.create_series)
def create_chart(self):
# 创建折线视图窗口
self.chart = QChartView(self)
self.chart.setRubberBand(QChartView.RectangleRubberBand)
self.chart.setGeometry(QtCore.QRect(0, 0, 980, 480))
self.chart.setRenderHint(QPainter.Antialiasing) # 抗锯齿
self.chart.raise_()
self.chart._chart = QChart() # 创建折线视图
self.chart._chart.setBackgroundVisible(visible=False) # 背景色透明
self.chart._chart.setBackgroundBrush(QBrush(QColor("#000FFF"))) # 改变图背景色
# 图形项默认无法接收悬停事件,可以使用QGraphicsItem的setAcceptHoverEvents()函数使图形项可以接收悬停事件。
# self.chart._chart.setAcceptHoverEvents(True)
# 4条折线的坐标值
# 执行创建折线的函数
self.series = QLineSeries(self)
self.series.setName("进水") # 1#折线命名
self.chart._chart.addSeries(self.series)
self.series2 = QLineSeries(self)
self.series2.setName("出水") # 2#折线命名
self.chart._chart.addSeries(self.series2)
# self.chart._chart.legend().setVisible(False) # 表头是否可见
# 自定义x轴
self.x_Aix1 = QDateTimeAxis() # 定义x轴,QCategoryAxis(文字描述坐标轴)
# self.x_Aix1.setFormat("yyyy-MM-dd HH:mm:ss") # 设定坐标格式
self.x_Aix1.setFormat("HH:mm:ss") # 设定坐标格式
self.x_Aix1.setLabelsAngle(60) # 倾斜角度
# x_Aix1.setTickCount(11) # 字体大小
curDateTIme = QDateTime.currentDateTime()
self.x_Aix1.setRange(curDateTIme, curDateTIme.addSecs(10)) # 设置量程
self.x_Aix1.setTickCount(10) # 设置每个单元格有几个小的分级
# 定义多个y轴
self.y1_Aix = QValueAxis() # 定义y1轴
self.y1_Aix.setLabelFormat("%d")
self.y1_Aix.setRange(0,200)
self.y1_Aix.setLabelsColor(QColor("#DC143C"))
self.y1_Aix.setLabelsEditable(True)
self.y1_Aix .setTickCount(10)
self.y1_Aix.setTitleText('出水压力(kpa)')
self.y2_Aix = QValueAxis() # 定义y2轴
self.y2_Aix.setLabelFormat("%d")
self.y2_Aix.setRange(0,200)
self.y2_Aix.setLabelsColor(QColor("#0000FF"))
self.y2_Aix.setLabelsEditable(True)
self.y2_Aix .setTickCount(10)
self.y2_Aix.setTitleText('出水压力(kpa)')
self.chart._chart.addAxis(self.y1_Aix, Qt.AlignLeft) # 添加到左侧
self.chart._chart.addAxis(self.y2_Aix, Qt.AlignRight) # 添加到右侧
self.chart._chart.setAxisX(self.x_Aix1, self.series)
self.chart.setChart(self.chart._chart)
self.series.attachAxis(self.y1_Aix)
self.series.attachAxis(self.x_Aix1)
self.series2.attachAxis(self.y2_Aix)
self.series2.attachAxis(self.x_Aix1)
def create_series(self):
dataTable = random.randint(90,100)
dataTable2 = random.uniform(90,100)
self.datalist.append(dataTable)
self.datalist2.append(dataTable2)
self.datalist.pop(0)
self.datalist2.pop(0)
curDateTIme = QDateTime.currentDateTime()
for i in range(0,20):
temp_AddTimePos = curDateTIme.addSecs(i)
# self.series.append(temp_AddTimePos.toMSecsSinceEpoch(), qrand()%18+2)
self.series.append(temp_AddTimePos.toMSecsSinceEpoch(), self.datalist[i])
self.series2.append(temp_AddTimePos.toMSecsSinceEpoch(), self.datalist2[i])
self.x_Aix1.setRange(curDateTIme, curDateTIme.addSecs(20)) # 横坐标轴实时更新
if self.series.count() > 20: #删除多余的数据量
self.series.removePoints(0, self.series.count() - 20)
if self.series2.count() > 20:
self.series2.removePoints(0, self.series2.count() - 20)
print(self.datalist)
print(self.series.count())
# self.series2.append(temp_AddTimePos.toMSecsSinceEpoch(), qrand()%18);
# for i in range(0,20):
# self.series.append(i, self.datalist[i])
# self.series2.append(i, self.datalist2[i])
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
# 初始化所有视图
login = View_event()
login.show()
sys.exit(app.exec_())
版权声明:本文为A13784130845原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。