Hello all,
I'm new at QCustomPlot and in thist forum but currently I'm a bit lost and I hopen, some of you can help.
I spent many hours to find a solution on my problem but I failed. Yes of course, I found some examples and solutions but no single solution is working properly.
The basis for my first implementation with QCustomPlot was the example https://www.qcustomplot.com/index.php/demos/realtimedatademo
I want to monitor min- and max Temperatures and whenevery I receive a set of data, I add the data to the graph.
Currently it is simulated in the code by generating a random value between -5 and +5 with a timer every second.
ui->customPlot2->graph(0)->addData(now, y1 ); ui->customPlot2->graph(1)->addData(now, y2 );
x-Axis is a QCPAxisTickerDateTime Axis from now (on the right) to 60 seconds in the past (on the left)
Measurements are scrolling from right to left.
Old and unintended data is deleted by checking the size of data and deleting allways the first key-value-pair
if ( ui->customPlot2->graph(0)->data()->size() > 60 ) { ui->customPlot2->graph(0)->data()->remove(ui->customPlot2->graph(0)->data()->begin()->key); }
What I would like to do now is, to draw also a dotted minimum- and maximum-line in the graph.
Now, my first approach was, to check the data from begin() to end() and identify min and max value.
And thats the problem :-( Honestly, I don't know how. I had a look for some existing function but I failed...
I tried to iterate in a for loop or while through the data structure but I also failed. I tried some examples found in the web and also some hints out of this support forum but I also failed. Maybe my problem is also cause by using the latest greates version of the QCustomPlot Class. E.g. QCPData, referenced a couple of times in examples is not there, etc.
I would be very very happy if somebody of you can give me a hint how to solve my problem.
Thank you so much beforehand!
PS: If needed, here is the complete code I'm testing and learning with ....
#include "mainwindow.h" #include "./ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // ===== CustomPlot 2 ===== // seconds of current time, we'll use it as starting point in time for data: QDateTime time_now; time_now = QDateTime::currentDateTime(); qint64 now = time_now.toSecsSinceEpoch(); ui->customPlot2->addGraph(); // blue line ui->customPlot2->graph(0)->setPen(QPen(QColor(40, 110, 255))); ui->customPlot2->addGraph(); // red line ui->customPlot2->graph(1)->setPen(QPen(QColor(255, 110, 40))); // configure bottom axis to show date instead of number: QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime); dateTicker->setDateTimeFormat("hh:mm:ss"); ui->customPlot2->xAxis->setTicker(dateTicker); ui->customPlot2->xAxis->setRange( now-60, now); ui->customPlot2->axisRect()->setupFullAxesBox(); ui->customPlot2->yAxis->setRange(-5, 5); // make left and bottom axes transfer their ranges to right and top axes: connect(ui->customPlot2->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot2->xAxis2, SLOT(setRange(QCPRange))); connect(ui->customPlot2->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot2->yAxis2, SLOT(setRange(QCPRange))); // ===== Timer ===== // Starte den Timer, um alle Sekunde eine Zufallszahl zu generieren timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(addDataPoint())); timer->start(1000); } MainWindow::~MainWindow() { timer->stop(); delete timer; myBars->deleteLater(); delete ui; } void MainWindow::addDataPoint() { // seconds of current time, we'll use it as starting point in time for data: QDateTime time_now; time_now = QDateTime::currentDateTime(); qint64 now = time_now.toSecsSinceEpoch(); float y1, y2; // Generiere eine Zufallszahl zwischen 0 und 100 y1 = (((float)(QRandomGenerator::global()->generate() % 100)) - 50) / 10; y2 = (((float)(QRandomGenerator::global()->generate() % 100)) - 50) / 10; qDebug() << "Entering addDataPoint : adding " << y1 << y2; ui->customPlot2->graph(0)->addData(now, y1 ); ui->customPlot2->graph(1)->addData(now, y2 ); if ( ui->customPlot2->graph(0)->data()->size() > 60 ) ui->customPlot2->graph(0)->data()->remove(ui->customPlot2->graph(0)->data()->begin()->key); if ( ui->customPlot2->graph(1)->data()->size() > 60 ) ui->customPlot2->graph(1)->data()->remove(ui->customPlot2->graph(1)->data()->begin()->key); ui->statusBar->showMessage( QString("Data points 1: %1 Data points 1: %2") .arg(ui->customPlot2->graph(0)->data()->size()) .arg(ui->customPlot2->graph(1)->data()->size()) ); ui->customPlot2->xAxis->setRange( now-60, now); ui->customPlot2->replot(); }