Hello,
I've read a few questions on the forum and googled for it, yet I can't figure out how to properly set the date range.
I have a CSV file I load into memory, and use boost::gregorian_date, which I then convert into QDate like so:
auto first = csvdata.Rows().front().date;
auto last = csvdata.Rows().back().date;
auto * scaleDraw = new QwtDateScaleDraw( Qt::UTC );
scaleDraw
->setDateFormat
( QwtDate
::Day,
QString("dd-mm-yyyy") );
auto * scaleEngine = new QwtDateScaleEngine( Qt::UTC );
plot.
setAxisScaleEngine( QwtPlot::xBottom, scaleEngine
);
plot.
setAxisScale( QwtPlot::xBottom, start.
toTime_t(), end.
toTime_t() ,
24 * 3600 * 30 );
plot.
setAxisScaleDraw( QwtPlot::xBottom, scaleDraw
);
plot.
setAxisLabelRotation( QwtPlot::xBottom,
-90.0 );
plot.
setAxisLabelAlignment( QwtPlot::xBottom, Qt
::AlignLeft | Qt
::AlignBottom );
auto first = csvdata.Rows().front().date;
auto last = csvdata.Rows().back().date;
QDateTime start ( QDate( first.year(), first.month(), first.day() ), QTime( 0, 0, 0 ) );
QDateTime end ( QDate( last.year(), last.month(), last.day() ), QTime( 0, 0, 0 ) );
auto * scaleDraw = new QwtDateScaleDraw( Qt::UTC );
scaleDraw->setDateFormat( QwtDate::Day, QString("dd-mm-yyyy") );
auto * scaleEngine = new QwtDateScaleEngine( Qt::UTC );
plot.setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
plot.setAxisScale( QwtPlot::xBottom, start.toTime_t(), end.toTime_t() , 24 * 3600 * 30 );
plot.setAxisScaleDraw( QwtPlot::xBottom, scaleDraw );
plot.setAxisLabelRotation( QwtPlot::xBottom, -90.0 );
plot.setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
To copy to clipboard, switch view to plain text mode
I then plot a simple moving average curve, which is essentialy an
std::pair<double, boost::gregorian_date> sma;
std::pair<double, boost::gregorian_date> sma;
To copy to clipboard, switch view to plain text mode
for ( int i = 0; i < sma.size()-1; i++ )
{
static boost::posix_time::ptime epoch ( boost::gregorian::date( 1970, 1, 1 ) );
auto secs = ( boost::posix_time::ptime ( sma[i].second, boost::posix_time::seconds(0) ) - epoch ).total_seconds();
points <<
QPointF( secs, sma
[i
].
first );
}
sma_curve->setSamples( points );
QwtPlotCurve * sma_curve = new QwtPlotCurve();
QPolygonF points;
for ( int i = 0; i < sma.size()-1; i++ )
{
static boost::posix_time::ptime epoch ( boost::gregorian::date( 1970, 1, 1 ) );
auto secs = ( boost::posix_time::ptime ( sma[i].second, boost::posix_time::seconds(0) ) - epoch ).total_seconds();
points << QPointF( secs, sma[i].first );
}
sma_curve->setSamples( points );
To copy to clipboard, switch view to plain text mode
Yet, the date range on the bottom is always starting from the unix time (Jan 1970).
I'm obviously doing something wrong.
Also, the date label for the axis, is not set to dd-mm-yyyyy, but rather to hh:mm DD dd MM yyyy
Yet the plotted data is correct, just misaligned to the actual dates (which in this case should be 2012-Feb-01 to 2014-Nov-12).
plot_error.jpg
Can someone please help?
PS: I'm using qwt 6.1.1 from the svn repository.
Bookmarks