QCustomPlot lag when too many points on the graph
-
Hello. I am working on my project which captures the current and voltage consumption based on a custom hardware that I have designed. A sneak peak of my GUI:
I am not gonna go into too much details about the project itself, but to summarize my issue:
- I am collecting current consumption and voltage samples at variable frequency (It can be (1Hz -> 20Hz) and plotting all points to the graph. The issue with the QCustomPlot is that when I run my application for a long time ( Lets say for 24 hours) , a lot of points accumulate on the graph which makes the graph very unresponsive and laggy when trying to scroll it or move it. After about 48 hours or more, there are so many points on the graph that the application simply crashes when trying to move the graph.
The code I use to setup the graph:
void MainWindow::setup_graph(QCustomPlot *customPlot) { current_upper_limit = 50; current_lower_limit = 0; QColor accentColor; QColor themeColor; QColor themeBackgroundColor; QColor traceColor; accentColor = QColor(Qt::red); traceColor = accentColor; themeColor = QColor(Qt::white); themeBackgroundColor = QColor(53, 57, 53); foreach(QCPAxisRect* rect, ui->customPlot->axisRects()){ foreach(QCPAxis* axis, rect->axes()){ axis->setLabelColor(themeColor); axis->setBasePen(QPen(themeColor, 1)); axis->setTickPen(QPen(themeColor, 1)); axis->setSubTickPen(QPen(themeColor, 1)); axis->setTickLabelColor(themeColor); axis->grid()->setPen(QPen(themeColor, 0.5, Qt::DotLine)); axis->grid()->setSubGridVisible(false); axis->setSelectedTickLabelColor(accentColor); axis->setSelectedLabelColor(accentColor); axis->setSelectedBasePen(QPen(accentColor, 1)); axis->setSelectedSubTickPen(QPen(accentColor, 1)); axis->setSelectedTickPen(QPen(accentColor, 1)); } } ui->customPlot->setBackground(QBrush(themeBackgroundColor)); //SETUP GRAPH 0 customPlot->addGraph(); customPlot->graph(0)->setScatterStyle(QCPScatterStyle::ssCircle); QPen pen_current; pen_current.setWidth(2); pen_current.setColor(QColor(80, 200, 120)); customPlot->graph(0)->setPen(pen_current); //SETUP GRAPH 1 customPlot->addGraph(); customPlot->graph(1)->setScatterStyle(QCPScatterStyle::ssCross); QPen pen_voltage; pen_voltage.setWidth(2); pen_voltage.setColor(QColor(0, 150, 255)); customPlot->graph(1)->setPen(pen_voltage); customPlot->graph()->setLineStyle(QCPGraph::lsLine); customPlot->xAxis->setLabel("Time(s)"); customPlot->yAxis->setLabel("Current/Voltage(mA/V)"); customPlot->xAxis->setRange(0,30); customPlot->yAxis->setRange(current_lower_limit,current_upper_limit); customPlot->legend->setVisible(true); customPlot->graph(0)->setName("Current"); customPlot->graph(1)->setName("Voltage"); ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables | QCP::iMultiSelect); //QCustomPlot::setSelectionRectMode //ui->customPlot->setSelectionRectMode(QCP::srmSelect); ui->customPlot->graph(0)->setSelectable(QCP::stDataRange); ui->customPlot->graph(1)->setSelectable(QCP::stDataRange); foreach(QCPAxisRect *rect, ui->customPlot->axisRects()){ rect->setRangeDrag(Qt::Horizontal); rect->setRangeZoom(Qt::Horizontal); } }
And then I simply append to the graph every sampling period:
void MainWindow::addPoint(double x, double y) { qv_x.append(x); qv_y.append(y); } void MainWindow::addPoint_voltage(double x, double y) { qv_x_voltage.append(x); qv_y_voltage.append(y); } void MainWindow::plot(QCustomPlot *customPlot) { customPlot->graph(0)->setData(qv_x,qv_y); customPlot->replot(); customPlot->update(); } void MainWindow::plot_voltage(QCustomPlot *customPlot) { customPlot->graph(1)->setData(qv_x_voltage,qv_y_voltage); customPlot->replot(); customPlot->update(); }
My question:
Is there any way to optimize this? How can I make it less laggy when there are a lot of points on the graph?
It is probabaly natural that the graph (or the application) gets laggy due to many samples but perhaps there are specific techniques to be used that help overcome this?
Appreciate any advice! :)
-
Hi,
You should contact the QCustomPlot folks for that kind of question. However something that is common: there's no point in trying to show a huge amount of points. Filter them, throw them out, use a moving window, but in any case don't try to show them all.