QCustomPlot Discussion and Comments

How to restart Line graph (Realtime Data Plot)Return to overview

Greetings,

I have successfully implemented QCustomPlot for real-time data plot, but my concern is how can I restart the graph to build from the initial point without closing the application?
In my app I need to redraw from the beginning, but yet I am not able to make it without closing application.

Looking forward.

Try this:

void Widget::clearGraph()
{
    if (ui->customPlot->graphCount() > 0)
        ui->customPlot->graph(0)->setData(QVector<double>(), QVector<double>());
    else
        qDebug() << Q_FUNC_INFO << "ui->customPlot->graphCount() == 0";

    key = 0.0; // last point on x axis
    ui->customPlot->yAxis->setRange(0.0, 1.0);
    ui->customPlot->xAxis->setRange(-20, -20, Qt::AlignRight); // you can change it for wishful range
    ui->customPlot->replot(QCustomPlot::rpQueuedRefresh);
}

Unfortunately, that hasn't worked for me. The code that I am running now is given below. Please suggest the mandatory changes, so that it can start plotting from the beginning whenever the user enters this screen.

void dynamic_measurement::realtimeDataSlot()
{
//    position_track();
    static QTime time=QTime::currentTime();

    // calculate two new data points:
    key = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds

    static double lastPointKey = 0;
    if (key-lastPointKey > 0.002) // at most add point every 2 ms
    {
      double x=average_A;
      double y=average_B;

      QString s = QString::number(x);
      QString t = QString::number(y);
      ui->custom_plot->graph(0)->addData(key, x);
      ui->custom_plot->graph(1)->addData(key, y);

      ui->custom_plot->graph(0)->setName("LEFT :"+s);
      ui->custom_plot->graph(1)->setName("RIGHT:"+t);

      lastPointKey = key;
    }

    ui->custom_plot->replot();

It will work:

void dynamic_measurement::realtimeDataSlot()
{
//    position_track();
    static QTime time=QTime::currentTime();
    static double keyRightBorder = 10.0; // 10 sec
 
    // calculate two new data points:
    key = time.elapsed()/1000.0; // time elapsed since start of demo, in seconds
 
    static double lastPointKey = 0;
    if (key-lastPointKey > 0.002) // at most add point every 2 ms
    {
      double x=average_A;
      double y=average_B;
 
      QString s = QString::number(x);
      QString t = QString::number(y);
      ui->custom_plot->graph(0)->addData(key, x);
      ui->custom_plot->graph(1)->addData(key, y);
 
      ui->custom_plot->graph(0)->setName("LEFT :"+s);
      ui->custom_plot->graph(1)->setName("RIGHT:"+t);
 
      lastPointKey = key;
    }
    if (key >= keyRightBorder){
        time.restart();
        ui->custom_plot->graph(0)->setData(QVector<double>(), QVector<double>());
        ui->custom_plot->graph(1)->setData(QVector<double>(), QVector<double>());
        key = 0;
        lastPointKey = -1; // you can change to lastPointKey = 0;
    }
    ui->custom_plot->replot();
}