add a layered canvas class
[PyX/mjg.git] / pyx / graph / axis / timeaxis.py
blob1b53361fe99720c50dcc471d2203662ab6985400
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2004-2006 André Wobst <wobsta@users.sourceforge.net>
6 # This file is part of PyX (http://pyx.sourceforge.net/).
8 # PyX is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # PyX is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with PyX; if not, write to the Free Software
20 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import datetime
24 from pyx.graph import style
25 from pyx.graph.axis import axis, rater
27 """some experimental code for creating a time axis
28 - it needs python 2.3 to be used (it is based on the new datetime data type)
29 - a timeaxis is always based on the datetime data type (there is no distinction between times and dates)
30 """
32 class timeaxis(axis.linear):
33 "time axis mapping based "
35 # TODO: how to deal with reversed timeaxis?
37 def __init__(self, parter=None, rater=rater.linear(), **args):
38 axis._regularaxis.__init__(self, divisor=None, **args)
39 self.parter = parter
40 self.rater = rater
42 def convert(self, data, x):
43 # XXX float division of timedelta instances
44 def mstimedelta(td):
45 "return the timedelta in microseconds"
46 return td.microseconds + 1000000*(td.seconds + 3600*24*td.days)
47 return mstimedelta(x - data.min) / float(mstimedelta(data.max - data.min))
48 # we could store float(mstimedelta(self.dx)) instead of self.dx, but
49 # I prefer a different solution (not based on huge integers) for the
50 # future
52 zero = datetime.timedelta(0)
55 class timetick(datetime.datetime):
57 def __init__(self, year, month, day, ticklevel=0, labellevel=0, label=None, labelattrs=[], **kwargs):
58 datetime.datetime.__init__(self, year, month, day, **kwargs)
59 self.ticklevel = ticklevel
60 self.labellevel = labellevel
61 self.label = label
62 self.labelattrs = labelattrs[:]
64 def merge(self, other):
65 if self.ticklevel is None or (other.ticklevel is not None and other.ticklevel < self.ticklevel):
66 self.ticklevel = other.ticklevel
67 if self.labellevel is None or (other.labellevel is not None and other.labellevel < self.labellevel):
68 self.labellevel = other.labellevel
71 class timetexter:
73 def __init__(self, format="%c"):
74 self.format = format
76 def labels(self, ticks):
77 for tick in ticks:
78 if tick.labellevel is not None and tick.label is None:
79 tick.label = tick.strftime(self.format)