Browse Source

Include index in serie

pull/136/head
Florian Mounier 11 years ago
parent
commit
027e0c2fae
  1. 21
      pygal/graph/bar.py
  2. 9
      pygal/graph/box.py
  3. 7
      pygal/graph/dot.py
  4. 11
      pygal/graph/funnel.py
  5. 8
      pygal/graph/gauge.py
  6. 16
      pygal/graph/graph.py
  7. 19
      pygal/graph/histogram.py
  8. 10
      pygal/graph/horizontalbar.py
  9. 11
      pygal/graph/line.py
  10. 6
      pygal/graph/pie.py
  11. 5
      pygal/graph/stackedbar.py
  12. 5
      pygal/graph/treemap.py
  13. 7
      pygal/graph/verticalpyramid.py
  14. 3
      pygal/serie.py
  15. 19
      pygal/svg.py
  16. 3
      pygal/util.py

21
pygal/graph/bar.py

@ -36,20 +36,19 @@ class Bar(Graph):
self._x_ranges = None
super(Bar, self).__init__(*args, **kwargs)
def _bar(self, parent, x, y, index, i, zero,
secondary=False, rounded=False):
def _bar(self, serie, parent, x, y, i, zero, secondary=False):
width = (self.view.x(1) - self.view.x(0)) / self._len
x, y = self.view((x, y))
series_margin = width * self._series_margin
x += series_margin
width -= 2 * series_margin
width /= self._order
x += index * width
x += serie.index * width
serie_margin = width * self._serie_margin
x += serie_margin
width -= 2 * serie_margin
height = self.view.y(zero) - y
r = rounded * 1 if rounded else 0
r = serie.rounded_bars * 1 if serie.rounded_bars else 0
self.svg.transposable_node(
parent, 'rect',
x=x, y=y, rx=r, ry=r, width=width, height=height,
@ -57,8 +56,9 @@ class Bar(Graph):
transpose = swap if self.horizontal else ident
return transpose((x + width / 2, y + height / 2))
def bar(self, serie_node, serie, index, rescale=False):
def bar(self, serie, rescale=False):
"""Draw a bar graph for a serie"""
serie_node = self.svg.serie(serie)
bars = self.svg.node(serie_node['plot'], class_="bars")
if rescale and self.secondary_series:
points = [
@ -79,8 +79,7 @@ class Bar(Graph):
val = self._format(serie.values[i])
x_center, y_center = self._bar(
bar, x, y, index, i, self.zero, secondary=rescale,
rounded=serie.rounded_bars)
serie, bar, x, y, i, self.zero, secondary=rescale)
self._tooltip_data(
bar, val, x_center, y_center, classes="centered")
self._static_value(serie_node, val, x_center, y_center)
@ -130,7 +129,7 @@ class Bar(Graph):
for y in y_pos]
def _plot(self):
for index, serie in enumerate(self.series):
self.bar(self._serie(index), serie, index)
for index, serie in enumerate(self.secondary_series, len(self.series)):
self.bar(self._serie(index), serie, index, True)
for serie in self.series:
self.bar(serie)
for serie in self.secondary_series:
self.bar(serie, True)

9
pygal/graph/box.py

@ -83,13 +83,14 @@ class Box(Graph):
"""
Plot the series data
"""
for index, serie in enumerate(self.series):
self._boxf(self._serie(index), serie, index)
for serie in self.series:
self._boxf(serie)
def _boxf(self, serie_node, serie, index):
def _boxf(self, serie):
"""
For a specific series, draw the box plot.
"""
serie_node = self.svg.serie(serie)
# Note: q0 and q4 do not literally mean the zero-th quartile
# and the fourth quartile, but rather the distance from 1.5 times
# the inter-quartile range to Q1 and Q3, respectively.
@ -103,7 +104,7 @@ class Box(Graph):
metadata)
val = self._format(serie.values)
x_center, y_center = self._draw_box(box, serie.values, index)
x_center, y_center = self._draw_box(box, serie.values, serie.index)
self._tooltip_data(box, val, x_center, y_center, classes="centered")
self._static_value(serie_node, val, x_center, y_center)

