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.
Code:
def __init__(self, dates):
self.dates = dates
self.setLabelAlignment(Qt.AlignLeft | Qt.AlignBottom)
self.setLabelRotation(270)
def label(self, value):
if value % 1 == 0 and int(value) < len(self.dates):
return QwtText(str
(self.
dates[int(value
)])) else:
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.
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
Re: Qwt X-Axis with Date, correct distance
Thanks for the fast answer! :)
Wich classes/methods I have to implement?
Re: Qwt X-Axis with Date, correct distance
Well now I have a solution to plot the values/dates correctly.
Code:
dateValues = self.datesToValues(dates)
lastDateVal = dateValues[len(dateValues) - 1]
log.info(lastDateVal)
draw = TimeScaleDraw(dates[0])
self.curve.setData(dateValues, numvalues)
def datesToValues(self, dates):
refDate = dates[0].toJulianDay()
values = []
for d in dates: values.append(float(d.toJulianDay() - refDate))
return values
Code:
def __init__(self, refDate):
self.refDate = refDate.toJulianDay()
self.setLabelAlignment(Qt.AlignLeft | Qt.AlignBottom)
self.setLabelRotation(270)
def label(self, value):
next
= QDate.
fromJulianDay(value
+ self.
refDate).
toString("dd.MM.yyyy")
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"
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
1 Attachment(s)
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.
Attachment 7246
[/CODE]