prevent double call of _cleanup, which harms usefiles (and is a bad idea in general)
[PyX.git] / pyx / graph / axis / timeaxis.py
blob7eab513cda85fe47b5ad2b99e437b566bb7cb28e
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 super().__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 # TODO: http://stackoverflow.com/questions/399022/why-cant-i-subclass-datetime-date
58 # possible fix: make the datetime an attribute of the tick
60 def __new__(cls, year, month, day, ticklevel=0, labellevel=0, label=None, labelattrs=[], **kwargs):
61 return datetime.datetime.__new__(cls, year, month, day, **kwargs)
63 def __init__(self, year, month, day, ticklevel=0, labellevel=0, label=None, labelattrs=[], **kwargs):
64 self.ticklevel = ticklevel
65 self.labellevel = labellevel
66 self.label = label
67 self.labelattrs = labelattrs[:]
69 def merge(self, other):
70 if self.ticklevel is None or (other.ticklevel is not None and other.ticklevel < self.ticklevel):
71 self.ticklevel = other.ticklevel
72 if self.labellevel is None or (other.labellevel is not None and other.labellevel < self.labellevel):
73 self.labellevel = other.labellevel
76 class timetexter:
78 def __init__(self, format="%c"):
79 self.format = format
81 def labels(self, ticks):
82 for tick in ticks:
83 if tick.labellevel is not None and tick.label is None:
84 tick.label = tick.strftime(self.format)