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
graph->rescaleAxes();
plot->replot();

Hi Brn
Thank you for your quick reply.
The code you wrote will output something like following GIF:
https://qt-api.ir/videos/gif/plot.gif
But I want something like this:
https://qt-api.ir/videos/gif/plot%202.gif
(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->graph(0)->setPen(QPen(Qt::blue));
    customPlot->addGraph(customPlot->xAxis2, customPlot->yAxis); // red line
    customPlot->graph(1)->setPen(QPen(Qt::red));

    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)
    {
        if(!plottingBlue)
        {
            plottingBlue = true;
            ui->customPlot->graph(0)->data()->clear();
        }
        ui->customPlot->graph(1)->data()->removeBefore(5+key);
    }
    else
    {
        if(plottingBlue)
        {
            plottingBlue = false;
            ui->customPlot->graph(1)->data()->clear();
        }
        ui->customPlot->graph(0)->data()->removeBefore(key-5);
    }

    // make key axis range scroll with the data (at a constant range size of 8):
    ui->customPlot->yAxis->rescale();
    ui->customPlot->replot();
}

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