[編輯 - 我擴大這一點更在http://www.acooke.org/cute/AutoScalin0.html]
的「好號」的樸素擴展算法似乎對基地12和60,這給了幾個小時和分鐘間隔好工作。這是代碼,我只是砍死在一起:
LIM10 = (10, [(1.5, 1), (3, 2), (7, 5)], [1, 2, 5])
LIM12 = (12, [(1.5, 1), (3, 2), (8, 6)], [1, 2, 6])
LIM60 = (60, [(1.5, 1), (20, 15), (40, 30)], [1, 15, 40])
def heckbert_d(lo, hi, ntick=5, limits=None):
'''
Heckbert's "nice numbers" algorithm for graph ranges, from "Graphics Gems".
'''
if limits is None:
limits = LIM10
(base, rfs, fs) = limits
def nicenum(x, round):
step = base ** floor(log(x)/log(base))
f = float(x)/step
nf = base
if round:
for (a, b) in rfs:
if f < a:
nf = b
break
else:
for a in fs:
if f <= a:
nf = a
break
return nf * step
delta = nicenum(hi-lo, False)
return nicenum(delta/(ntick-1), True)
def heckbert(lo, hi, ntick=5, limits=None):
'''
Heckbert's "nice numbers" algorithm for graph ranges, from "Graphics Gems".
'''
def _heckbert():
d = heckbert_d(lo, hi, ntick=ntick, limits=limits)
graphlo = floor(lo/d) * d
graphhi = ceil(hi/d) * d
fmt = '%' + '.%df' % max(-floor(log10(d)), 0)
value = graphlo
while value < graphhi + 0.5*d:
yield fmt % value
value += d
return list(_heckbert())
因此,舉例來說,如果你想從0到60顯示秒,
>>> heckbert(0, 60, limits=LIM60)
['0', '15', '30', '45', '60']
或小時內從0到5:
>>> heckbert(0, 5, limits=LIM12)
['0', '2', '4', '6']
像「1.5天」,「9天」等事情在實施方面(對我而言)高度依賴於語言。例如,在C甚至C++中,我只是使用unsigned long來保存兩次之間的差值,而在Java中,我可能會創建一個Time或Moment類,並且可能已經有一些地方存在這些類。 .. – Joanis 2010-01-22 04:41:42