Results 1 to 4 of 4

Thread: Axis changes date-stringformat after zooming (QwtDateScaleDraw)

  1. #1
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Axis changes date-stringformat after zooming (QwtDateScaleDraw)

    I have a QwtDateScaleDraw on my x-axis. When I zoom out my xAxis shows me dates like:
    "08:44:00
    Fr 28 Jan 1916"

    actually it should be
    "08:44:00
    28.01.16"

    when I zoom in again, it has the right Format again

    Qt Code:
    1. double MonitorPlot::getXPoint(QString m_date, QString m_time)
    2. {
    3. QDateTime datetime;
    4. QDate date;
    5. QTime time;
    6.  
    7. time = time.fromString(m_time, "hh:mm:ss");
    8. date = date.fromString(m_date, "dd.MM.yy");
    9.  
    10. datetime.setDate(date);
    11. datetime.setTime(time);
    12.  
    13. return QwtDate::toDouble(datetime);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #define TIMESTRINGFORMAT "hh:mm:ss"
    2. #define DATESTRINGFORMAT "dd.MM.yy"
    3.  
    4. QwtPlot *MonitorPlot::getPlot()
    5. {
    6. QwtPlot *plot;
    7. plot = new QwtPlot(this);
    8. plot->setCanvasBackground(Qt::white);
    9.  
    10. QwtPlotGrid *liveGrid = new QwtPlotGrid();
    11. liveGrid->setMajorPen( QPen( Qt::gray, 0, Qt::DotLine ) );
    12. liveGrid->setMinorPen( QPen( Qt::gray, 0 , Qt::DotLine ) );
    13. liveGrid->attach(plot);
    14.  
    15. plot->setAxisAutoScale(QwtPlot::yLeft, true);
    16.  
    17. QwtDateScaleDraw *scaleDraw = new QwtDateScaleDraw( Qt::LocalTime );
    18. scaleDraw->setDateFormat(QwtDate::Millisecond, QString("%1%2%3").arg(TIMESTRINGFORMAT).arg("\n").arg(DATESTRINGFORMAT));
    19. plot->setAxisScaleDraw(QwtPlot::xBottom, scaleDraw);
    20.  
    21. //plot->enableAxis(QwtPlot::xBottom, false);
    22.  
    23. setLegend(plot);
    24. connect( plot->axisWidget( QwtPlot::xBottom ), SIGNAL( scaleDivChanged() ), SLOT( scaleDivChanged() ) );
    25.  
    26. return plot;
    27. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Axis changes date-stringformat after zooming (QwtDateScaleDraw)

    QwtDateScaleDraw does the same conversion regardless of the values. So in theory zooming shouldn't have an impact - beside, when the conversion algorithm is wrong for certain values.

    The code you are interested in is:
    Qt Code:
    1. QwtText QwtDateScaleDraw::label( double value ) const
    2. {
    3. const QDateTime dt = toDateTime( value );
    4. const QString fmt = dateFormatOfDate(
    5. dt, intervalType( scaleDiv() ) );
    6.  
    7. return QwtDate::toString( dt, fmt, d_data->week0Type );
    8. }
    To copy to clipboard, switch view to plain text mode 
    I recommend to do some testing with your values, maybe you find something ( see QwtDate::toDouble() to feed in value parameters ).

    Uwe

  3. #3
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Axis changes date-stringformat after zooming (QwtDateScaleDraw)

    Qt Code:
    1. scaleDraw->setDateFormat(QwtDate::Millisecond, QString("%1%2%3").arg(TIMESTRINGFORMAT).arg("\n").arg(DATESTRINGFORMAT));
    To copy to clipboard, switch view to plain text mode 

    I set the DateFormat only for IntervalType Milliseconds. I also need to set the dateformat for Second, Minute, Hour, Day, Week and month.
    I thought "setDateFormat" would set a specific IntervalType. But I have to setDateFormat for different IntervalTypes, am I right?

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

    Default Re: Axis changes date-stringformat after zooming (QwtDateScaleDraw)

    The algorithm for finding the IntervalType for a specific scale is implemented in QwtDateScaleDraw::intervalType(). Then the format for the chosen IntervalType is used for converting the tick labels to a string.

    So yes, if other interval types might happen in your application ( and your are unhappy with the default formats ) you would have to modify all of them. But of course a format including milliseconds doesn't make too much sense, when all tick labels are aligned to hours as you waste space for appending "000".

    Uwe

Similar Threads

  1. KDChart Date-time Axis howto?
    By galrub in forum Qt Programming
    Replies: 0
    Last Post: 15th October 2012, 12:36
  2. Qwt X-Axis with Date, correct distance
    By Lykanthropie in forum Qwt
    Replies: 5
    Last Post: 5th January 2012, 19:22
  3. zooming when mouse hovers axis-label
    By Andreas in forum Qwt
    Replies: 3
    Last Post: 12th December 2011, 10:04
  4. Date Scale for X-Axis
    By mishani99 in forum Qwt
    Replies: 3
    Last Post: 6th September 2011, 12:40
  5. Time/Date Axis
    By sigger in forum Qwt
    Replies: 12
    Last Post: 1st May 2011, 10:55

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.