QCustomPlot Discussion and Comments

Step Line graph is skipping the first point.Return to overview

Hello,

While using the qcustomplot to generate a simple step-like graph, I noticed something strange in the chart.
Here is how I initialize my graph:

 ui->time_plot->xAxis->setRange(0, 6, Qt::AlignLeft);
    ui->time_plot->yAxis->setRange(0, 10, Qt::AlignCenter);
    ui->time_plot->addGraph();
    numberOfGraphs+=1;
    ui->time_plot->graph()->setPen(QPen(Qt::blue));
    //Give the axes some labels:
    ui->time_plot->graph(0)->setLineStyle(QCPGraph::lsStepRight);
    ui->time_plot->graph(0)->setName("Graph1");
    ui->time_plot->graph(0)->setPen(QPen(QColor(0, 0, 255), 2));
    ui->time_plot->xAxis->setLabel("Sample #");
    ui->time_plot->yAxis->setLabel("Count");
    ui->time_plot->xAxis->setLabelFont(ui->label->font());
    ui->time_plot->yAxis->setLabelFont(ui->label->font());
    ui->time_plot->xAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
    ui->time_plot->yAxis->grid()->setPen(QPen(QColor(140, 140, 140), 1, Qt::DotLine));
    ui->time_plot->xAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
    ui->time_plot->yAxis->grid()->setSubGridPen(QPen(QColor(80, 80, 80), 1, Qt::DotLine));
    ui->time_plot->xAxis->grid()->setSubGridVisible(true);
    ui->time_plot->yAxis->grid()->setSubGridVisible(true);
    ui->time_plot->xAxis->grid()->setZeroLinePen(Qt::NoPen);
    ui->time_plot->yAxis->grid()->setZeroLinePen(Qt::NoPen);
    ui->time_plot->graph(0)->layer()->setMode(QCPLayer::lmBuffered);
    ui->time_plot->setNoAntialiasingOnDrag(true);
    ui->time_plot->setNotAntialiasedElements(QCP::aeAll);
    ui->time_plot->axisRect()->setRangeZoom(Qt::Horizontal);
    ui->time_plot->axisRect()->setRangeDrag(Qt::Horizontal);
    connect(ui->time_plot->xAxis,    SIGNAL(rangeChanged(QCPRange)), this,   SLOT(onXRangeChanged(QCPRange)));
    ui->time_plot->xAxis->setUpperEnding(QCPLineEnding::esSpikeArrow);

Here is how I generate simple data and replot the graph:

  ui->time_plot->graph(0)->data().data()->clear();
QVector<double> x2(10), y2(10);
    for (int i=0; i<10; ++i)
    {
        x2[i] = i;
        y2[i] = 700 + (i*1000);
        qDebug() << "x[" << i << "] = " << x2[i] << " y[" << i << "] = " << y2[i];
    }
    ui->time_plot->graph(0)->setData(x2, y2);
    ui->time_plot->xAxis->setRange(0, 10, Qt::AlignLeading);
    ui->time_plot->yAxis->rescale();
    ui->time_plot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
    ui->time_plot->replot();
    ui->time_plot->update();

Here is the qDebug log (my data points):
x[ 0 ] = 0 y[ 0 ] = 700
x[ 1 ] = 1 y[ 1 ] = 1700
x[ 2 ] = 2 y[ 2 ] = 2700
x[ 3 ] = 3 y[ 3 ] = 3700
x[ 4 ] = 4 y[ 4 ] = 4700
x[ 5 ] = 5 y[ 5 ] = 5700
x[ 6 ] = 6 y[ 6 ] = 6700
x[ 7 ] = 7 y[ 7 ] = 7700
x[ 8 ] = 8 y[ 8 ] = 8700
x[ 9 ] = 9 y[ 9 ] = 9700

The graph seems like it starts with y2[1], which is 1700. Even though it shows the X-Axises with sample #0, it shows the value of 1700, which is y[1]. Why is this behavior happening when graph sample # 0 (x[0]) does not show up as a y[0] value of 700 in the graph?


Thank you,
Matt

I fixed the problem by changing the

   ui->time_plot->graph(0)->setLineStyle(QCPGraph::lsStepRight);

To
   ui->time_plot->graph(0)->setLineStyle(QCPGraph::lsStepLeft);