7
pygal/graph/dot.py

@ -32,8 +32,9 @@ class Dot(Graph):
_adapters = [positive]
def dot(self, serie_node, serie, r_max):
def dot(self, serie, r_max):
"""Draw a dot line"""
serie_node = self.svg.serie(serie)
view_values = list(map(self.view, serie.points))
for i, value in safe_enumerate(serie.values):
x, y = view_values[i]
@ -72,5 +73,5 @@ class Dot(Graph):
r_max = min(
self.view.x(1) - self.view.x(0),
(self.view.y(0) or 0) - self.view.y(1)) / (2 * (self._max or 1) * 1.05)
for index, serie in enumerate(self.series):
self.dot(self._serie(index), serie, r_max)
for serie in self.series:
self.dot(serie, r_max)

11
pygal/graph/funnel.py

@ -35,9 +35,9 @@ class Funnel(Graph):
def _format(self, value):
return super(Funnel, self)._format(abs(value))
def funnel(self, serie_node, serie, index):
def funnel(self, serie):
"""Draw a dot line"""
serie_node = self.svg.serie(serie)
fmt = lambda x: '%f %f' % x
for i, poly in enumerate(serie.points):
metadata = serie.metadata.get(i)
@ -54,7 +54,7 @@ class Funnel(Graph):
class_='funnel reactive tooltip-trigger')
x, y = self.view((
self._x_labels[index][1], # Poly center from label
self._x_labels[serie.index][1], # Poly center from label
sum([point[1] for point in poly]) / len(poly)))
self._tooltip_data(funnels, value, x, y, classes='centered')
self._static_value(serie_node, value, x, y)
@ -93,6 +93,5 @@ class Funnel(Graph):
self._y_labels = list(zip(map(self._format, y_pos), y_pos))
def _plot(self):
for index, serie in enumerate(self.series):
self.funnel(
self._serie(index), serie, index)
for serie in self.series:
self.funnel(serie)

8
pygal/graph/gauge.py

@ -41,7 +41,8 @@ class Gauge(Graph):
self.height - self.margin.y,
self._box)
def needle(self, serie_node, serie):
def needle(self, serie):
serie_node = self.svg.serie(serie)
for i, theta in enumerate(serie.values):
if theta is None:
continue
@ -112,6 +113,5 @@ class Gauge(Graph):
self._x_labels = list(zip(map(self._format, x_pos), x_pos))
def _plot(self):
for index, serie in enumerate(self.series):
self.needle(
self._serie(index), serie)
for serie in self.series:
self.needle(serie)

16
pygal/graph/graph.py

@ -403,22 +403,6 @@ class Graph(BaseGraph):
-90, self._legend_at_left_width, yc)
text.text = title_line
def _serie(self, serie):
"""Make serie node"""
return dict(
plot=self.svg.node(
self.nodes['plot'],
class_='series serie-%d color-%d' % (
serie, serie % len(self.style['colors']))),
overlay=self.svg.node(
self.nodes['overlay'],
class_='series serie-%d color-%d' % (
serie, serie % len(self.style['colors']))),
text_overlay=self.svg.node(
self.nodes['text_overlay'],
class_='series serie-%d color-%d' % (
serie, serie % len(self.style['colors']))))
def _interpolate(self, xs, ys):
"""Make the interpolation"""
x = []

19
pygal/graph/histogram.py

