什么是负责更新QChart中点的功能或槽?

2024-10-03 19:27:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在现场绘制与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)

它在live中工作了一段时间,应用程序崩溃了。你知道吗

所有的类代码都在这里:

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

Tags: andtheselftimeinitvaluesappendpen