mirror of https://github.com/Kozea/pygal.git
Florian Mounier
13 years ago
7 changed files with 176 additions and 42 deletions
@ -0,0 +1,54 @@ |
|||||||
|
#!/usr/bin/env python |
||||||
|
from pygal import Line, Bar, XY, Pie, StackedBar, Config |
||||||
|
from math import cos, sin |
||||||
|
|
||||||
|
bar = Bar() |
||||||
|
rng = [-3, -32, -39] |
||||||
|
bar.add('test1', rng) |
||||||
|
bar.add('test2', map(abs, rng)) |
||||||
|
bar.x_labels = map(str, rng) |
||||||
|
bar.title = "Bar test" |
||||||
|
with open('out-bar.svg', 'w') as f: |
||||||
|
f.write(bar.render()) |
||||||
|
|
||||||
|
stackedbar = StackedBar() |
||||||
|
rng = [3, 32, 39, 12] |
||||||
|
stackedbar.add('test1', rng) |
||||||
|
rng2 = [24, 8, 18, 12] |
||||||
|
stackedbar.add('test2', rng2) |
||||||
|
rng3 = [6, 1, -10, 0] |
||||||
|
stackedbar.add('test3', rng3) |
||||||
|
stackedbar.x_labels = map(lambda x: '%s / %s / %s' % x, |
||||||
|
zip(map(str, rng), |
||||||
|
map(str, rng2), |
||||||
|
map(str, rng3))) |
||||||
|
stackedbar.title = "Stackedbar test" |
||||||
|
with open('out-stackedbar.svg', 'w') as f: |
||||||
|
f.write(stackedbar.render()) |
||||||
|
|
||||||
|
line = Line(Config(y_scale=.0005)) |
||||||
|
rng = range(-30, 31, 5) |
||||||
|
line.add('test1', [cos(x / 10.) for x in rng]) |
||||||
|
line.add('test2', [sin(x / 10.) for x in rng]) |
||||||
|
line.add('test3', [cos(x / 10.) - sin(x / 10.) for x in rng]) |
||||||
|
line.x_labels = map(str, rng) |
||||||
|
line.title = "Line test" |
||||||
|
with open('out-line.svg', 'w') as f: |
||||||
|
f.write(line.render()) |
||||||
|
|
||||||
|
xy = XY(Config(x_scale=1)) |
||||||
|
xy.add('test1', [(1981, 1), (2004, 2), (2003, 10), (2012, 8), (1999, -4)]) |
||||||
|
xy.add('test2', [(1988, -1), (1986, 12), (2007, 7), (2010, 4), (1999, 2)]) |
||||||
|
xy.title = "XY test" |
||||||
|
with open('out-xy.svg', 'w') as f: |
||||||
|
f.write(xy.render()) |
||||||
|
|
||||||
|
pie = Pie() |
||||||
|
pie.add('test', 121) |
||||||
|
pie.add('test2', 29) |
||||||
|
# pie.add('test3', 242) |
||||||
|
# pie.add('test4', 90) |
||||||
|
# pie.add('test5', 175) |
||||||
|
pie.title = "Pie test" |
||||||
|
with open('out-pie.svg', 'w') as f: |
||||||
|
f.write(pie.render()) |
@ -0,0 +1,38 @@ |
|||||||
|
from pygal.base import BaseGraph |
||||||
|
|
||||||
|
|
||||||
|
class StackedBar(BaseGraph): |
||||||
|
"""Stacked Bar graph""" |
||||||
|
|
||||||
|
def _draw(self): |
||||||
|
transposed = zip(*[serie.values for serie in self.series]) |
||||||
|
vals = [sum(val) for val in transposed] |
||||||
|
ymin, ymax = min(min(vals), 0), max(max(vals), 0) |
||||||
|
length = len(self.series[0].values) |
||||||
|
x_pos = [x / float(length) for x in range(length + 1) |
||||||
|
] if length > 1 else [0, 1] # Center if only one value |
||||||
|
y_pos = self._pos( |
||||||
|
ymin, ymax, self.y_scale) if not self.y_labels else map( |
||||||
|
int, self.y_labels) |
||||||
|
x_ranges = zip(x_pos, x_pos[1:]) |
||||||
|
|
||||||
|
x_labels = self.x_labels and zip(self.x_labels, [ |
||||||
|
sum(x_range) / 2 for x_range in x_ranges]) |
||||||
|
y_labels = zip(map(str, y_pos), y_pos) |
||||||
|
|
||||||
|
self._compute_margin(x_labels, y_labels) |
||||||
|
self.svg.set_view(ymin, ymax) |
||||||
|
self.svg.make_graph() |
||||||
|
self.svg.x_axis(x_labels) |
||||||
|
self.svg.y_axis(y_labels) |
||||||
|
self.svg.legend([serie.title for serie in self.series]) |
||||||
|
self.svg.title() |
||||||
|
|
||||||
|
stack_vals = [0] * length |
||||||
|
for serie in self.series: |
||||||
|
serie_node = self.svg.serie(serie.index) |
||||||
|
stack_vals = self.svg.stackbar( |
||||||
|
serie_node, serie, [ |
||||||
|
tuple((x_ranges[i][j], v) for j in range(2)) |
||||||
|
for i, v in enumerate(serie.values)], |
||||||
|
stack_vals) |
Loading…
Reference in new issue