jeudi 26 novembre 2020

Best data structure to create buffer for plotting [closed]

I have an external app that outputs to stdout in real-time, and I'm trying to create a UI for that app with QT and qcustomplot.

I'm doing real-time plotting however I noticed that adding each point does not work well enough, so I found that a solution would be to use a buffer and replot n points at once according to this link:

https://www.qcustomplot.com/index.php/support/forum/704

My current code looks like this:

void    Monitor::readyReadStandardOutput(){

    QProcess *p = (QProcess *)sender();
    // get the data
    QByteArray buf = p->readAllStandardOutput();
    QList<QByteArray>  buf_res =   ((buf.split(','))[1]).split('\n');

    double d= (buf_res[0]).toDouble() ;
    this->win->queue.enqueue(d); // here we're adding the new point to a queue in our window

}

Then in my mainwindow

void MainWindow::realtimeDataSlot2(){
    if (queue.size() == 0 ) return;
    double val = queue.dequeue() ;
    double key = time.elapsed()/1000.0; 
    static double lastPointKey = 0;
    if (key-lastPointKey > 0.02) // at most add point every 2 ms
    {
     ui->customPlot->graph(0)->addData(key, val );
     lastPointKey = key;
    }
    ui->customPlot->xAxis->setRange(key, 8, Qt::AlignRight);
    ui->customPlot->replot();

}

addData's arguments can be replaced with a vector, so I'm looking for something that looks like this.

  1. Change the queue to a "buffer" (but which QT/C++ type exactly)?

  2. In the reading function we need to replace the enqueue of 1 element to pushing multiple elements at once to some buffer

  3. In the plotting function extracting the first 1000 points from the buffer into a QVector<double>.

I'm a bit confused on how to achieve this and what types to use exactly that allow adding multiple elements and extracting multiple elements easily and efficiently.

Aucun commentaire:

Enregistrer un commentaire