[reportlab-users] Need to extend NormalDateXValueAxis to be with hour, minute and second

Robin Becker robin at reportlab.com
Tue May 17 08:15:27 EDT 2016


On 17/05/2016 11:33, Fatih Karatana wrote:
> Thank you for your response, Robin. I'll look at what you've recently done
> with and try to involve to the code. Do you mind if I let you know when
> I face any hardness while developing? BTW, is there any progress different
> from the latest version of your work?

I did some things to make it work in python3, the initial object inheritance 
wouldn't work there. Beware, this is mail wrapped almost surely. Used it with 
various time ranges and the unit switching does occur, but probably not at the 
point that's best for most people. The axis ticking rawInterval is used to 
compute when we should switch. This currently switches when the interval exceeds 
a particular unit. It might be better to switch when say two intervals exceed 
the unit or say when half the range exceeds the unit.

################################################################################
class TimeValueAxis:
	_mc = 60
	_hc = 60*_mc
	_dc = 24*_hc

	def __init__(self,*args,**kwds):
		if not self.labelTextFormat:
			self.labelTextFormat = self.timeLabelTextFormatter
		self._saved_tickInfo = {}

	def _calcValueStep(self):
		'''Calculate _valueStep for the axis or get from valueStep.'''
		if self.valueStep is None:
			rawRange = self._valueMax - self._valueMin
			rawInterval = rawRange / 
min(float(self.maximumTicks-1),(float(self._length)/self.minimumTickSpacing))
			#here's where we try to choose the correct value for the unit
			if rawInterval >= self._dc:
				d = self._dc
				self._unit = 'days'
			elif rawInterval >= self._hc:
				d = self._hc
				self._unit = 'hours'
			elif rawInterval >= self._mc:
				d = self._mc
				self._unit = 'minutes'
			else:
				d = 1
				self._unit = 'seconds'
			self._unitd = d
			if d>1:
				rawInterval = int(rawInterval/d)
			self._valueStep = nextRoundNumber(rawInterval) * d
		else:
			self._valueStep = self.valueStep

	def timeLabelTextFormatter(self,val):
		u = self._unitd
		k = (u,tuple(self._tickValues))
		if k in self._saved_tickInfo:
			fmt = self._saved_tickInfo[k]
		else:
			uf = float(u)
			tv = [v/uf for v in self._tickValues]
			s = self._unit[0]
			if _allInt(tv):
				fmt = lambda x, uf=uf, s=s: '%.0f%s' % (x/uf,s)
			else:
				stv = ['%.10f' % v for v in tv]
				stvl = max((len(v.rstrip('0'))-v.index('.')-1) for v in stv)
				if u==1:
					fmt = lambda x,uf=uf,fmt='%%.%dfs' % stvl: fmt % (x/uf)
				else:
					#see if we can represent fractions
					fm = 24 if u==self._dc else 60
					fv = [(v - int(v))*fm for v in tv]
					if _allInt(fv):
						s1 = 'h' if u==self._dc else ('m' if u==self._mc else 's')
						fmt = lambda x,uf=uf,fm=fm, fmt='%%d%s%%d%%s' % (s,s1): fmt % 
(int(x/uf),int((x/uf - int(x/uf))*fm))
					else:
						fmt = lambda x,uf=uf,fmt='%%.%df%s' % (stvl,s): fmt % (x/uf)
			self._saved_tickInfo[k] = fmt

		return fmt(val)
		


class XTimeValueAxis(TimeValueAxis,XValueAxis):
	def __init__(self,*args,**kwds):
		XValueAxis.__init__(self,*args,**kwds)
		TimeValueAxis.__init__(self,*args,**kwds)
#################################################################################


>
> On Tue, May 10, 2016 at 2:50 PM Robin Becker <robin at reportlab.com> wrote:
>
.......

-- 
Robin Becker


More information about the reportlab-users mailing list