Results 1 to 6 of 6

Thread: Qwt X-Axis with Date, correct distance

  1. #1
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Qwt X-Axis with Date, correct distance

    Hi there,

    I have a list with some tuples like [(datetime.date, int), (datetime.date, int),].

    I can already plot one date per tick, but I want to have the correct distance between two different dates.

    Qt Code:
    1. class TimeScaleDraw(QwtScaleDraw):
    2. def __init__(self, dates):
    3. QwtScaleDraw.__init__(self)
    4. self.dates = dates
    5. self.setLabelAlignment(Qt.AlignLeft | Qt.AlignBottom)
    6. self.setLabelRotation(270)
    7.  
    8.  
    9. def label(self, value):
    10. if value % 1 == 0 and int(value) < len(self.dates):
    11. return QwtText(str(self.dates[int(value)]))
    12. else:
    13. return QwtText('')
    To copy to clipboard, switch view to plain text mode 

    I have no idea how I can create a 'time true' axis.

    The dates for example are 2010-02-01, 2010-10-14, 2010-11-11, 2011-03-28.

    I am using PyQt4.Qwt5.
    Last edited by Lykanthropie; 2nd January 2012 at 15:51.

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

    Default Re: Qwt X-Axis with Date, correct distance

    Define a reference date and translate your date values into doubles representing the difference from the reference.
    F.e when you use the julian day as reference you could use QDate;;toJulianDay/fromJuliandDay.

    Uwe

  3. The following user says thank you to Uwe for this useful post:

    Lykanthropie (2nd January 2012)

  4. #3
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Qwt X-Axis with Date, correct distance

    Thanks for the fast answer!

    Wich classes/methods I have to implement?

  5. #4
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: Qwt X-Axis with Date, correct distance

    Well now I have a solution to plot the values/dates correctly.

    Qt Code:
    1. dateValues = self.datesToValues(dates)
    2. div = QwtScaleDiv()
    3. lastDateVal = dateValues[len(dateValues) - 1]
    4. log.info(lastDateVal)
    5. draw = TimeScaleDraw(dates[0])
    6.  
    7. self.qwtPlot.setAxisScaleDraw(QwtPlot.xBottom, draw)
    8. self.qwtPlot.setAxisScale(QwtPlot.xBottom, 0.0, lastDateVal)
    9.  
    10. self.curve.setData(dateValues, numvalues)
    11.  
    12. self.qwtPlot.replot()
    13.  
    14.  
    15. def datesToValues(self, dates):
    16. refDate = dates[0].toJulianDay()
    17. values = []
    18. for d in dates: values.append(float(d.toJulianDay() - refDate))
    19. return values
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. class TimeScaleDraw(QwtScaleDraw):
    2. def __init__(self, refDate):
    3. QwtScaleDraw.__init__(self)
    4. self.refDate = refDate.toJulianDay()
    5. self.setLabelAlignment(Qt.AlignLeft | Qt.AlignBottom)
    6. self.setLabelRotation(270)
    7.  
    8.  
    9. def label(self, value):
    10. next = QDate.fromJulianDay(value + self.refDate).toString("dd.MM.yyyy")
    11. return QwtText(next)
    To copy to clipboard, switch view to plain text mode 

    It is not nice, but it's working.



    Now I want to have changing labels/ticks.

    For Example:
    more than 2 years: label = 2010, 2011, 2012..
    more than 6 months = Mar 2010, Apr 2010 ...

    How do I implement this?

    I mean something like this:

    if dx > 365+366: #zwei Jahre
    format = "YYYY"
    Last edited by Lykanthropie; 3rd January 2012 at 18:54.

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

    Default Re: Qwt X-Axis with Date, correct distance

    This is the job of a scale engine ( QwtScaleEngine ) - unfortunately there is no implementation for date/time values available yet ( on my TODO list since years ).

    Uwe

    PS: looks like I will spend time on a date/time scale engine in the next weeks

  7. #6
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qwt X-Axis with Date, correct distance

    Here is a quick and ugly implementation of date scale engine and qwtscaledraw derived class implementation. It tries to divide the time frame mentioned as seconds to 1.1.2000 into approximately 10 pieces. Such is a division that the point comes out to something like start of month / start of minute rather than like 2nd jan 13:00:57....You need to enter plot points as f(time), secondsTo1.1.2000.....
    Classes: DateScaleEngine:QwtLinearScaleEngine and TimeScaleDraw:QwtScaleDraw
    Hope it helps,
    Prashant Jha.
    new 2.h
    [/CODE]

Similar Threads

  1. Date Scale for X-Axis
    By mishani99 in forum Qwt
    Replies: 3
    Last Post: 6th September 2011, 11:40
  2. Replies: 1
    Last Post: 2nd August 2011, 18:01
  3. Replies: 5
    Last Post: 26th June 2011, 11:43
  4. Time/Date Axis
    By sigger in forum Qwt
    Replies: 12
    Last Post: 1st May 2011, 09:55
  5. Replies: 1
    Last Post: 5th January 2010, 21:00

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.