Browse Source

Change _bar return value to all bounds and extract _tooltip_and_print_values. Make histogram a subtype of Bar.

pull/293/head
Florian Mounier 9 years ago
parent
commit
f85991636c
  1. 9
      demo/moulinrouge/tests.py
  2. 5
      docs/changelog.rst
  3. 13
      pygal/config.py
  4. 28
      pygal/graph/bar.py
  5. 23
      pygal/graph/histogram.py
  6. 3
      pygal/graph/stackedbar.py

9
demo/moulinrouge/tests.py

@ -351,6 +351,15 @@ def get_test_routes(app):
bar.x_labels_major = [4]
return bar.render_response()
@app.route('/test/bar/position')
def test_bar_print_values_position():
bar = Bar(print_values=True, print_values_position='top')
bar.add('1', [1, 2, 3])
bar.add('2', [4, 5, 6])
bar.x_labels = [2, 4, 6]
bar.x_labels_major = [4]
return bar.render_response()
@app.route('/test/histogram')
def test_histogram():
hist = Histogram(style=styles['neon'])

5
docs/changelog.rst

@ -2,6 +2,11 @@
Changelog
=========
2.0.13 UNRELEASED
======
* Bar label positioning
2.0.12
======

13
pygal/config.py

@ -456,6 +456,15 @@ class Config(CommonConfig):
False, bool,
"Text", "Display values as text over plot")
dynamic_print_values = Key(
False, bool,
"Text", "Show values only on hover")
print_values_position = Key(
'center', str,
"Text", "Customize position of `print_values`. "
"(For bars: `top`, `center` or `bottom`)")
print_zeroes = Key(
True, bool,
"Text", "Display zero values as well")
@ -464,10 +473,6 @@ class Config(CommonConfig):
False, bool,
"Text", "Display value labels")
dynamic_print_values = Key(
False, bool,
"Text", "Show values only on hover")
truncate_legend = Key(
None, int, "Text",
"Legend string length truncation threshold",

28
pygal/graph/bar.py

@ -35,11 +35,6 @@ class Bar(Graph):
_series_margin = .06
_serie_margin = .06
def __init__(self, *args, **kwargs):
"""Bar chart creation"""
self._x_ranges = None
super(Bar, self).__init__(*args, **kwargs)
def _bar(self, serie, parent, x, y, i, zero, secondary=False):
"""Internal bar drawing function"""
width = (self.view.x(1) - self.view.x(0)) / self._len
@ -63,8 +58,18 @@ class Bar(Graph):
parent, 'rect',
x=x, y=y, rx=r, ry=r, width=width, height=height,
class_='rect reactive tooltip-trigger'), serie.metadata.get(i))
return x, y, width, height
def _tooltip_and_print_values(
self, serie_node, serie, parent, i, val, metadata,
x, y, width, height):
transpose = swap if self.horizontal else ident
return transpose((x + width / 2, y + height / 2))
x_center, y_center = transpose((x + width / 2, y + height / 2))
self._tooltip_data(
parent, val, x_center, y_center, "centered",
self._get_x_label(i))
self._static_value(
serie_node, val, x_center, y_center, metadata)
def bar(self, serie, rescale=False):
"""Draw a bar graph for a serie"""
@ -79,19 +84,18 @@ class Bar(Graph):
if None in (x, y) or (self.logarithmic and y <= 0):
continue
metadata = serie.metadata.get(i)
val = self._format(serie.values[i])
bar = decorate(
self.svg,
self.svg.node(bars, class_='bar'),
metadata)
val = self._format(serie.values[i])
x_center, y_center = self._bar(
bounds = self._bar(
serie, bar, x, y, i, self.zero, secondary=rescale)
self._tooltip_data(
bar, val, x_center, y_center, "centered",
self._get_x_label(i))
self._static_value(serie_node, val, x_center, y_center, metadata)
self._tooltip_and_print_values(
serie_node, serie, bar, i, val, metadata, *bounds)
def _compute(self):
"""Compute y min and max and y scale and set labels"""

23
pygal/graph/histogram.py

@ -24,10 +24,11 @@ as bars of varying width.
from __future__ import division
from pygal.graph.dual import Dual
from pygal.util import alter, cached_property, decorate, ident, swap
from pygal.graph.bar import Bar
from pygal.util import alter, cached_property, decorate
class Histogram(Dual):
class Histogram(Dual, Bar):
"""Histogram chart class"""
_series_margin = 0
@ -72,8 +73,7 @@ class Histogram(Dual):
parent, 'rect',
x=x, y=y, rx=r, ry=r, width=width, height=height,
class_='rect reactive tooltip-trigger'), serie.metadata.get(i))
transpose = swap if self.horizontal else ident
return transpose((x + width / 2, y + height / 2))
return x, y, width, height
def bar(self, serie, rescale=False):
"""Draw a bar graph for a serie"""
@ -92,12 +92,10 @@ class Histogram(Dual):
metadata)
val = self._format(serie.values[i][0])
x_center, y_center = self._bar(
bounds = self._bar(
serie, bar, x0, x1, y, i, self.zero, secondary=rescale)
self._tooltip_data(
bar, val, x_center, y_center, "centered",
self._get_x_label(i))
self._static_value(serie_node, val, x_center, y_center, metadata)
self._tooltip_and_print_values(
serie_node, serie, bar, i, val, metadata, *bounds)
def _compute(self):
"""Compute x/y min and max and x/y scale and set labels"""
@ -122,10 +120,3 @@ class Histogram(Dual):
self._box.xmin, self._box.xmax = xmin, xmax
if yrng:
self._box.ymin, self._box.ymax = ymin, ymax
def _plot(self):
"""Draw bars for series and secondary series"""
for serie in self.series:
self.bar(serie)
for serie in self.secondary_series:
self.bar(serie, True)

3
pygal/graph/stackedbar.py

@ -131,8 +131,7 @@ class StackedBar(Bar):
parent, 'rect',
x=x, y=y, rx=r, ry=r, width=width, height=height,
class_='rect reactive tooltip-trigger')
transpose = swap if self.horizontal else ident
return transpose((x + width / 2, y + height / 2))
return x, y, width, height
def _plot(self):
"""Draw bars for series and secondary series"""

Loading…
Cancel
Save