1
#Copyright ReportLab Europe Ltd. 2000-2004
2
#see license.txt for license details
3
#history http://www.reportlab.co.uk/cgi-bin/viewcvs.cgi/public/reportlab/trunk/reportlab/graphics/charts/utils.py
4
"Utilities used here and there."
5
__version__=''' $Id$ '''
7
from time import mktime, gmtime, strftime
11
### Dinu's stuff used in some line plots (likely to vansih).
13
def mkTimeTuple(timeString):
14
"Convert a 'dd/mm/yyyy' formatted string to a tuple for use in the time module."
17
dd, mm, yyyy = map(int, string.split(timeString, '/'))
18
list[:3] = [yyyy, mm, dd]
23
def str2seconds(timeString):
24
"Convert a number of seconds since the epoch into a date string."
26
return mktime(mkTimeTuple(timeString))
29
def seconds2str(seconds):
30
"Convert a date string into the number of seconds since the epoch."
32
return strftime('%Y-%m-%d', gmtime(seconds))
35
### Aaron's rounding function for making nice values on axes.
37
from math import log10
39
def nextRoundNumber(x):
40
"""Return the first 'nice round number' greater than or equal to x
42
Used in selecting apropriate tick mark intervals; we say we want
43
an interval which places ticks at least 10 points apart, work out
44
what that is in chart space, and ask for the nextRoundNumber().
45
Tries the series 1,2,5,10,20,50,100.., going up or down as needed.
48
#guess to nearest order of magnitude
53
return -1.0 * nextRoundNumber(-x)
63
base = 10.0 ** (lg - 1)
65
base = 10.0 ** lg # e.g. base(153) = 100
66
# base will always be lower than x
78
### Robin's stuff from rgb_ticks.
80
from math import log10, floor
82
_intervals=(.1, .2, .25, .5)
83
_j_max=len(_intervals)-1
86
def find_interval(lo,hi,I=5):
87
'determine tick parameters for range [lo, hi] using I intervals'
98
raise ValueError, "lo>hi"
100
b= (x>0 and (x<1 or x>10)) and 10**floor(log10(x)) or 1
104
if a<=_intervals[-1]: break
108
while a>_intervals[j]: j = j + 1
126
if hi<=x and n<=lo: break
131
return n, x, ss, lo - n + x - hi
134
def find_good_grid(lower,upper,n=(4,5,6,7,8,9), grid=None):
136
t = divmod(lower,grid)[0] * grid
137
hi, z = divmod(upper,grid)
144
n = xrange(max(1,n-2),max(n+3,2))
148
z=find_interval(lower,upper,i)
155
def ticks(lower, upper, n=(4,5,6,7,8,9), split=1, percent=0, grid=None):
157
return tick positions and labels for range lower<=x<=upper
158
n=number of intervals to try (can be a list or sequence)
159
split=1 return ticks then labels else (tick,label) pairs
161
t, hi, grid = find_good_grid(lower, upper, n, grid)
162
power = floor(log10(grid))
163
if power==0: power = 1
167
if power > 3 or power < -3:
168
format = '%+'+`w+7`+'.0e'
171
digits = int(power)+w
172
format = '%' + `digits`+'.0f'
174
digits = w-int(power)
175
format = '%'+`digits+2`+'.'+`digits`+'f'
177
if percent: format=format+'%%'
179
n = int(float(hi-t)/grid+0.1)+1
185
labels.append(format % v)
190
T.append((v, format % v))
b'\\ No newline at end of file'