@ -63,8 +63,7 @@ class Histogram(Graph):
sum(map(abs, self.xvals)) != 0,
sum(map(abs, self.yvals)) != 0))
def _bar(self, parent, x0, x1, y, index, i, zero,
secondary=False, rounded=False):
def _bar(self, serie, parent, x0, x1, y, i, zero, secondary=False):
x, y = self.view((x0, y))
x1, _ = self.view((x1, y))
width = x1 - x
@ -73,7 +72,7 @@ class Histogram(Graph):
x += series_margin
width -= 2 * series_margin
r = self.rounded_bars * 1 if self.rounded_bars else 0
r = serie.rounded_bars * 1 if serie.rounded_bars else 0
self.svg.transposable_node(
parent, 'rect',
x=x, y=y, rx=r, ry=r, width=width, height=height,
@ -81,8 +80,9 @@ class Histogram(Graph):
transpose = swap if self.horizontal else ident
return transpose((x + width / 2, y + height / 2))
def bar(self, serie_node, serie, index, rescale=False):
def bar(self, serie, rescale=False):
"""Draw a bar graph for a serie"""
serie_node = self.svg.serie(serie)
bars = self.svg.node(serie_node['plot'], class_="histbars")
points = serie.points
@ -98,8 +98,7 @@ class Histogram(Graph):
val = self._format(serie.values[i][0])
x_center, y_center = self._bar(
bar, x0, x1, y, index, i, self.zero, secondary=rescale,
rounded=serie.rounded_bars)
serie, bar, x0, x1, y, i, self.zero, secondary=rescale)
self._tooltip_data(
bar, val, x_center, y_center, classes="centered")
self._static_value(serie_node, val, x_center, y_center)
@ -136,7 +135,7 @@ class Histogram(Graph):
self._y_labels = list(zip(map(self._format, y_pos), y_pos))
def _plot(self):
for index, serie in enumerate(self.series):
self.bar(self._serie(index), serie, index)
for index, serie in enumerate(self.secondary_series, len(self.series)):
self.bar(self._serie(index), serie, index, True)
for serie in self.series:
self.bar(serie)
for serie in self.secondary_series:
self.bar(serie, True)

10
pygal/graph/horizontalbar.py

@ -28,9 +28,7 @@ class HorizontalBar(HorizontalGraph, Bar):
"""Horizontal Bar graph"""
def _plot(self):
for index, serie in enumerate(self.series[::-1]):
num = len(self.series) - index - 1
self.bar(self._serie(num), serie, index)
for index, serie in enumerate(self.secondary_series[::-1]):
num = len(self.secondary_series) + len(self.series) - index - 1
self.bar(self._serie(num), serie, index + len(self.series), True)
for serie in self.series[::-1]:
self.bar(serie)
for serie in self.secondary_series[::-1]:
self.bar(serie, True)

11
pygal/graph/line.py

