PDA

View Full Version : Suggested way to implement a time-series plot



vix
15th September 2021, 09:13
Hello,
I need to implement a time-series plot (i.e. a plot where every second one new sample is added to a curve).
When the curves contains more than a given number of samples (as an example 100), the x-scale is set so that only the most recent 100 samples are shown.

I did this calling
QwtPlotCurve::setSamples() after I get every new sample, and setting the whole time-history.
I set the x-scale when it's necessary.

But I wonder if there is a better way (maybe appending a new sample to an existing curve).

Thanks in advance

vix
20th October 2021, 07:34
Sorry for pushing,
but can someone share ideas or link to documentation on how to implement a time-series plot?

Every idea is welcome.
Thanks in advance

d_stranz
20th October 2021, 16:00
It sounds like you are confusing the map and the earth it represents. You should be storing your time series data in a separate data structure, not the curve itself. When each new point comes in, you give the curve only the last 100 points to plot and set the x axis range accordingly.

If you don't want your data structure to save points for infinity, you can implement the data structure as a fixed-length linked list, and append new points to the head while removing old points from the tail end. To make copying the last 100 points quick without having to travel the whole list, save the pointer to the list position where the copy starts, and as each new point comes in, add it to the head, move the pointer forward one item in the list, then copy the points from there forward into the curve and update the plot.