import os
import time
import sys
FileName = os.path.basename(sys.argv[0])
FilePath = sys.argv[0].replace(FileName,"")
UiName = FileName.replace(".py",".ui")
UiPath = FilePath +UiName
Ui_pyName = FilePath+"ui.py"
FileFlag = os.path.isfile(Ui_pyName)
if FileFlag == 0:
sys_cmd = os.popen("pyuic5"+" -o "+Ui_pyName+" "+UiPath)
time.sleep(1)
from ui import Ui_Form
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtChart import *
import random
DataVector = []
mutex = QMutex()
class ThreadCtl(QThread):
def __init__(self):
super().__init__()
self.threadstatus = 1
def run(self):
while self.threadstatus == 1:
self.randdata_h = random.uniform(1, 6)
self.randdata_h = round(self.randdata_h, 2)
# self.randdata_w = random.uniform(1, 6)
# self.randdata_w = round(self.randdata_w, 2)
# self.points = [self.post,self.randdata_h,]
time.sleep(1)
mutex.lock()
DataVector.append(self.randdata_h)
mutex.unlock()
print("DataVector:",DataVector)
class m_window(QWidget,Ui_Form):
def __init__(self):
super(m_window,self).__init__()
self.setupUi(self)
self.threadCtl = ThreadCtl()
self.threadCtl.start()
self.timer = QTimer()
self.timer.timeout.connect(self.update)
self.timer.start(2000)
self.ctl = 0
self.series_1 = QLineSeries() #定义LineSerise,将类QLineSeries实例化
self._1_point_0 = QPointF(0.00,0.00) #定义折线坐标点
self._1_point_1 = QPointF(0.80,6.00)
self._1_point_2 = QPointF(2.00,2.00)
self._1_point_3 = QPointF(4.00,3.00)
self._1_point_4 = QPointF(1.00,3.00)
self._1_point_5 = QPointF(5.00,3.00)
self._1_point_list = [self._1_point_0,self._1_point_1,self._1_point_4,self._1_point_2,self._1_point_3,self._1_point_5] #定义折线清单
self.series_1.append(self._1_point_list) #折线添加坐标点清单
self.series_1.setName("折线一")
self.series_2 = QLineSeries() #定义LineSerise
self._2_point_0 = QPointF(0.00,0.00) #定义折线坐标点
self._2_point_1 = QPointF(0.50,5.00)
self._2_point_2 = QPointF(2.00,1.00)
self._2_point_3 = QPointF(4.00,2.00)
self._2_point_4 = QPointF(1.00,2.00)
self._2_point_5 = QPointF(5.00,2.25)
self._2_point_list = [self._2_point_0,self._2_point_1,self._2_point_4,self._2_point_2,self._2_point_3,self._2_point_5] #定义折线清单
self.series_2.append(self._2_point_list) #折线添加坐标点清单
self.series_2.setName("折线二")
self.series_3 = QLineSeries() #定义LineSerise
# self.series_3.append(2.22,3.45)
# self.series_3.append(3.45,5.23)
self.x_Aix = QValueAxis()
self.x_Aix.setRange(0.00,5.00)
self.x_Aix.setLabelFormat("%0.2f")
self.x_Aix.setTickCount(6)
self.x_Aix.setMinorTickCount(0)
self.y_Aix = QValueAxis()
self.y_Aix.setRange(0.00,6.00)
self.y_Aix.setLabelFormat("%0.2f")
self.y_Aix.setTickCount(7)
self.y_Aix.setMinorTickCount(0)
self.charView = QChartView(self) #定义charView,父窗体类型为 Window
self.setLayout(self.gridLayout)
self.gridLayout.addWidget(self.charView)
self.charView.setGeometry(0,0,self.width(),self.height()) #设置charView位置、大小
self.charView.chart().addSeries(self.series_1) #添加折线
self.charView.chart().addSeries(self.series_2) #添加折线
# self.charView.chart().setAxisX(self.x_Aix) #设置x轴属性
# self.charView.chart().setAxisY(self.y_Aix) #设置y轴属性
self.charView.chart().createDefaultAxes() #使用默认坐标系
self.charView.chart().axisX().setRange(0.00,5.00)
self.charView.chart().axisX().setTickCount(6)
self.charView.chart().axisX().setMinorTickCount(2)
self.charView.chart().axisX().setLabelFormat("%0.2f")
self.charView.chart().axisY().setRange(0.00,6.00)
self.charView.chart().axisY().setTickCount(7)
self.charView.chart().axisY().setMinorTickCount(2)
self.charView.chart().setTitleBrush(QBrush(Qt.cyan)) #设置标题笔刷
self.charView.chart().setTitle("双折线") #设置标题
self.charView.show()#显示charView
def update(self):
if len(DataVector) > 0 :
mutex.lock()
if len(DataVector) > 10 :
print("------")
while len(DataVector) > 1 :
self.value = DataVector[0]
DataVector.pop(0)
del self._1_point_list[len(self._1_point_list)-1]
self._1_point_list.insert(0,QPointF(0,self.value))
for i in range(0,len(self._1_point_list)):
self._1_point_list[i].setX(i)
self.series_1.replace(self._1_point_list)
self.value = DataVector[0]
DataVector.pop(0)
mutex.unlock()
del self._1_point_list[len(self._1_point_list)-1]
self._1_point_list.insert(0,QPointF(0,self.value))
for i in range(0,len(self._1_point_list)):
self._1_point_list[i].setX(i)
self.series_1.replace(self._1_point_list)
app = QApplication(sys.argv)
window = m_window();
window.show()
sys.exit(app.exec_())