Browse Source

Merge branch 'pie-value-formatter' of https://github.com/never-eat-yellow-snow/pygal into never-eat-yellow-snow-pie-value-formatter

pull/219/merge
Florian Mounier 10 years ago
parent
commit
71857b132f
  1. 13
      pygal/graph/pie.py

13
pygal/graph/pie.py

@ -33,6 +33,15 @@ class Pie(Graph):
_adapters = [positive, none_to_zero] _adapters = [positive, none_to_zero]
@property
def _format(self):
"""Return the value formatter for this graph"""
def percentage_formatter(y, self=self):
total = sum(map(sum, map(lambda x: x.values, self.series)))
perc = y/total
return '{0:.2%}'.format(perc)
return self.value_formatter or percentage_formatter
def slice(self, serie, start_angle, total): def slice(self, serie, start_angle, total):
"""Make a serie slice""" """Make a serie slice"""
serie_node = self.svg.serie(serie) serie_node = self.svg.serie(serie)
@ -57,7 +66,7 @@ class Pie(Graph):
else: else:
angle = 2 * pi * perc angle = 2 * pi * perc
serie_angle += angle serie_angle += angle
val = '{0:.2%}'.format(perc) val = self._format(val)
metadata = serie.metadata.get(i) metadata = serie.metadata.get(i)
slice_ = decorate( slice_ = decorate(
self.svg, self.svg,
@ -77,7 +86,7 @@ class Pie(Graph):
total_perc += perc total_perc += perc
if dual: if dual:
val = '{0:.2%}'.format(total_perc) val = self._format(total_perc*total)
self.svg.slice(serie_node, self.svg.slice(serie_node,
self.svg.node(slices, class_="big_slice"), self.svg.node(slices, class_="big_slice"),
radius * .9, 0, serie_angle, radius * .9, 0, serie_angle,

Loading…
Cancel
Save