GG2013
11th July 2013, 03:57
Hallo,
My x-axis data is in date-time (eg: 2010-11-27-01-00-00) format and it is taken hourly over 7 days. I would like to have 7 major ticks and at each tick the label would
be like 2010-11-27-00-00-00, 2010-11-27-01-00-00,.....2010-12-03-23-00-00. Somehow I can't figure it out properly- please see the code below & the attached figure.
Also I would like to have a few clarifications:
1. How the value of "v" in label is being passed on? Sorry, this has been asked before in the forum but the answer by Uwe is not clear to me.
2. The values for MajorTick & MinorTick are set as 7 and 24 resp. but the graph shows 5 major and 20 minor ticks.
3. The values of date1 & date2 are correctly set (i.e 27-Nov-2010 & 03-Dec-2010) but x1 & x2 both show a value of 1.29e12- don't understand why?
Any help would be greatly appreciated.
9278
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw( )
{
setTickLength(QwtScaleDiv::MajorTick, 7);
setTickLength(QwtScaleDiv::MinorTick, 24);
setTickLength(QwtScaleDiv::MediumTick, 0);
setLabelRotation(0);
setLabelAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
setSpacing(20);
}
virtual QwtText label( double v ) const
{
QDateTime time;
time = time.fromTime_t(v);
QDate date (2010, 11, 27);
time.setDate(date);
qDebug()<<"v"<<v;
return time.toString("yyyy-MM-dd-hh-mm-ss"); //MM is important to display months correctly, mm does not work
}
Plot::Plot(QWidget *parent ): QwtPlot( parent )
{
setAutoReplot( false );
QwtPlotCanvas *canvas = new QwtPlotCanvas();
canvas->setBorderRadius( 10 );
setCanvas( canvas );
plotLayout()->setAlignCanvasToScales( true );
QwtLegend *legend = new QwtLegend;
legend->setDefaultItemMode( QwtLegendData::Checkable );
insertLegend( legend, QwtPlot::RightLegend );
setAxisTitle( QwtPlot::xBottom, "Date:Time[yyyy-mm-dd-hh-mm-ss]" );
QDateTime date1(QDate(2010,11,27), QTime(0,0,0));
QDateTime date2(QDate(2010,12,3), QTime(23,0,0));
double x1 = QwtDate::toDouble(date1);
double x2 = QwtDate::toDouble(date2);
double stepSize = 3600;
qDebug()<<date1<<date2<<QwtDate::toDateTime(x1, Qt::LocalTime)<<x1;
//QwtDateScaleDraw *scaleDraw = new DateScaleDraw(Qt::UTC);
//setAxisScaleDraw( QwtPlot::xBottom, scaleDraw );
setAxisScaleDraw( QwtPlot::xBottom, new TimeScaleDraw);
QwtDateScaleEngine *scaleEngine = new QwtDateScaleEngine();
scaleEngine->autoScale(170, x1, x2, stepSize);
//setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
//setAxisScale( QwtPlot::xBottom, 0, 170 );
setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
.....
}
My x-axis data is in date-time (eg: 2010-11-27-01-00-00) format and it is taken hourly over 7 days. I would like to have 7 major ticks and at each tick the label would
be like 2010-11-27-00-00-00, 2010-11-27-01-00-00,.....2010-12-03-23-00-00. Somehow I can't figure it out properly- please see the code below & the attached figure.
Also I would like to have a few clarifications:
1. How the value of "v" in label is being passed on? Sorry, this has been asked before in the forum but the answer by Uwe is not clear to me.
2. The values for MajorTick & MinorTick are set as 7 and 24 resp. but the graph shows 5 major and 20 minor ticks.
3. The values of date1 & date2 are correctly set (i.e 27-Nov-2010 & 03-Dec-2010) but x1 & x2 both show a value of 1.29e12- don't understand why?
Any help would be greatly appreciated.
9278
class TimeScaleDraw: public QwtScaleDraw
{
public:
TimeScaleDraw( )
{
setTickLength(QwtScaleDiv::MajorTick, 7);
setTickLength(QwtScaleDiv::MinorTick, 24);
setTickLength(QwtScaleDiv::MediumTick, 0);
setLabelRotation(0);
setLabelAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
setSpacing(20);
}
virtual QwtText label( double v ) const
{
QDateTime time;
time = time.fromTime_t(v);
QDate date (2010, 11, 27);
time.setDate(date);
qDebug()<<"v"<<v;
return time.toString("yyyy-MM-dd-hh-mm-ss"); //MM is important to display months correctly, mm does not work
}
Plot::Plot(QWidget *parent ): QwtPlot( parent )
{
setAutoReplot( false );
QwtPlotCanvas *canvas = new QwtPlotCanvas();
canvas->setBorderRadius( 10 );
setCanvas( canvas );
plotLayout()->setAlignCanvasToScales( true );
QwtLegend *legend = new QwtLegend;
legend->setDefaultItemMode( QwtLegendData::Checkable );
insertLegend( legend, QwtPlot::RightLegend );
setAxisTitle( QwtPlot::xBottom, "Date:Time[yyyy-mm-dd-hh-mm-ss]" );
QDateTime date1(QDate(2010,11,27), QTime(0,0,0));
QDateTime date2(QDate(2010,12,3), QTime(23,0,0));
double x1 = QwtDate::toDouble(date1);
double x2 = QwtDate::toDouble(date2);
double stepSize = 3600;
qDebug()<<date1<<date2<<QwtDate::toDateTime(x1, Qt::LocalTime)<<x1;
//QwtDateScaleDraw *scaleDraw = new DateScaleDraw(Qt::UTC);
//setAxisScaleDraw( QwtPlot::xBottom, scaleDraw );
setAxisScaleDraw( QwtPlot::xBottom, new TimeScaleDraw);
QwtDateScaleEngine *scaleEngine = new QwtDateScaleEngine();
scaleEngine->autoScale(170, x1, x2, stepSize);
//setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
//setAxisScale( QwtPlot::xBottom, 0, 170 );
setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
.....
}