Browse Source

Add legend_at_bottom_columns option to specify number of columns in legend when at bottom. Fixes #157

pull/185/head
Florian Mounier 10 years ago
parent
commit
60fb41b1d5
  1. 5
      demo/moulinrouge/tests.py
  2. 3
      pygal/config.py
  3. 7
      pygal/graph/base.py
  4. 3
      pygal/graph/graph.py

5
demo/moulinrouge/tests.py

@ -511,9 +511,10 @@ def get_test_routes(app):
@app.route('/test/half_pie') @app.route('/test/half_pie')
def test_half_pie(): def test_half_pie():
pie = Pie(half_pie=True) pie = Pie(half_pie=True)
for i in range(10): for i in range(100):
pie.add(str(i), i, inner_radius=.1) pie.add(str(i), i, inner_radius=.1)
pie.legend_at_bottom = True
pie.legend_at_bottom_columns = 4
return pie.render_response() return pie.render_response()
@app.route('/test/legend_at_bottom/<chart>') @app.route('/test/legend_at_bottom/<chart>')

3
pygal/config.py

@ -221,6 +221,9 @@ class Config(CommonConfig):
legend_at_bottom = Key( legend_at_bottom = Key(
False, bool, "Look", "Set to true to position legend at bottom") 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( legend_box_size = Key(
12, int, "Look", "Size of legend boxes") 12, int, "Look", "Size of legend boxes")

7
pygal/graph/base.py

@ -27,7 +27,7 @@ from pygal.util import (
get_text_box, get_texts_box, cut, rad, humanize, truncate, split_title) get_text_box, get_texts_box, cut, rad, humanize, truncate, split_title)
from pygal.svg import Svg from pygal.svg import Svg
from pygal.util import cached_property, majorize from pygal.util import cached_property, majorize
from math import sin, cos, sqrt from math import sin, cos, sqrt, ceil
class BaseGraph(object): class BaseGraph(object):
@ -93,8 +93,11 @@ class BaseGraph(object):
self.legend_font_size) self.legend_font_size)
if self.legend_at_bottom: if self.legend_at_bottom:
h_max = max(h, self.legend_box_size) 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( self.margin.bottom += self.spacing + h_max * round(
sqrt(self._order) - 1) * 1.5 + h_max cols - 1) * 1.5 + h_max
else: else:
if series_group is self.series: if series_group is self.series:
legend_width = self.spacing + w + self.legend_box_size legend_width = self.spacing + w + self.legend_box_size

3
pygal/graph/graph.py

@ -279,7 +279,8 @@ class Graph(BaseGraph):
y = (self.margin.top + self.view.height + y = (self.margin.top + self.view.height +
self._x_title_height + self._x_title_height +
self._x_labels_height + self.spacing) 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: if not truncation:
available_space = self.view.width / cols - ( available_space = self.view.width / cols - (

Loading…
Cancel
Save