-2

I want to draw multiple sin with QCustomPlot in Qt. I want the sins to be bellow each other. actually, I want to show something like ECG. Can anyone help me?

Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63
user55340
  • 87
  • 1
  • 6

1 Answers1

2

Your requirements are quite short-spoken, so I will give a simple solution.

All you need is to add multiple sinus graphs to a customPlot object, and add an offset to each sinus.

  customPlot->addGraph();
  customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
  customPlot->addGraph();
  customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
  customPlot->addGraph();
  customPlot->graph(2)->setPen(QPen(Qt::green)); // line color green for third graph
  customPlot->addGraph();
  customPlot->graph(3)->setPen(QPen(Qt::yellow)); // line color yellow for fourth graph
  // generate some points of data
  QVector<double> x(250), y0(250), y1(250), y2(250), y3(250);
  for (int i=0; i<250; ++i)
  {
    x[i] = i;
    y0[i] = qCos(i/10.0);
    y1[i] = qCos(i/10.0) + 3;   //add offset
    y2[i] = qCos(i/10.0) + 6;   //add offset
    y3[i] = qCos(i/10.0) + 9;   //add offset
  }
  // configure right and top axis to show ticks but no labels:
  // (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
  customPlot->yAxis->setTickLabels(false);
  customPlot->xAxis2->setVisible(true);
  customPlot->xAxis2->setTickLabels(false);
  customPlot->yAxis2->setVisible(true);
  customPlot->yAxis2->setTickLabels(false);
  // make left and bottom axes always transfer their ranges to right and top axes:
  connect(customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->xAxis2, SLOT(setRange(QCPRange)));
  connect(customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis2, SLOT(setRange(QCPRange)));
  // pass data points to graphs:
  customPlot->graph(0)->setData(x, y0);
  customPlot->graph(1)->setData(x, y1);
  customPlot->graph(2)->setData(x, y2);
  customPlot->graph(3)->setData(x, y3);
  // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
  customPlot->graph(0)->rescaleAxes();
  // same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
  customPlot->graph(1)->rescaleAxes(true);
  customPlot->graph(2)->rescaleAxes(true);
  customPlot->graph(3)->rescaleAxes(true);
  // Note: we could have also just called customPlot->rescaleAxes(); instead
  // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
  customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);

The result will be something like this: enter image description here

Rishu Singh
  • 49
  • 1
  • 11
Bence Kaulics
  • 7,066
  • 7
  • 33
  • 63