QtCustomPlot Realtime Data Demo
-
Hi,
I'm new to this Qt and I am trying to plot a real time data graph with random data via QtCustomPlot. The graph that I generated was unable to show the real time random data. May I know what is the problem of my code?
Below is the source code:void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
customPlot->addGraph(); // blue line
customPlot->graph(0)->setPen(QPen(Qt::blue));
customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
customPlot->graph(0)->setAntialiasedFill(false);customPlot->addGraph(); // blue dot
customPlot->graph(1)->setPen(QPen(Qt::blue));
customPlot->graph(1)->setLineStyle(QCPGraph::lsNone);
customPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
customPlot->xAxis->setTicker(timeTicker);
customPlot->axisRect()->setupFullAxesBox();
customPlot->yAxis->setRange(-40, 40);connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
dataTimer.start(0);
}void MainWindow::realtimeDataSlot()
{std::uniform_real_distribution<double>distribution(1,20.5); double value1 = distribution(*QRandomGenerator::global()); qDebug() << value1; while(value1 !=0) { value1=distribution(*QRandomGenerator::global()); qDebug() << value1; } static QTime time(QTime::currentTime()); double key = time.elapsed()/1000.0; static double lastPointKey = 0; if (key-lastPointKey > 0.002) // at most add point every 2 ms { ui->customPlot->graph(0)->addData(key, value1); ui->customPlot->graph(1)->addData(key, value1); lastPointKey = key; } ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight); ui->customPlot->replot();}
-
Hi,
I'm new to this Qt and I am trying to plot a real time data graph with random data via QtCustomPlot. The graph that I generated was unable to show the real time random data. May I know what is the problem of my code?
Below is the source code:void MainWindow::setupRealtimeDataDemo(QCustomPlot *customPlot)
{
customPlot->addGraph(); // blue line
customPlot->graph(0)->setPen(QPen(Qt::blue));
customPlot->graph(0)->setBrush(QBrush(QColor(240, 255, 200)));
customPlot->graph(0)->setAntialiasedFill(false);customPlot->addGraph(); // blue dot
customPlot->graph(1)->setPen(QPen(Qt::blue));
customPlot->graph(1)->setLineStyle(QCPGraph::lsNone);
customPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssDisc);QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
customPlot->xAxis->setTicker(timeTicker);
customPlot->axisRect()->setupFullAxesBox();
customPlot->yAxis->setRange(-40, 40);connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));connect(&dataTimer, SIGNAL(timeout()), this, SLOT(realtimeDataSlot()));
dataTimer.start(0);
}void MainWindow::realtimeDataSlot()
{std::uniform_real_distribution<double>distribution(1,20.5); double value1 = distribution(*QRandomGenerator::global()); qDebug() << value1; while(value1 !=0) { value1=distribution(*QRandomGenerator::global()); qDebug() << value1; } static QTime time(QTime::currentTime()); double key = time.elapsed()/1000.0; static double lastPointKey = 0; if (key-lastPointKey > 0.002) // at most add point every 2 ms { ui->customPlot->graph(0)->addData(key, value1); ui->customPlot->graph(1)->addData(key, value1); lastPointKey = key; } ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight); ui->customPlot->replot();}
@Jayden123 said in QtCustomPlot Realtime Data Demo:
The graph that I generated was unable to show the real time random data.
What exactly happens? It plots nothing? It plots the wrong things? It plots the right things, but not all of them? It works for a while and then goes wrong? The re-range-scaling does not work right? Or what?
Although it may not be your problem, I would recommend you get rid of your
staticvariables and make them members ofMainWindow, and initialize them as appropriate. -
@Jayden123 said in QtCustomPlot Realtime Data Demo:
The graph that I generated was unable to show the real time random data.
What exactly happens? It plots nothing? It plots the wrong things? It plots the right things, but not all of them? It works for a while and then goes wrong? The re-range-scaling does not work right? Or what?
Although it may not be your problem, I would recommend you get rid of your
staticvariables and make them members ofMainWindow, and initialize them as appropriate.