I want to make a real time graph to plot the data from my Arduino and I want to use the following function from QCustomPlot to plot the graph in an ECG style (starting again after few seconds and replacing the previous data):
void QCPGraph::addData(const QVector<double> &keys, const QVector<double> &values)`
with keys=time
and values=data
from serial port.
I already have the serial data and a graph that is continuous but I don't know how to modify this with the function above and make time vector.
Can you give me an example of how to call that function?
If I get it right, you have a graph that it's xAxis
range is constant. Lets say it is defined as MAX_RANGE
seconds, and you want that once it passes MAX_RANGE
seconds it will clear the graph and start over again from 0
sec.
If all this is right then I guess you already have a function that you are calling once every T
seconds in order to update the plot. If not then take a look at this example.
Lets assume that you already have a function that you are calling every T
seconds:
void MyPlot::updatePlot(int yValue)
Then simply add a timeCounter
as a class variable that will be updated every call. Then add an if
statement that will check if it passed MAX_RANGE
. If it did then clear the graph using clearData()
, add the new value and reset timeCounter
. If it didn't then just add the new value. Simple example (just make the changes to fit for your case):
void MyPlot::updatePlot(int yValue){
this->timeCounter += T;
if (this->timeCounter >= MAX_RANGE) {
ui->customPlot->graph(0)->clearData();
ui->customPlot->graph(0)->addData(0, yValue);
this->timeCounter = 0;
}
else {
ui->customPlot->graph(0)->addData(this->timeCounter, yValue);
}
}