I ran into a problem that gaps in graphs disappears at some point of zooming out.
Here is simple example that reproduces this undesirable behavior:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { QCustomPlot *main_plot = new QCustomPlot(this); main_plot->setMinimumSize(800, 600); main_plot->setInteractions(QCP::iRangeZoom | QCP::iRangeDrag); main_plot->axisRect()->setRangeDrag(Qt::Horizontal | Qt::Vertical); main_plot->axisRect()->setRangeZoom(Qt::Horizontal); QHBoxLayout *h_layout = new QHBoxLayout; h_layout->addWidget(main_plot); QWidget *main_widget = new QWidget(this); main_widget->setLayout(h_layout); setCentralWidget(main_widget); double x1 = 1000.0; double x2 = 1010.0; double xNaN = 1012.5; double x3 = 1015.0; double x4 = 1025; QVector<double> x = {x1, x2, xNaN, x3, x4}; double y1 = 0; double y2 = 0; double yNaN = qQNaN(); double y3 = 10; double y4 = 10; QVector<double> y = {y1, y2, yNaN, y3, y4}; double range_size = x.last() - x.first(); QCPRange x_range(x.first() - range_size * 0.2, x.last() + range_size * 0.2); QCPRange y_range(-5, 15); main_plot->xAxis->setRange(x_range); main_plot->yAxis->setRange(y_range); main_plot->addGraph(); QPen pen; pen.setColor(Qt::blue); pen.setWidth(5); main_plot->graph(0)->setPen(pen); main_plot->graph(0)->setData(x, y, true); main_plot->replot(); }
In the beginning main_plot
draws lines with gap as it should be. But when xAxis range gets more than approximately 10000, it starts drawing line between points which is certainly not what I expect to see.
I've worked out that the reason is the AdaptiveSampling used in QCPGraph::getOptimizedLineData
. It seems that this algorithm gets rid of NaN values at some point while zooming out so lineData
contains only non-NaN values.
The simplest solution I can see is to set AdaptiveSampling disabled, but I'm not sure that it's a good idea as this algorithm seems to improve performance.
So what is the most proper solution of this issue?