@ -66,8 +66,9 @@ class Line(Graph):
values +
[(values[end][0], zero)])
def line(self, serie_node, serie, rescale=False):
def line(self, serie, rescale=False):
"""Draw the line serie"""
serie_node = self.svg.serie(serie)
if rescale and self.secondary_series:
points = [
(x, self._scale_diff + (y - self._scale_min_2nd) * self._scale)
@ -149,8 +150,8 @@ class Line(Graph):
self._y_labels = list(zip(map(self._format, y_pos), y_pos))
def _plot(self):
for index, serie in enumerate(self.series):
self.line(self._serie(index), serie)
for serie in self.series:
self.line(serie)
for index, serie in enumerate(self.secondary_series, len(self.series)):
self.line(self._serie(index), serie, True)
for serie in self.secondary_series:
self.line(serie, True)

6
pygal/graph/pie.py

@ -33,8 +33,9 @@ class Pie(Graph):
_adapters = [positive, none_to_zero]
def slice(self, serie_node, start_angle, serie, total):
def slice(self, serie, start_angle, total):
"""Make a serie slice"""
serie_node = self.svg.serie(serie)
dual = self._len > 1 and not self._order == 1
slices = self.svg.node(serie_node['plot'], class_="slices")
@ -93,6 +94,5 @@ class Pie(Graph):
current_angle = 0
for index, serie in enumerate(self.series):
angle = self.slice(
self._serie(index), current_angle, serie, total)
angle = self.slice(serie, current_angle, total)
current_angle += angle

5
pygal/graph/stackedbar.py

@ -92,8 +92,7 @@ class StackedBar(Bar):
self._secondary_max = (positive_vals and max(
max(positive_vals), self.zero)) or self.zero
def _bar(self, parent, x, y, index, i, zero,
secondary=False, rounded=False):
def _bar(self, serie, parent, x, y, i, zero, secondary=False):
if secondary:
cumulation = (self.secondary_negative_cumulation
if y < self.zero else
@ -122,7 +121,7 @@ class StackedBar(Bar):
x += serie_margin
width -= 2 * serie_margin
height = self.view.y(zero) - y
r = rounded * 1 if rounded else 0
r = serie.rounded_bars * 1 if serie.rounded_bars else 0
self.svg.transposable_node(
parent, 'rect',
x=x, y=y, rx=r, ry=r, width=width, height=height,

5
pygal/graph/treemap.py

@ -71,7 +71,7 @@ class Treemap(Graph):
self._rect(serie, serie_node, rects, datum, x, y, w, h, i)
else:
datum = data[0]
serie_node = self._serie(datum._index)
serie_node = self.svg.serie(datum)
self._binary_tree(
list(enumerate(datum.values)),
total, x, y, w, h,
@ -126,7 +126,4 @@ class Treemap(Graph):
self.view.box.ymax = h = total / w
self.view.box.fix()
for index, serie in enumerate(self.series):
serie._index = index
self._binary_tree(self.series, total, x, y, w, h)

7
pygal/graph/verticalpyramid.py

@ -56,9 +56,8 @@ class VerticalPyramid(StackedBar):
self._secondary_max = max(max(positive_vals), max(negative_vals))
self._secondary_min = - self._secondary_max
def _bar(self, parent, x, y, index, i, zero,
secondary=False, rounded=False):
if index % 2:
def _bar(self, serie, parent, x, y, i, zero, secondary=False):
if serie.index % 2:
y = -y
return super(VerticalPyramid, self)._bar(
parent, x, y, index, i, zero, secondary, rounded)
serie, parent, x, y, i, zero, secondary)

3
pygal/serie.py

@ -25,7 +25,8 @@ from pygal.util import cached_property
class Serie(object):
"""Serie containing title, values and the graph serie index"""
def __init__(self, title, values, config, metadata=None):
def __init__(self, index, title, values, config, metadata=None):
self.index = index
self.title = title
self.values = values
self.config = config

19
pygal/svg.py

@ -167,6 +167,25 @@ class Svg(object):
extras[key1], extras[key2] = attr2, attr1
return self.node(parent, tag, attrib, **extras)
def serie(self, serie):
"""Make serie node"""
return dict(
plot=self.node(
self.graph.nodes['plot'],
class_='series serie-%d color-%d' % (
serie.index, serie.index % len(
self.graph.style['colors']))),
overlay=self.node(
self.graph.nodes['overlay'],
class_='series serie-%d color-%d' % (
serie.index, serie.index % len(
self.graph.style['colors']))),
text_overlay=self.node(
self.graph.nodes['text_overlay'],
class_='series serie-%d color-%d' % (
serie.index, serie.index % len(
self.graph.style['colors']))))
def line(self, node, coords, close=False, **kwargs):
"""Draw a svg line"""
line_len = len(coords)

3
pygal/util.py

@ -403,7 +403,8 @@ def prepare_values(raw, config, cls):
serie_config = SerieConfig()
serie_config(**config.to_dict())
serie_config(**serie_config_kwargs)
series.append(Serie(title, values, serie_config, metadata))
series.append(
Serie(len(series), title, values, serie_config, metadata))
return series

Loading…
Cancel
Save