Skip to content

Commit

Permalink
Allow to inverse y axis, Fixes Kozea#24
Browse files Browse the repository at this point in the history
  • Loading branch information
Le-Stagiaire committed Mar 18, 2015
1 parent 41186be commit 4dedda5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
11 changes: 8 additions & 3 deletions demo/moulinrouge/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ class LolConfig(Config):

@app.route('/test/dateline')
def test_dateline():
from datetime import date
datey = DateLine(show_dots=False)
datey.add('1', [
(datetime(2013, 1, 2), 300),
Expand Down Expand Up @@ -427,8 +426,8 @@ def test_timexy():
(time(21, 2, 29), 10),
(time(12, 30, 59), 7)
])
datey.add('2',
[(time(12, 12, 12), 4), (time(), 8), (time(23, 59, 59), 6)])
datey.add(
'2', [(time(12, 12, 12), 4), (time(), 8), (time(23, 59, 59), 6)])
datey.x_label_rotation = 25
return datey.render_response()

Expand Down Expand Up @@ -624,4 +623,10 @@ def test_sparkline_for(chart):
graph.legend_at_bottom = True
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())))
2 changes: 2 additions & 0 deletions pygal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ class Config(CommonConfig):
False, bool, "Misc",
"Don't prefix css")

inverse_y_axis = Key(False, bool, "Misc", "Inverse Y axis direction")


class SerieConfig(CommonConfig):
"""Class holding serie config values"""
Expand Down
8 changes: 5 additions & 3 deletions pygal/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from __future__ import division
from pygal.interpolate import INTERPOLATIONS
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 (
truncate, reverse_text_len, get_texts_box, cut, rad, decorate)
from math import sqrt, ceil, cos
Expand Down Expand Up @@ -58,7 +58,7 @@ def _set_view(self):
else:
view_class = LogView
else:
view_class = View
view_class = ReverseView if self.inverse_y_axis else View

self.view = view_class(
self.width - self.margin.x,
Expand Down Expand Up @@ -211,7 +211,9 @@ def _y_axis(self):
self.show_y_guides):
self.svg.node(
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'
)

Expand Down
7 changes: 7 additions & 0 deletions pygal/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ def __call__(self, xy):
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):
def __init__(self, width, height, box):
self._force_vertical = None
Expand Down

0 comments on commit 4dedda5

Please sign in to comment.