PDA

View Full Version : Plotting using Qwt with time values in the x-axis



leinad
15th July 2020, 13:25
Hi,

I have two QVectors where the y-axis has double values, but the x-axis has time values which are specified in HH::MM::SS.xxxxx, where xxxxx is in milliseconds.
QVector<QString> timeData; //x-axis
QVector<double> attributeData; //y-axis

To setup a plot I have the following:
QwtPlotCurve *curve1 = new QwtPlotCurve( "Curve 1" );

When assigning setSamples since time is not a double value, any ideas on how to plot by time on the x axis? Of course qwt needs to be able to interpret the time so it can move along the x-axis properly. There must be a way to do it, just kind find any info on it. I tried to look it up in the documentation but no luck.

curve1->setSamples(timeData, y-data); //time data is not a double what else can I use?

If someone can post an example, that would great.

Thanks in advance!

d_stranz
15th July 2020, 15:36
Qwt has plenty of examples of curves where the x axis labels are in time units (see here) (https://qwt.sourceforge.io/curvescreenshots.html). Go look at one of those to see how the x axis data is represented and how it is mapped to x axis labels. QwtDateScaleEngine (https://qwt.sourceforge.io/class_qwt_date_scale_engine.html) and QwtDateScaleDraw (https://qwt.sourceforge.io/class_qwt_date_scale_draw.html) might be just the thing you are looking for.

leinad
15th July 2020, 18:55
Thanks, but I was hoping to see some sort of coding example of how to implement it. Nothing fancy just how to assign a date to an array (vector/list, etc) and add it to a plot.

d_stranz
15th July 2020, 23:31
My Qwt 6.1.3 distribution has a "playground" subdirectory with a "timescale" project in it.

leinad
16th July 2020, 15:50
Great, thanks. I'll take a look.

leinad
17th July 2020, 17:21
I looked through the examples, but no where do I see where one can convert time since epoch (which is a double or long long int) to hh:mm::sec.zzzzzz for the x-axis. I have the value in milliseconds but how do I show it on the x-axis as readable time? Also the data is constantly updating so the x-axis time has to change with with it. This is real-time updates. There must be an example somewhere.

Added after 1 28 minutes:

Here is a better description of the problem. The data I get from a database is nanoseconds since 1970.
If I use that value and print out the results such as this, I get the correct date and time:
qDebug() << QDateTime::fromMSecsSinceEpoch(valueInNanoseconds. toLongLong()/1000000).toString();

however when I use that same value in setSamples for the x-axis I'm off by 4 months and several hours for the date and time.

plot = new QwtPlot();

// add curves
curve = new QwtPlotCurve( "Curve 1" );

curve->attach(plot);

scaleDraw = new QwtDateScaleDraw(Qt::UTC);
scaleDraw->setDateFormat(QwtDate::Millisecond, "yyyy-mm-dd hh:mm:ss.zzzzzz");
scaleDraw->setDateFormat(QwtDate::Second, "yyyy-mm-dd hh:mm:ss");
scaleDraw->setDateFormat(QwtDate::Minute, "yyyy-mm-dd hh:mm:ss");
scaleDraw->setDateFormat(QwtDate::Hour, "yyyy-mm-dd hh:mm:ss");
plot->setAxisScaleDraw(QwtPlot::xBottom, scaleDraw);

In a different method, this code fragment to plot the data, timedata is the same value as valueInNanoseconds shown above. The y-data seems to plot correctly is just the date and time does not match to what I expect.
curve->setSamples(timeData, attributeData);
plot->replot();

Added after 14 minutes:

Okay, so I fixed the problem with time being off. I should have used Qt::LocalTime which corrects the time, but not sure why the month is off. Any ideas?