QCustomPlot Discussion and Comments

Real time plotting to multiple axesReturn to overview

I'm trying to write code to plot real time data on three different graphs. I am using the layout system to create my graphs and plot to them as follows:

  customPlot->plotLayout()->clear();
  QCPLayoutGrid *subLayout = new QCPLayoutGrid;

  customPlot->plotLayout()->addElement(0, 0, subLayout);

  QCPAxisRect *x = new QCPAxisRect(customPlot, false);
  QCPAxisRect *y = new QCPAxisRect(customPlot, false);
  QCPAxisRect *z = new QCPAxisRect(customPlot, false);

  subLayout->addElement(0, 0, x);
  subLayout->addElement(0, 1, y);
  subLayout->addElement(0, 2, z);

  x->setMaximumSize(300,300);
  x->setMinimumSize(300,300);
  y->setMaximumSize(300,300);
  y->setMinimumSize(300,300);
  z->setMaximumSize(300,300);
  z->setMinimumSize(300,300);

  x->addAxes(QCPAxis::atBottom | QCPAxis::atLeft);
  y->addAxes(QCPAxis::atBottom | QCPAxis::atLeft);
  z->addAxes(QCPAxis::atBottom | QCPAxis::atLeft);

  x->setupFullAxesBox();
  y->setupFullAxesBox();
  z->setupFullAxesBox();

  x->axis(QCPAxis::atLeft)->setRange(-2048, 2048);
  y->axis(QCPAxis::atLeft)->setRange(-2048, 2048);
  z->axis(QCPAxis::atLeft)->setRange(-2048, 2048);

  x->axis(QCPAxis::atBottom)->setTickLabelType(QCPAxis::ltDateTime);
  x->axis(QCPAxis::atBottom)->setDateTimeFormat("hh:mm:ss");
  x->axis(QCPAxis::atBottom)->setAutoTickStep(false);
  x->axis(QCPAxis::atBottom)->setTickStep(2);

  y->axis(QCPAxis::atBottom)->setTickLabelType(QCPAxis::ltDateTime);
  y->axis(QCPAxis::atBottom)->setDateTimeFormat("hh:mm:ss");
  y->axis(QCPAxis::atBottom)->setAutoTickStep(false);
  y->axis(QCPAxis::atBottom)->setTickStep(2);

  z->axis(QCPAxis::atBottom)->setTickLabelType(QCPAxis::ltDateTime);
  z->axis(QCPAxis::atBottom)->setDateTimeFormat("hh:mm:ss");
  z->axis(QCPAxis::atBottom)->setAutoTickStep(false);
  z->axis(QCPAxis::atBottom)->setTickStep(2);

  QCPGraph *xGraph = customPlot->addGraph(x->axis(QCPAxis::atBottom), x->axis(QCPAxis::atLeft));
  QCPGraph *yGraph = customPlot->addGraph(y->axis(QCPAxis::atBottom), y->axis(QCPAxis::atLeft));
  QCPGraph *zGraph = customPlot->addGraph(z->axis(QCPAxis::atBottom), z->axis(QCPAxis::atLeft));

  QCPGraph *xDotGraph = customPlot->addGraph(x->axis(QCPAxis::atBottom), x->axis(QCPAxis::atLeft));
  QCPGraph *yDotGraph = customPlot->addGraph(y->axis(QCPAxis::atBottom), y->axis(QCPAxis::atLeft));
  QCPGraph *zDotGraph = customPlot->addGraph(z->axis(QCPAxis::atBottom), z->axis(QCPAxis::atLeft));

  customPlot->setNotAntialiasedElements(QCP::aeAll);


  // X axis - Blue Line
  xGraph->setPen(QPen(Qt::blue));
  xGraph->setAntialiasedFill(false);

  // X axis - Blue Dot
  xDotGraph->setPen(QPen(Qt::blue));
  xDotGraph->setLineStyle(QCPGraph::lsNone);
  xDotGraph->setScatterStyle(QCPScatterStyle::ssDisc);

  // Connect X axes (left to right and top to bottom) - allows scaling to look good
  connect(x->axis(QCPAxis::atLeft), SIGNAL(rangeChanged(QCPRange)), x->axis(QCPAxis::atRight), SLOT(setRange(QCPRange)));
  connect(x->axis(QCPAxis::atBottom), SIGNAL(rangeChanged(QCPRange)), x->axis(QCPAxis::atTop), SLOT(setRange(QCPRange)));

  double old_key = key;
  for (int i=0; i<50000;i++)
  {
    key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
    xGraph->addData(key, 0);
    xDotGraph->clearData();
    xDotGraph->addData(key, 0);


    xGraph->removeDataBefore(key-8);
    xDotGraph->removeDataBefore(key-8);
    // rescale value (vertical) axis to fit the current data:
    xGraph->rescaleValueAxis();
    xDotGraph->rescaleValueAxis(true);
    x->axis(QCPAxis::atBottom)->setRange(key+0.25, 8, Qt::AlignRight);

    //????????????????????????????????????????????????????????????????????????????????????
    old_key = key;
  }

The issue I am having is with creating the animation.
???? - In the real time demo, the question marks were replaced by customPlot->replot().

I was wondering what the equivalent of replot is for these individual graphs? I tried calling customPlot->replot() but it doesn't work. The figures are only plotted once all the computation is completed.

Thanks in advance!

Which operating system are you using? Perhaps your window manager doesn't redraw in between the iterations. You can try to enable forced redraws by calling

customPlot->setPlottingHint(QCP::phForceRepaint);

once in the beginning.

That didn't work (thanks though). I'm using Windows 7, 64 bit. Any ideas how I could force my windows manager to plot between iterations?

why do you call xDotGraph->clearData() at line 77 when you also call removeDataBefore() at line 82?