From 8f42442524d0ee3bbe8fa285ae8136488cabde7a Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Mon, 18 Aug 2014 16:32:56 +0200 Subject: [PATCH] Add `stack_from_top` option to reverse stack graph data order. Fix #141 --- CHANGELOG | 3 +++ demo/moulinrouge/tests.py | 7 +++++++ pygal/config.py | 7 +++++++ pygal/graph/stackedbar.py | 6 ++++++ pygal/graph/stackedline.py | 6 ++++++ pygal/test/test_stacked.py | 9 +++++++++ 6 files changed, 38 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 8dc944f..1ccb7c4 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +V 1.5.1 - UNRELEASED + Add `stack_from_top` option to reverse stack graph data order + V 1.5.0 Add per serie configuration Add half pie (thanks philt2001) diff --git a/demo/moulinrouge/tests.py b/demo/moulinrouge/tests.py index 0804aa8..56a52eb 100644 --- a/demo/moulinrouge/tests.py +++ b/demo/moulinrouge/tests.py @@ -338,6 +338,13 @@ def get_test_routes(app): stacked.add('2', [4, 5, 6]) return stacked.render_response() + @app.route('/test/stacked/reverse') + def test_stacked_reverse(): + stacked = StackedBar(stack_from_top=True) + stacked.add('1', [1, 2, 3]) + stacked.add('2', [4, 5, 6]) + return stacked.render_response() + @app.route('/test/show_dots') def test_show_dots(): line = Line(show_dots=False) diff --git a/pygal/config.py b/pygal/config.py index 2e70f0e..bde5b79 100644 --- a/pygal/config.py +++ b/pygal/config.py @@ -224,6 +224,13 @@ class Config(CommonConfig): legend_box_size = Key( 12, int, "Look", "Size of legend boxes") + rounded_bars = Key( + None, int, "Look", "Set this to the desired radius in px") + + stack_from_top = Key( + False, bool, "Look", "Stack from top to zero, this makes the stacked " + "data match the legend order") + spacing = Key( 10, int, "Look", "Space between titles/legend/axes") diff --git a/pygal/graph/stackedbar.py b/pygal/graph/stackedbar.py index 2a17017..4502962 100644 --- a/pygal/graph/stackedbar.py +++ b/pygal/graph/stackedbar.py @@ -128,3 +128,9 @@ class StackedBar(Bar): class_='rect reactive tooltip-trigger') transpose = swap if self.horizontal else ident return transpose((x + width / 2, y + height / 2)) + + def _plot(self): + for serie in self.series[::-1 if self.stack_from_top else 1]: + self.bar(serie) + for serie in self.secondary_series[::-1 if self.stack_from_top else 1]: + self.bar(serie, True) diff --git a/pygal/graph/stackedline.py b/pygal/graph/stackedline.py index a11a98e..8350202 100644 --- a/pygal/graph/stackedline.py +++ b/pygal/graph/stackedline.py @@ -54,3 +54,9 @@ class StackedLine(Line): serie.interpolated = self._interpolate(x_pos, accumulation) else: serie.interpolated = [] + + def _plot(self): + for serie in self.series[::-1 if self.stack_from_top else 1]: + self.line(serie) + for serie in self.secondary_series[::-1 if self.stack_from_top else 1]: + self.line(serie, True) diff --git a/pygal/test/test_stacked.py b/pygal/test/test_stacked.py index d3063db..ba3b945 100644 --- a/pygal/test/test_stacked.py +++ b/pygal/test/test_stacked.py @@ -28,6 +28,15 @@ def test_stacked_line(): ('1', '2', '11', '14')) +def test_stacked_line_reverse(): + stacked = StackedLine(stack_from_top=True) + stacked.add('one_two', [1, 2]) + stacked.add('ten_twelve', [10, 12]) + q = stacked.render_pyquery() + assert set(q("desc.value").text().split(' ')) == set( + ('11', '14', '1', '2')) + + def test_stacked_line_log(): stacked = StackedLine(logarithmic=True) stacked.add('one_two', [1, 2])