Nominate our 2022 Qt Champions!

time at x axis in a real time data plot

  • Hey

    I'm trying to plot real time data in a graph.
    So I copied the code from the example:

    static double lastPointKey = 0;
    if (key-lastPointKey > 0.01) // at most add point every 10 ms
      double value0 = qSin(key); //qSin(key*1.6+qCos(key*1.7)*2)*10 + qSin(key*1.2+0.56)*20 + 26;
      double value1 = qCos(key); //qSin(key*1.3+qCos(key*1.2)*1.2)*7 + qSin(key*0.9+0.26)*24 + 26;
      // add data to lines:
      ui->customPlot->graph(0)->addData(key, value0);
      ui->customPlot->graph(1)->addData(key, value1);
      // set data of dots:
      ui->customPlot->graph(2)->addData(key, value0);
      ui->customPlot->graph(3)->addData(key, value1);
      // remove data of lines that's outside visible range:
      // rescale value (vertical) axis to fit the current data:
      lastPointKey = key;
    // make key axis range scroll with the data (at a constant range size of 8):
    ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);

    Now I have the current time at the x axis.
    I want to start measuring by 0 seconds and then go further every second.

    So I defined two variables

    double starttime = QDataTime::currentDateTime().toTime_t(); // at the beginning
    double now=QDataTime::currentDateTime().toTime_t(); // time, every loop
    //also in the addData function?

    But now I get something like that:

    1:00:00 1:00:01 1:00:02 1:00:03 1:00:04

    and the data between every second is interpolated linear, so that it looks very ugly and react slowly.
    Is there a way to fix it?

Log in to reply