Browse Source

Try of multi value pie

pull/8/head
Florian Mounier 13 years ago
parent
commit
3fee82b9c4
  1. 3
      demo/moulinrouge/__init__.py
  2. 12
      demo/simple_test.py
  3. 24
      pygal/graph/pie.py

3
demo/moulinrouge/__init__.py

@ -119,13 +119,12 @@ def create_app():
config.height = height config.height = height
config.fill = True config.fill = True
config.style = styles[style] config.style = styles[style]
labels = [random_label() for i in range(data)] config.x_labels = [random_label() for i in range(data)]
svgs = [] svgs = []
for type in ('Bar', 'Line', 'XY', 'StackedBar', for type in ('Bar', 'Line', 'XY', 'StackedBar',
'StackedLine', 'HorizontalBar', 'StackedLine', 'HorizontalBar',
'HorizontalStackedBar', 'HorizontalStackedBar',
'Pie', 'Radar'): 'Pie', 'Radar'):
config.x_labels = labels if type != 'Pie' else None
svgs.append({'type': type, svgs.append({'type': type,
'series': xy_series if type == 'XY' else other_series, 'series': xy_series if type == 'XY' else other_series,
'config': b64encode(pickle.dumps(config))}) 'config': b64encode(pickle.dumps(config))})

12
demo/simple_test.py

@ -102,12 +102,12 @@ xy.title = "XY test"
with open('out-xy.svg', 'w') as f: with open('out-xy.svg', 'w') as f:
f.write(xy.render()) f.write(xy.render())
pie = Pie() pie = Pie(Config(style=NeonStyle))
pie.add('test', 121) pie.add('test', [11, 7, 12])
pie.add('test2', 29) pie.add('test2', [29, 21, 9])
# pie.add('test3', 242) pie.add('test3', [24, 10, 32])
# pie.add('test4', 90) pie.add('test4', [20, 18, 9])
# pie.add('test5', 175) pie.add('test5', [17, 5, 10])
pie.title = "Pie test" pie.title = "Pie test"
with open('out-pie.svg', 'w') as f: with open('out-pie.svg', 'w') as f:
f.write(pie.render()) f.write(pie.render())

24
pygal/graph/pie.py

@ -24,12 +24,15 @@ from math import cos, sin, pi
class Pie(Graph): class Pie(Graph):
"""Pie graph""" """Pie graph"""
def slice(self, serie_node, start_angle, angle, perc): def slice(self, serie_node, start_angle, angle, perc,
small=False):
slices = self.svg.node(serie_node, class_="slices") slices = self.svg.node(serie_node, class_="slices")
slice_ = self.svg.node(slices, class_="slice") slice_ = self.svg.node(slices, class_="slice")
center = ((self.width - self.margin.x) / 2., center = ((self.width - self.margin.x) / 2.,
(self.height - self.margin.y) / 2.) (self.height - self.margin.y) / 2.)
r = min(center) r = min(center)
if small:
r *= .8
center_str = '%f %f' % center center_str = '%f %f' % center
rxy = '%f %f' % tuple([r] * 2) rxy = '%f %f' % tuple([r] * 2)
to = '%f %f' % (r * sin(angle), r * (1 - cos(angle))) to = '%f %f' % (r * sin(angle), r * (1 - cos(angle)))
@ -51,19 +54,28 @@ class Pie(Graph):
def _compute(self): def _compute(self):
for serie in self.series: for serie in self.series:
serie.values = [max(serie.values[0], 0)] serie.values = map(lambda x: max(x, 0), serie.values)
return super(Pie, self)._compute() return super(Pie, self)._compute()
def _plot(self): def _plot(self):
total = float(sum(serie.values[0] for serie in self.series)) total = float(sum(map(sum, map(lambda x: x.values, self.series))))
if total == 0: if total == 0:
return return
current_angle = 0 current_angle = 0
for serie in self.series: for serie in self.series:
val = serie.values[0] angle = 2 * pi * sum(serie.values) / total
angle = 2 * pi * val / total
self.slice( self.slice(
self._serie(serie.index), self._serie(serie.index),
current_angle, current_angle,
angle, val / total) angle, sum(serie.values) / total)
if len(serie.values) > 1:
small_current_angle = current_angle
for val in serie.values:
small_angle = 2 * pi * val / total
self.slice(
self._serie(serie.index),
small_current_angle,
small_angle, val / total, True)
small_current_angle += small_angle
current_angle += angle current_angle += angle

Loading…
Cancel
Save