0

I need to plot in live with Qchart, but it didn't update with the code below.

So I am searching the function or the slot which is responsable of plot update, so I will activate it by myself.

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()

if at the end in stead of self.update, I put :

self.removeAllseries()
self.addseries(self.mycurv)

it is working in live for a while, and the the application crashes.

All the class code is here :

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
gxmad
  • 1,650
  • 4
  • 23
  • 29
  • Where do you call the plotData function? – eyllanesc Oct 16 '17 at 13:17
  • @eyllanesc that is the problem with QChart, plot is called with addSeries(), removeAllSeries() , and normally with update but update does not work. – gxmad Oct 16 '17 at 15:50
  • If you want me to help you, I need you to share the complete code, you can do it through github, drive, dropbox, etc. – eyllanesc Oct 16 '17 at 16:01
  • @eyllanesc I put Class code, is that enough? – gxmad Oct 16 '17 at 16:32
  • I need a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve), that is, that code must be reproducible and the code it shows is not. – eyllanesc Oct 16 '17 at 16:35
  • @eyllanesc, it is just a simplified version with only one curve, because my app should work with more than that. the class, the complete class in my app. – gxmad Oct 16 '17 at 16:53
  • From my experience in helping to solve problems many times I have been shown how you show that it does not reproduce the error directly but I have to patch it and it works for me, I show my solution and it does not work for the one who had the problem because I did not show it the complete code. I do not want to go through the same disgust and so I always ask for a reproducible code, ie I copy the code and paste it to my editor and execute it, and analyze the problem. – eyllanesc Oct 16 '17 at 16:56
  • I do not need the complete code of your project but a code that reproduces the error, ie you just have to copy and run. I'm not asking you too much. – eyllanesc Oct 16 '17 at 16:58

0 Answers0