QCustomPlot Discussion and Comments

Replacing data in realtimeReturn to overview

Hi everyone. I'm currently working on a plot which plots data in real-time. I mean after each 100 ms a new data arrives and I add it to the graph. I want to have a fix window which shows 0-10 seconds. After 10 seconds (I mean for example on the 10001th millisecond) the data of first plotted point should be changed with current arrived data and so on for other points. So I want the output to update the last plot with new data, point by point. I've had some progress so far but I want to know if there is anything implemented for that in QCP to replace a data point with a new one. Or better stated: what can I do to achieve that result?
Any help or suggestion is appreciated.

Hi Ali

graph->addData(lastKey, lastVal);
graph->data()->removeBefore(lastKey-windowSize); // windowSize=10

Hi Brn
Thank you for your quick reply.
The code you wrote will output something like following GIF:
But I want something like this:
(this is only something I tried to show you what I want ;) )
Any idea?

Some modifications with the realtime qcp example seem to do the job, but not sure if it's the best way to do what you want, and in this case, you may lose samples...

void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
    demoName = "Real Time Data Demo";
    customPlot->addGraph(customPlot->xAxis, customPlot->yAxis); // blue line
    customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis); // red line

    customPlot->xAxis->setRange(0, 5);
    customPlot->xAxis2->setRange(5, 10);

    // setup a timer that repeatedly calls MainWindow::realtimeDataSlot:
    connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
    dataTimer.start(0); // Interval 0 means to refresh as fast as possible

void MainWindow::realtimeDataSlot()
    static QTime time(QTime::currentTime());
    // calculate two new data points:
    double keyTime = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds

    double key = std::fmod(keyTime,10);

    static bool plottingBlue = true;

    // add data to lines:
    double val = qSin(keyTime)+qrand()/(double)RAND_MAX*1*qSin(keyTime/0.3843);
    ui->customPlot->graph(0)->addData(key, val);
    ui->customPlot->graph(1)->addData(key, val);

    if(key < 5.0)
            plottingBlue = true;
            plottingBlue = false;

    // make key axis range scroll with the data (at a constant range size of 8):

Nicely done Brn.
Thank you. It is what I wanted.

if (key-lastPointKey > 0.002) // at most add point every 2 ms
    //cannot remove exactly the lastPointKey,instead add a very small value to it
     ui->customPlot->graph(0)->addData(key, y);   // y is the new data

This is my method, hope it can help you!

what mean window size?


May be useful?