Skip to content

Commit

Permalink
Multiline titles
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Oct 4, 2012
1 parent a9de30e commit b8b19bc
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
13 changes: 13 additions & 0 deletions demo/moulinrouge/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ def test_bar_links():
bar.zero = 1
return bar.render_response()

@app.route('/test/long_title')
def test_long_title():
bar = Bar()
bar.add('Lol', [2, None, 12])
bar.title = '123456789 ' * 30
return bar.render_response()

@app.route('/test/no_data')
def test_no_data():
bar = Bar()
bar.title = '123456789 ' * 30
return bar.render_response()

@app.route('/test/none')
def test_bar_none():
bar = Bar()
Expand Down
2 changes: 1 addition & 1 deletion pygal/css/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
*/

.title {
font-family: sans;
font-family: monospace;
font-size: {{ font_sizes.title }};
}

Expand Down
22 changes: 18 additions & 4 deletions pygal/graph/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
from pygal.util import (
get_text_box, get_texts_box, cut, rad, humanize, truncate)
from pygal.svg import Svg
from pygal.util import cached_property
from math import sin, cos, sqrt
from pygal.util import cached_property, reverse_text_len
from math import sin, cos, sqrt, ceil


class BaseGraph(object):
Expand Down Expand Up @@ -65,6 +65,19 @@ def __getattr__(self, attr):
return object.__getattribute__(self.config, attr)
return object.__getattribute__(self, attr)

def _split_title(self):
size = reverse_text_len(self.width, self.title_font_size)
title = self.title.strip()
self.title = []
while len(title) > size:
title_part = title[:size]
i = title_part.rfind(' ')
if i == -1:
i = len(title_part)
self.title.append(title_part[:i])
title = title[i:].strip()
self.title.append(title)

@property
def _format(self):
"""Return the value formatter for this graph"""
Expand All @@ -91,8 +104,8 @@ def _compute_margin(self):
self.margin.right += 10 + w + self.legend_box_size

if self.title:
h, w = get_text_box(self.title, self.title_font_size)
self.margin.top += 10 + h
h, _ = get_text_box(self.title[0], self.title_font_size)
self.margin.top += len(self.title) * (10 + h)

if self._x_labels:
h, w = get_texts_box(
Expand Down Expand Up @@ -148,6 +161,7 @@ def _order(self):
def _draw(self):
"""Draw all the things"""
self._compute()
self._split_title()
self._compute_margin()
self._decorate()
self._plot()
Expand Down
21 changes: 10 additions & 11 deletions pygal/graph/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def _x_axis(self, draw_axes=True):
available_space = (
last_label_position - first_label_position) / (
len(self._x_labels) - 1)
truncation = int(
reverse_text_len(available_space, self.label_font_size))
truncation = reverse_text_len(
available_space, self.label_font_size)

if 0 not in [label[1] for label in self._x_labels] and draw_axes:
self.svg.node(axis, 'path',
Expand Down Expand Up @@ -207,8 +207,8 @@ def _legend(self):
if not truncation:
available_space = self.view.width / cols - (
self.legend_box_size + 5)
truncation = int(reverse_text_len(
available_space, self.legend_font_size))
truncation = revesre_text_len(
available_space, self.legend_font_size)
else:
x = self.margin.left + self.view.width + 10
y = self.margin.top + 10
Expand Down Expand Up @@ -255,13 +255,12 @@ def _legend(self):
def _title(self):
"""Make the title"""
if self.title:
self.svg.node(
self.nodes['graph'], 'text', class_='title',
x=self.margin.left + self.view.width / 2,
y=self.title_font_size + 10
).text = truncate(
self.title, int(reverse_text_len(
self.width, self.title_font_size)))
for i, title_line in enumerate(self.title, 1):
self.svg.node(
self.nodes['graph'], 'text', class_='title',
x=self.margin.left + self.view.width / 2,
y=i * (self.title_font_size + 10)
).text = title_line

def _serie(self, serie):
"""Make serie node"""
Expand Down
2 changes: 1 addition & 1 deletion pygal/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def text_len(length, fs):

def reverse_text_len(width, fs):
"""Approximation of text length"""
return width / (0.6 * fs)
return int(width / (0.6 * fs))


def get_text_box(text, fs):
Expand Down

0 comments on commit b8b19bc

Please sign in to comment.