Skip to content

Commit

Permalink
Add legend_at_bottom_columns option to specify number of columns in l…
Browse files Browse the repository at this point in the history
…egend when at bottom. Fixes #157
  • Loading branch information
paradoxxxzero committed Nov 21, 2014
1 parent 70449fb commit 60fb41b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions demo/moulinrouge/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,10 @@ def test_pie_serie_radius():
@app.route('/test/half_pie')
def test_half_pie():
pie = Pie(half_pie=True)
for i in range(10):
for i in range(100):
pie.add(str(i), i, inner_radius=.1)

pie.legend_at_bottom = True
pie.legend_at_bottom_columns = 4
return pie.render_response()

@app.route('/test/legend_at_bottom/<chart>')
Expand Down
3 changes: 3 additions & 0 deletions pygal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ class Config(CommonConfig):
legend_at_bottom = Key(
False, bool, "Look", "Set to true to position legend at bottom")

legend_at_bottom_columns = Key(
None, int, "Look", "Set to true to position legend at bottom")

legend_box_size = Key(
12, int, "Look", "Size of legend boxes")

Expand Down
7 changes: 5 additions & 2 deletions pygal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
get_text_box, get_texts_box, cut, rad, humanize, truncate, split_title)
from pygal.svg import Svg
from pygal.util import cached_property, majorize
from math import sin, cos, sqrt
from math import sin, cos, sqrt, ceil


class BaseGraph(object):
Expand Down Expand Up @@ -93,8 +93,11 @@ def _compute_margin(self):
self.legend_font_size)
if self.legend_at_bottom:
h_max = max(h, self.legend_box_size)
cols = (self._order // self.legend_at_bottom_columns
if self.legend_at_bottom_columns
else ceil(sqrt(self._order)) or 1)
self.margin.bottom += self.spacing + h_max * round(
sqrt(self._order) - 1) * 1.5 + h_max
cols - 1) * 1.5 + h_max
else:
if series_group is self.series:
legend_width = self.spacing + w + self.legend_box_size
Expand Down
3 changes: 2 additions & 1 deletion pygal/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ def _legend(self):
y = (self.margin.top + self.view.height +
self._x_title_height +
self._x_labels_height + self.spacing)
cols = ceil(sqrt(self._order)) or 1
cols = self.legend_at_bottom_columns or ceil(
sqrt(self._order)) or 1

if not truncation:
available_space = self.view.width / cols - (
Expand Down

0 comments on commit 60fb41b

Please sign in to comment.