I am sending a value to the function plotMainGraph(double serData)
, so it plots the value I send each time. As an example, I am just sending value 5 and 25 continuously, but the plot which I get is not correct, even though I am sending 5 and 25 each time. The plot may hold constant at the value of 25 and suddenly decrease to 5. Sometimes the plot holds constant at 5 for some time and then suddenly increases to 25 before going back to 5.
I want plot to be uniform as explained above: It should plot 5 and 25 at a constant rate, but that is not happening.
Here is my code:
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |
QCP::iSelectLegend | QCP::iSelectPlottables);
ui->customPlot->xAxis->setRange(0, 20);
ui->customPlot->yAxis->setRange(-5, 5);
/*customPlot->plotLayout()->insertRow(0);
QCPTextElement *title = new QCPTextElement(customPlot, "Interaction Example", QFont("sans", 17, QFont::Bold));
customPlot->plotLayout()->addElement(0, 0, title);*/
// ui->customPlot->xAxis->setLabel("Time(in Second)");
// ui->customPlot->yAxis->setLabel("Pressure");
//ui->customPlot->legend->setVisible(true);
QFont legendFont = font();
legendFont.setPointSize(10);
ui->customPlot->legend->setFont(legendFont);
ui->customPlot->legend->setSelectedFont(legendFont);
ui->customPlot->legend->setSelectableParts(QCPLegend::spItems); // legend box shall not be selectable, only legend items
//background color code extra
ui->customPlot->setBackground(QColor("lightgrey"));
//till here
ui->customPlot->addGraph(); // blue line
ui->customPlot->graph(0)->setPen(QPen(QColor("yellow"),3));//
QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
timeTicker->setTimeFormat("%h:%m:%s");
ui->customPlot->xAxis->setTicker(timeTicker);
// ui->customPlot->axisRect()->setupFullAxesBox();
ui->customPlot->yAxis->setRange(-5, 25);//
// make left and bottom axes transfer their ranges to right and top axes:
connect(ui->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->xAxis2, SLOT(setRange(QCPRange)));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), ui->customPlot->yAxis2, SLOT(setRange(QCPRange)));
void MainWindow::plotMainGraph(double serData)
{
connect(&dataTimer, &QTimer::timeout, [this,serData]()
{
static QTime time(QTime::currentTime());
double key = time.elapsed() / 1000;
static double lastPointKey = 0;
if (key - lastPointKey > 0.01)
{
double value = serData;
ui->customPlot->graph(0)->addData(key, value);
lastPointKey = key;
}
ui->customPlot->xAxis->setRange(key, 30, Qt::AlignRight);
ui->customPlot->replot();
});
}
ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes |QCP::iSelectLegend | QCP::iSelectPlottables);
ui->customPlot->xAxis->setRange(0, 20);
ui->customPlot->yAxis->setRange(-5, 5);