remi
remi

Reputation: 1047

QCustomPlot add scatter points on top of existing plot

After creating a plot using qcustomplot, how can I, based on existing X values, retrieve the Y values, and then plot points at these locations?

My attempt is below:

Create plot:

This function creates a plot, adds some data (dates) on the x-axis and some values on the y axis:

void Qt_PlotTest::createPlot(){    
   QCustomPlot* customPlot = ui.widget;
   customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));

   //Set some data:
   double now = QDateTime::currentDateTime().toTime_t();
   QVector<double> yData, xData;
   for (int i = 0; i < 100; i++){
      xData.push_back(now + i*24.0 * 3600.0);
      yData.push_back(pow(double(i), 2) + 550.0*sin(double(i)/4.0));
   }

   // create graph and assign data to it:
   customPlot->addGraph();
   customPlot->graph(0)->setData(xData, yData);

   //Fix axes:
   //Set date axis:
   QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
   dateTicker->setDateTimeFormat("d. MMMM\nyyyy");
   customPlot->xAxis->setTicker(dateTicker);   
   customPlot->xAxis->setLabel("Time");
   customPlot->yAxis->setLabel("Value");   
   customPlot->rescaleAxes();
   customPlot->replot();

   //Set interations:
   customPlot->setInteraction(QCP::iRangeDrag, true);
   customPlot->setInteraction(QCP::iRangeZoom, true);           
}

Which results in:

Plot of random data vs dates

Set scatter points:

This functions should plot a few scatter points at correct x/y locations based on x values. (I need to read the corresponding Y value from the existing plot).

void Qt_PlotTest::setScatterPoints(){

   QCustomPlot* customPlot = ui.widget;

   //The locations where scatter points should be plotted:
   QDate qd1(2018, 03, 26), qd2(2018, 03, 30), qd3(2018, 05, 11), qd4(2018, 06, 15);
   QVector<double> dates = { (double)QDateTime(qd1).toTime_t(), (double)QDateTime(qd2).toTime_t(), (double)QDateTime(qd3).toTime_t(), (double)QDateTime(qd4).toTime_t() };

   //Use tracer to find data at these dates:
   QVector<double> values;
   QCPItemTracer *tracer = new QCPItemTracer(customPlot);
   tracer->setGraph(customPlot->graph(0));
   tracer->setInterpolating(true);
   for (int i = 0; i < 4; i++){
      tracer->setGraphKey(dates[i]);
      values.push_back(tracer->position->coords().y());
   }

   //Plot points at these locations:
   QCPGraph* dwPoints = new QCPGraph(customPlot->xAxis, customPlot->yAxis);
   dwPoints->setAdaptiveSampling(false);
   dwPoints->setLineStyle(QCPGraph::lsNone);
   dwPoints->setScatterStyle(QCPScatterStyle::ssCircle);
   dwPoints->setPen(QPen(QBrush(Qt::red), 2));

   dwPoints->addData(dates, values);
   customPlot->replot();        
}

Which results in:Scatter points on plot

So obviously the QCPItemTracer didn't find the correct Y values. (I also got some additional axes which I don't want.)

Is QCPItemTracer what I want to use to achieve what I want? I also saw some example using QCPDataMap to find Y-values based on X-values. But as far as I understand, QCPDataMap, is no longer in qcustomplot.

Upvotes: 3

Views: 3157

Answers (1)

remi
remi

Reputation: 1047

I solved my own problem:

I had to call tracer->updatePosition() to actually get the coordinates in the coordinate system in which the tracer is placed.

The axes I didn't want were actually the visualization of the tracer at the final tracer location. I just had to call tracer->setVisibile(false) to hide it.

So in void Qt_PlotTest::setScatterPoints I have this:

QCPItemTracer *tracer = new QCPItemTracer(customPlot);   
   tracer->setGraph(customPlot->graph(0));
   tracer->setInterpolating(true);
   tracer->setVisible(false);    
   for (int i = 0; i < 4; i++){
      tracer->setGraphKey(dates[i]);
      tracer->updatePosition();
      values.push_back(tracer->position->coords().y());
   }

Result:

enter image description here

Upvotes: 4

Related Questions