Browse Source

Allow line to be horizontal. Fix #204

pull/222/merge
Florian Mounier 10 years ago
parent
commit
6fef3d2823
  1. 26
      demo/moulinrouge/tests.py
  2. 5
      pygal/graph/line.py
  3. 9
      pygal/svg.py
  4. 4
      pygal/util.py

26
demo/moulinrouge/tests.py

@ -9,6 +9,7 @@ from pygal import (
from pygal.style import styles, Style, RotateStyle from pygal.style import styles, Style, RotateStyle
from pygal.colors import rotate from pygal.colors import rotate
from pygal.graph.horizontal import HorizontalGraph
from pygal.graph.frenchmap import DEPARTMENTS, REGIONS from pygal.graph.frenchmap import DEPARTMENTS, REGIONS
from pygal.graph.swissmap import CANTONS from pygal.graph.swissmap import CANTONS
from random import randint, choice from random import randint, choice
@ -631,6 +632,31 @@ def get_test_routes(app):
graph.legend_at_bottom = True graph.legend_at_bottom = True
return graph.render_response() return graph.render_response()
@app.route('/test/normal/<chart>')
def test_normal_for(chart):
graph = CHARTS_BY_NAME[chart]()
graph.add('1', [1, 3, 12, 3, 4, None, 9])
graph.add('2', [7, -4, 10, None, 8, 3, 1])
graph.add('3', [7, -14, -10, None, 8, 3, 1])
graph.add('4', [7, 4, -10, None, 8, 3, 1])
graph.x_labels = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
graph.legend_at_bottom = True
return graph.render_response()
@app.route('/test/horizontal_force/<chart>')
def test_horizontal_force_for(chart):
class H(CHARTS_BY_NAME[chart], HorizontalGraph):
pass
graph = H()
graph.add('1', [1, 3, 12, 3, 4, None, 9])
graph.add('2', [7, -4, 10, None, 8, 3, 1])
graph.add('3', [7, -14, -10, None, 8, 3, 1])
graph.add('4', [7, 4, -10, None, 8, 3, 1])
graph.x_labels = ('a', 'b', 'c', 'd', 'e', 'f', 'g')
graph.legend_at_bottom = True
return graph.render_response()
@app.route('/test/inverse_y_axis/<chart>') @app.route('/test/inverse_y_axis/<chart>')
def test_inverse_y_axis_for(chart): def test_inverse_y_axis_for(chart):
graph = CHARTS_BY_NAME[chart](**dict(inverse_y_axis=True)) graph = CHARTS_BY_NAME[chart](**dict(inverse_y_axis=True))

5
pygal/graph/line.py

@ -109,8 +109,9 @@ class Line(Graph):
self.svg.node(serie_node['overlay'], class_="dots"), self.svg.node(serie_node['overlay'], class_="dots"),
metadata) metadata)
val = self._get_value(serie.points, i) val = self._get_value(serie.points, i)
self.svg.node(dots, 'circle', cx=x, cy=y, r=serie.dots_size, self.svg.transposable_node(
class_='dot reactive tooltip-trigger') dots, 'circle', cx=x, cy=y, r=serie.dots_size,
class_='dot reactive tooltip-trigger')
self._tooltip_data( self._tooltip_data(
dots, val, x, y) dots, val, x, y)
self._static_value( self._static_value(

9
pygal/svg.py

@ -30,7 +30,7 @@ import json
from datetime import date, datetime from datetime import date, datetime
from numbers import Number from numbers import Number
from math import cos, sin, pi from math import cos, sin, pi
from pygal.util import template, coord_format, minify_css from pygal.util import template, minify_css
from pygal import __version__ from pygal import __version__
@ -183,7 +183,7 @@ class Svg(object):
def transposable_node(self, parent=None, tag='g', attrib=None, **extras): def transposable_node(self, parent=None, tag='g', attrib=None, **extras):
"""Make a new svg node which can be transposed if horizontal""" """Make a new svg node which can be transposed if horizontal"""
if self.graph.horizontal: if self.graph.horizontal:
for key1, key2 in (('x', 'y'), ('width', 'height')): for key1, key2 in (('x', 'y'), ('width', 'height'), ('cx', 'cy')):
attr1 = extras.get(key1, None) attr1 = extras.get(key1, None)
attr2 = extras.get(key2, None) attr2 = extras.get(key2, None)
extras[key1], extras[key2] = attr2, attr1 extras[key1], extras[key2] = attr2, attr1
@ -219,6 +219,11 @@ class Svg(object):
origin_index += 1 origin_index += 1
if origin_index == line_len: if origin_index == line_len:
return return
if self.graph.horizontal:
coord_format = lambda xy: '%f %f' % (xy[1], xy[0])
else:
coord_format = lambda xy: '%f %f' % xy
origin = coord_format(coords[origin_index]) origin = coord_format(coords[origin_index])
line = ' '.join([coord_format(c) line = ' '.join([coord_format(c)
for c in coords[origin_index + 1:] for c in coords[origin_index + 1:]

4
pygal/util.py

@ -131,10 +131,6 @@ def template(string, **kwargs):
return _swap_curly(string).format(**kwargs) return _swap_curly(string).format(**kwargs)
def coord_format(xy):
"""Format x y coords to svg"""
return '%f %f' % xy
swap = lambda tuple_: tuple(reversed(tuple_)) swap = lambda tuple_: tuple(reversed(tuple_))
ident = lambda x: x ident = lambda x: x

Loading…
Cancel
Save