Results 1 to 5 of 5

Thread: QwtPlot - problem with date-time label at major ticks

  1. #1
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QwtPlot - problem with date-time label at major ticks

    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.

    Screenshot-1.jpg
    Qt Code:
    1. class TimeScaleDraw: public QwtScaleDraw
    2. {
    3. public:
    4. TimeScaleDraw( )
    5. {
    6. setTickLength(QwtScaleDiv::MajorTick, 7);
    7. setTickLength(QwtScaleDiv::MinorTick, 24);
    8. setTickLength(QwtScaleDiv::MediumTick, 0);
    9. setLabelRotation(0);
    10. setLabelAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    11. setSpacing(20);
    12. }
    13. virtual QwtText label( double v ) const
    14. {
    15. QDateTime time;
    16. time = time.fromTime_t(v);
    17.  
    18. QDate date (2010, 11, 27);
    19. time.setDate(date);
    20.  
    21. qDebug()<<"v"<<v;
    22. return time.toString("yyyy-MM-dd-hh-mm-ss"); //MM is important to display months correctly, mm does not work
    23. }
    24.  
    25.  
    26. Plot::Plot(QWidget *parent ): QwtPlot( parent )
    27. {
    28. setAutoReplot( false );
    29.  
    30. QwtPlotCanvas *canvas = new QwtPlotCanvas();
    31. canvas->setBorderRadius( 10 );
    32.  
    33. setCanvas( canvas );
    34.  
    35. plotLayout()->setAlignCanvasToScales( true );
    36.  
    37. QwtLegend *legend = new QwtLegend;
    38. legend->setDefaultItemMode( QwtLegendData::Checkable );
    39. insertLegend( legend, QwtPlot::RightLegend );
    40.  
    41. setAxisTitle( QwtPlot::xBottom, "Date:Time[yyyy-mm-dd-hh-mm-ss]" );
    42.  
    43. QDateTime date1(QDate(2010,11,27), QTime(0,0,0));
    44. QDateTime date2(QDate(2010,12,3), QTime(23,0,0));
    45.  
    46. double x1 = QwtDate::toDouble(date1);
    47. double x2 = QwtDate::toDouble(date2);
    48. double stepSize = 3600;
    49. qDebug()<<date1<<date2<<QwtDate::toDateTime(x1, Qt::LocalTime)<<x1;
    50.  
    51. //QwtDateScaleDraw *scaleDraw = new DateScaleDraw(Qt::UTC);
    52. //setAxisScaleDraw( QwtPlot::xBottom, scaleDraw );
    53. setAxisScaleDraw( QwtPlot::xBottom, new TimeScaleDraw);
    54.  
    55. QwtDateScaleEngine *scaleEngine = new QwtDateScaleEngine();
    56. scaleEngine->autoScale(170, x1, x2, stepSize);
    57. //setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
    58. //setAxisScale( QwtPlot::xBottom, 0, 170 );
    59. setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
    60. setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
    61. .....
    62. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot - problem with date-time label at major ticks

    Quote Originally Posted by GG2013 View Post
    1. How the value of "v" in label is being passed on?
    msecs since Epoch. You can translate it into a QDateTime value using QwtDate::toDateTime().

    Your TimeScaleDraw class doesn't make much sense. Better use a QwtDateScaleDraw object and use setDateFormat() to assign the date format strings you want to have ( by the way: why doesn't mm work ? ).

    The values for MajorTick & MinorTick are set as 7 and 24 resp. but the graph shows 5 major and 20 minor ticks.
    No the tick length is the length of the line used for drawing a tick in pixels. This has nothing to do with the number of the ticks or where to put them.
    If you want to set the position of the ticks explicitly you can use QwtPlot::setAxisScaleDiv(), but usually you want a QwtDateScaleEngine to do this job for you.

    Note that most of the work for implementing the QwtDateScaleEngine was for the part that calculates the ticks for a given interval. The implementation of its autoscaler ( aligning an interval ) is a bit poor in Qwt 6.1.0. I have a better one on my disk that will make it into Qwt 6.1.1, but it needs some more testing. So for the moment I recommend to set the interval using QwtPlot::setAxisScale() - if you know the interval in application code.

    Uwe

    PS: better ask in the Qwt sub forum

  3. #3
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QwtPlot - problem with date-time label at major ticks

    Thanks a lot Uwe for your time.
    Sorry for not posting it in qwt forum (next time).

    Now Major/minor ticks & "v" are clear to me.
    But I still can't get the label right. Please see the code and attached plot.

    If I don't use any scale engine then the label format works fine only with "setDateFormat(QwtDate::Millisecond, QString("yyyy-mm-dd-hh-mm-ss"));" i.e yyyy-mm-dd-hh-mm-ss.
    With any other interval type (for ex Seconds) irrespective of any string format it always returns one particular format.
    When scale engine is used, this formatting is changed again (same as above) as you see in the figure.

    Also, how do I change the start date in x-axis? Even if the data is in mSec (since epoch) it is not reflected in the plot. In otherwords x-data should show "2010-11-27-00-00-00" and not .....1970(epoch).

    Best regards,
    Screenshot.jpg

    Qt Code:
    1. setAxisTitle( QwtPlot::xBottom, "Date:Time[yyyy-mm-dd-hh-mm-ss]" );
    2. QDateTime date1(QDate(2010,11,27), QTime(0,0,0));
    3. QDateTime date2(QDate(2010,12,03), QTime(23,0,0));
    4.  
    5. QwtDateScaleDraw *scaleDraw = new QwtDateScaleDraw;
    6. scaleDraw->setDateFormat(QwtDate::Millisecond, QString("yyyy-mm-dd-hh-mm-ss"));
    7. QwtDateScaleEngine *scaleEngine = new QwtDateScaleEngine( Qt::UTC );
    8. setAxisScaleDraw( QwtPlot::xBottom, scaleDraw );
    9. setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
    10. setAxisScale(QwtPlot::xBottom, (date1.toTime_t())*1000, (date2.toTime_t())*1000, 24*3600*1000);
    11.  
    12. setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
    13. setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,312
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QwtPlot - problem with date-time label at major ticks

    If I don't use any scale engine then the label format works fine only with "setDateFormat(QwtDate::Millisecond, QString("yyyy-mm-dd-hh-mm-ss"));"
    The Qwt Date/Time scale classes use a classification for time interval ( see QwtDate::IntervalType ). QwtDateScaleDraw simply iterates over the major ticks and returns the largest "interval type", that can be used to display a value without rounding error. F.e. your screenshot looks like an interval, that is aligned to days.

    Having a QwtDateScaleEngine is unrelated to this, beside that it will build ticks that are aligned to an interval type and the scale draw object will use a different format then. The 10:00 in your screenshot might have to do with the UTC offset of your local timezone: you have configured the scale engine to use UTC, but the scale draw and your QDateTime values are in local time !

    For each interval type you can set the date format individually. The idea is that it doesn't make much sense to show miliseconds f.e. in a scale like in the screenshot. Note that changing the format for a interval of milliseconds is probably the only one, that you don't want to change as it would round away the milliseconds.

    Another option is to treat any interval as something related to seconds:

    Qt Code:
    1. class YourScaleDraw: public QwtDateScaleDraw
    2. {
    3. ....
    4.  
    5. virtual QwtDate::IntervalType QwtDateScaleDraw::intervalType( const QwtScaleDiv &scaleDiv ) const
    6. {
    7. IntervalType intervalType = QwtDateScaleDraw::intervalType( scaleDiv );
    8. if ( intervalType > QwtDate::Seconds )
    9. intervalType = QwtDate::Seconds;
    10.  
    11. return intervalType;
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

    Of course you could also change the format strings for all interval types showing the seconds instead.

    Also, how do I change the start date in x-axis? Even if the data is in mSec (since epoch) it is not reflected in the plot. In otherwords x-data should show "2010-11-27-00-00-00" and not .....1970(epoch).
    Use QwtDate::toDouble() to build a double from a QDateTime - using QDateTime::toTime_t rounds away the milisecond part.
    But beside that you would have to multiply with 1000.0 - with the code above ( using 1000 as integer ) results in an integer overflow.

    Uwe

  5. #5
    Join Date
    May 2013
    Location
    Melbourne
    Posts
    36
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QwtPlot - problem with date-time label at major ticks

    Thank you so much Uwe for your time and explanations. Sorry for replying late as I was away over the weekend.

    Yes, the major problem was that multiplication with 1000. It was giving a wrong value (I could check that with manual calculation). Using QwtDate::toDouble works fine, else need to specify 1000 as double (as used in commented line which works too).

    The following code now sets the major ticks correctly (please see attached fig). However, I would like also to set 24 minor ticks, each corresponding to hour (current code shows 4 divisions. I was trying with scaleEngine-divideScale but it didn't have any impact.
    Kind regards,
    PS. I can set minor ticks now- Thank you.

    Attachment 9292

    Qt Code:
    1. QDateTime date1(QDate(2010,11,27), QTime(0,0,0));
    2. double x1 = QwtDate::toDouble(date1);
    3. //double x1 = date1.toTime_t() * double(1000);
    4. QDateTime date2(QDate(2010,12,03), QTime(23,0,0));
    5. double x2 = QwtDate::toDouble(date2);
    6. //double x2 = date2.toTime_t() * double(1000) ;
    7.  
    8. QwtDateScaleDraw *scaleDraw = new QwtDateScaleDraw(Qt::LocalTime);
    9. scaleDraw->setDateFormat(QwtDate::Day, QString("yyyy-MM-dd-hh-mm-ss"));
    10. setAxisScaleDraw(QwtPlot::xBottom, scaleDraw);
    11.  
    12. QwtDateScaleEngine *scaleEngine = new QwtDateScaleEngine( Qt::LocalTime );
    13. //scaleEngine->divideScale(x1,x2,7,7*24);
    14. setAxisScaleEngine( QwtPlot::xBottom, scaleEngine );
    15.  
    16. setAxisScale(QwtPlot::xBottom, x1, x2, 24*3600*1000);
    17.  
    18. //qDebug()<<date1<<date2<<x1<<x2;
    19. setAxisLabelRotation( QwtPlot::xBottom, -50.0 );
    20. setAxisLabelAlignment( QwtPlot::xBottom, Qt::AlignLeft | Qt::AlignBottom );
    To copy to clipboard, switch view to plain text mode 
    Last edited by GG2013; 16th July 2013 at 04:17.

Similar Threads

  1. Replies: 14
    Last Post: 30th October 2012, 15:45
  2. Replies: 3
    Last Post: 6th December 2011, 07:56
  3. Qwt date and time problem
    By CassioTC in forum Qwt
    Replies: 0
    Last Post: 25th August 2011, 13:39
  4. QTimer problem with Date/Time
    By bhavikdhoot in forum Qt Programming
    Replies: 2
    Last Post: 12th May 2011, 11:12
  5. date-time problem
    By aegis in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2007, 18:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.