Browse Source

Allow to inverse y axis, Fixes #24

pull/207/head
Clément Plasse 10 years ago
parent
commit
4dedda50d2
  1. 11
      demo/moulinrouge/tests.py
  2. 2
      pygal/config.py
  3. 8
      pygal/graph/graph.py
  4. 7
      pygal/view.py

11
demo/moulinrouge/tests.py

@ -379,7 +379,6 @@ def get_test_routes(app):
@app.route('/test/dateline') @app.route('/test/dateline')
def test_dateline(): def test_dateline():
from datetime import date
datey = DateLine(show_dots=False) datey = DateLine(show_dots=False)
datey.add('1', [ datey.add('1', [
(datetime(2013, 1, 2), 300), (datetime(2013, 1, 2), 300),
@ -427,8 +426,8 @@ def get_test_routes(app):
(time(21, 2, 29), 10), (time(21, 2, 29), 10),
(time(12, 30, 59), 7) (time(12, 30, 59), 7)
]) ])
datey.add('2', datey.add(
[(time(12, 12, 12), 4), (time(), 8), (time(23, 59, 59), 6)]) '2', [(time(12, 12, 12), 4), (time(), 8), (time(23, 59, 59), 6)])
datey.x_label_rotation = 25 datey.x_label_rotation = 25
return datey.render_response() return datey.render_response()
@ -624,4 +623,10 @@ 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/inverse_y_axis/<chart>')
def test_inverse_y_axis(chart):
graph = CHARTS_BY_NAME[chart](**dict(inverse_y_axis=True))
graph.add('inverse', [1, 2, 3, 12, 24, 36])
return graph.render_response()
return list(sorted(filter(lambda x: x.startswith('test'), locals()))) return list(sorted(filter(lambda x: x.startswith('test'), locals())))

2
pygal/config.py

@ -446,6 +446,8 @@ class Config(CommonConfig):
False, bool, "Misc", False, bool, "Misc",
"Don't prefix css") "Don't prefix css")
inverse_y_axis = Key(False, bool, "Misc", "Inverse Y axis direction")
class SerieConfig(CommonConfig): class SerieConfig(CommonConfig):
"""Class holding serie config values""" """Class holding serie config values"""

8
pygal/graph/graph.py

@ -24,7 +24,7 @@ Commmon graphing functions
from __future__ import division from __future__ import division
from pygal.interpolate import INTERPOLATIONS from pygal.interpolate import INTERPOLATIONS
from pygal.graph.base import BaseGraph from pygal.graph.base import BaseGraph
from pygal.view import View, LogView, XYLogView from pygal.view import View, LogView, XYLogView, ReverseView
from pygal.util import ( from pygal.util import (
truncate, reverse_text_len, get_texts_box, cut, rad, decorate) truncate, reverse_text_len, get_texts_box, cut, rad, decorate)
from math import sqrt, ceil, cos from math import sqrt, ceil, cos
@ -58,7 +58,7 @@ class Graph(BaseGraph):
else: else:
view_class = LogView view_class = LogView
else: else:
view_class = View view_class = ReverseView if self.inverse_y_axis else View
self.view = view_class( self.view = view_class(
self.width - self.margin.x, self.width - self.margin.x,
@ -211,7 +211,9 @@ class Graph(BaseGraph):
self.show_y_guides): self.show_y_guides):
self.svg.node( self.svg.node(
axis, 'path', axis, 'path',
d='M%f %f h%f' % (0, self.view.height, self.view.width), d='M%f %f h%f' % (
0, 0 if self.inverse_y_axis else self.view.height,
self.view.width),
class_='line' class_='line'
) )

7
pygal/view.py

@ -155,6 +155,13 @@ class View(object):
return (self.x(x), self.y(y)) return (self.x(x), self.y(y))
class ReverseView(View):
def y(self, y):
if y is None:
return None
return (self.height * (y - self.box.ymin) / self.box.height)
class HorizontalView(View): class HorizontalView(View):
def __init__(self, width, height, box): def __init__(self, width, height, box):
self._force_vertical = None self._force_vertical = None

Loading…
Cancel
Save