我需要使用 Qchart 实时绘图,但它没有使用下面的代码进行更新。
所以我正在搜索负责情节更新的功能或插槽,所以我将自己激活它。
def __init__(self, params):
super().__init__()
#init with params
self.mycurv.append(QLineSeries(self))
self.addSeries(self.allCruvs[-1])
def plotData(self, Time, Values, color=None, curvIndex=0):
self.mycurv.clear()
lastTime = self.myZero
for i, theTime in enumerate(Time):
if (theTime > lastTime + MININTERVALTIME):
self.mycurv.append(lastTime - self.myZero, Values[i])
self.mycurv.append(theTime - self.myZero, Values[i])
lastTime = theTime
self.update()
如果在最后而不是self.update
,我把:
self.removeAllseries()
self.addseries(self.mycurv)
它正在运行一段时间,应用程序崩溃。
所有课程代码都在这里:
from PyQt5 import QtGui
from PyQt5.QtGui import Qt
from PyQt5.QtChart import QChart, QChartView, QLineSeries, QValueAxis
import datetime
MININTERVALTIME = 5
PENSIZE = 2.5
class chartClass(QChart):
def __init__(self, params):
super().__init__()
#init with params
self.allCruvs = []
self.allCruvs.append(QLineSeries(self))
self.addSeries(self.allCruvs[-1])
def plotData(self, Time, Values, color=None, curvIndex=0):
#test new curv or clear the old one
if (curvIndex < len(self.allCruvs)):
self.allCruvs[curvIndex].clear()
else:
curvIndex = len(self.allCruvs)
self.allCruvs.append(QLineSeries(self))
self.addSeries(self.allCruvs[-1])
# Get max and min for time axe and title
self.myZero = min(Time)
self.myMax = max(Time)
# define title
dateFormat = '%m/%d %H:%M'
myZeroSTR = datetime.datetime.fromtimestamp(int(self.myZero)).strftime(dateFormat)
myMaxSTR = datetime.datetime.fromtimestamp(int(self.myMax)).strftime(dateFormat)
self.allCruvs[-1].setName(self.type + " [ " + myZeroSTR + " - " + myMaxSTR + " ]")
# define number of ticks // still in prehistoric way to do automatic new one
maxDelta = round(max(Time) - self.myZero, 0)
if (maxDelta < 20):
self.ticks = maxDelta + 1
elif maxDelta > 20:
self.ticks = int(maxDelta / 10) + 2
elif maxDelta > 200:
self.ticks = int(maxDelta / 100) + 2
elif maxDelta > 2000:
self.ticks = int(maxDelta / 1000) + 2
elif maxDelta > 20000:
self.ticks = int(maxDelta / 10000) + 2
# Set axis ticks and min/max
self.xAxe.setTickCount(self.ticks)
self.xAxe.setRange(0, maxDelta)
self.yAxe.setRange(min(Values), max(Values))
# set Pen and brush // need to plot line and the points with it
pen = QtGui.QPen()
brush = QtGui.QBrush()
pen.setWidthF(PENSIZE)
# pen.setWidthF(3)
pen.setCapStyle(Qt.RoundCap)
pen.setJoinStyle(Qt.RoundJoin)
# pen.setStyle(QtCore.Qt.NoPen)
# pen.setStyle(QtCore.Qt.SolidLine)
myColor = color
if color is None:
self.grColor += 1
self.grColor %= len(self.myPalette)
myColor = self.colorByIndex()
pen.setColor(myColor)
brush.setColor(myColor)
self.allCruvs[-1].setPointsVisible(True)
self.allCruvs[-1].setPen(pen)
pen.setBrush(brush)
self.allCruvs[-1].setBrush(brush)
lastTime = self.myZero
for i, theTime in enumerate(Time):
if (theTime > lastTime + MININTERVALTIME):
# Best thing to do is to plot an empty field, but couldn't do it so I plot an horizontal line for now
self.allCruvs[-1].append(lastTime - self.myZero, Values[i])
self.allCruvs[-1].append(theTime - self.myZero, Values[i])
lastTime = theTime
# when I add this part it plots the line but without points
try:
self.addSeries(self.allCruvs[-1])
# when I comment the part before, it does not plot the curv at all
#try:
# self.addSeries(self.allCruvs[-1])
# With this test I know that curvs exists and are in visible mode but nothing is shown on the screen
for curv in self.series():
print("self.allCruvs[] : ", len(curv), "is visible : ", curv.isVisible())
# curv.show() # do nothing new I can see
self.update() # do nothing new I can see