mirror of https://github.com/Kozea/pygal.git
Florian Mounier
13 years ago
11 changed files with 206 additions and 61 deletions
@ -1,20 +1,7 @@
|
||||
from collections import namedtuple |
||||
|
||||
Serie = namedtuple('Serie', ('title', 'values', 'index')) |
||||
Label = namedtuple('Label', ('label', 'pos')) |
||||
|
||||
|
||||
class Margin(object): |
||||
def __init__(self, top, right, bottom, left): |
||||
self.top = top |
||||
self.right = right |
||||
self.bottom = bottom |
||||
self.left = left |
||||
|
||||
@property |
||||
def x(self): |
||||
return self.left + self.right |
||||
|
||||
@property |
||||
def y(self): |
||||
return self.top + self.bottom |
||||
from pygal.bar import Bar |
||||
from pygal.line import Line |
||||
from pygal.xy import XY |
||||
from pygal.pie import Pie |
||||
from pygal.config import Config |
||||
|
@ -0,0 +1,27 @@
|
||||
from pygal.serie import Serie |
||||
from pygal.base import BaseGraph |
||||
from math import pi |
||||
|
||||
|
||||
class Pie(BaseGraph): |
||||
"""Pie graph""" |
||||
|
||||
def add(self, title, value): |
||||
self.series.append(Serie(title, [value], len(self.series))) |
||||
|
||||
def _draw(self): |
||||
self._compute_margin() |
||||
self.svg.set_view() |
||||
self.svg.make_graph() |
||||
self.svg.legend([serie.title for serie in self.series]) |
||||
self.svg.title() |
||||
total = float(sum(serie.values[0] for serie in self.series)) |
||||
current_angle = 0 |
||||
for serie in self.series: |
||||
val = serie.values[0] |
||||
angle = 2 * pi * val / total |
||||
self.svg.slice( |
||||
self.svg.serie(serie.index), |
||||
current_angle, |
||||
angle, val / total) |
||||
current_angle += angle |
@ -0,0 +1,11 @@
|
||||
class Serie(object): |
||||
def __init__(self, title, values, index): |
||||
self.title = title |
||||
self.values = values |
||||
self.index = index |
||||
|
||||
|
||||
class Label(object): |
||||
def __init__(self, label, pos): |
||||
self.label = label |
||||
self.pos = pos |
@ -0,0 +1,31 @@
|
||||
from pygal.base import BaseGraph |
||||
|
||||
|
||||
class XY(BaseGraph): |
||||
"""XY Line graph""" |
||||
|
||||
def _draw(self): |
||||
for serie in self.series: |
||||
serie.values = sorted(serie.values, key=lambda x: x[0]) |
||||
xvals = [val[0] for serie in self.series for val in serie.values] |
||||
yvals = [val[1] for serie in self.series for val in serie.values] |
||||
xmin, xmax = min(xvals), max(xvals) |
||||
ymin, ymax = min(yvals), max(yvals) |
||||
|
||||
x_pos = self._pos(xmin, xmax, self.x_scale) |
||||
y_pos = self._pos(ymin, ymax, self.y_scale) |
||||
|
||||
x_labels = zip(map(str, x_pos), x_pos) |
||||
y_labels = zip(map(str, y_pos), y_pos) |
||||
|
||||
self._compute_margin(x_labels, y_labels) |
||||
self.svg.set_view(ymin, ymax, xmin, xmax) |
||||
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() |
||||
|
||||
for serie in self.series: |
||||
self.svg.line( |
||||
self.svg.serie(serie.index), serie.values) |
Loading…
Reference in new issue