Skip to content

Commit

Permalink
merge unit changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Dugan committed Feb 16, 2010
1 parent 88b0612 commit 7814bb7
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions webapp/graphite/render/glyph.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@
dict(seconds=864000,minorGridUnit=YEAR, minorGridStep=1, majorGridUnit=YEAR, majorGridStep=1, labelUnit=YEAR, labelStep=1, format="%y"),
)

UNITTAB = {
'binary': (
('Pi', 1024.0**5),
('Ti', 1024.0**4),
('Gi', 1024.0**3),
('Mi', 1024.0**2),
('Ki', 1024.0 )),
'si': (
('P', 1000.0**5),
('T', 1000.0**4),
('G', 1000.0**3),
('M', 1000.0**2),
('K', 1000.0 ))
}


class Graph:
customizable = ('width','height','margin','bgcolor','fgcolor', \
Expand Down Expand Up @@ -280,8 +295,9 @@ class LineGraph(Graph):
('title','vtitle','lineMode','lineWidth','hideLegend', \
'hideAxes','minXStep','hideGrid','majorGridLineColor', \
'minorGridLineColor','thickness','min','max', \
'graphOnly','yMin','yMax','yLimit','yStep','areaMode','areaAlpha','drawNullAsZero','tz','YAxis' \
'pieMode')
'graphOnly','yMin','yMax','yLimit','yStep','areaMode', \
'areaAlpha','drawNullAsZero','tz', 'YAxis','pieMode', \
'yUnitSystem')
validLineModes = ('staircase','slope')
validAreaModes = ('none','first','all','stacked')
validPieModes = ('maximum', 'minimum', 'average')
Expand Down Expand Up @@ -310,6 +326,8 @@ def drawGraph(self,**params):
params['lineWidth'] = params['thickness']
if 'YAxis' not in params:
params['YAxis'] = 'left'
if 'yUnitSystem' not in params:
params['yUnitSystem'] = 'si'
self.params = params
# When Y Axis is labeled on the right, we subtract x-axis positions from the max,
# instead of adding to the minimum
Expand Down Expand Up @@ -604,12 +622,14 @@ def setupYAxis(self):
if not self.params.get('hideAxes',False):
#Create and measure the Y-labels
def makeLabel(yValue): #TODO beautify this!
yValue, prefix = format_units(yValue,
system=self.params.get('yUnitSystem'))
if self.ySpan > 10:
return "%d " % int(yValue)
return "%d %s " % (int(yValue), prefix)
elif self.ySpan > 3:
return "%.1f " % float(yValue)
return "%.1f %s " % (float(yValue), prefix)
else:
return "%.2f " % float(yValue)
return "%.2f %s " % (float(yValue), prefix)
self.yLabelValues = list( frange(self.yBottom,self.yTop,self.yStep) )
self.yLabels = map(makeLabel,self.yLabelValues)
self.yLabelWidth = max([self.getExtents(label)['width'] for label in self.yLabels])
Expand Down Expand Up @@ -868,3 +888,20 @@ def tz_difference(tz):
except:
os.environ['TZ'] = settings.TIME_ZONE
return tz_delta

def format_units(v, system="si"):
"""Format the given value in standardized units.
``system`` is either 'binary' or 'si'
For more info, see:
http://en.wikipedia.org/wiki/SI_prefix
http://en.wikipedia.org/wiki/Binary_prefix
"""

for prefix, size in UNITTAB[system]:
if v >= size:
v /= size
return v, prefix

return v, ""

0 comments on commit 7814bb7

Please sign in to comment.