Browse Source

Multiline titles

pull/8/head
Florian Mounier 12 years ago
parent
commit
b8b19bc063
  1. 13
      demo/moulinrouge/tests.py
  2. 2
      pygal/css/base.css
  3. 22
      pygal/graph/base.py
  4. 21
      pygal/graph/graph.py
  5. 2
      pygal/util.py

13
demo/moulinrouge/tests.py

@ -49,6 +49,19 @@ def get_test_routes(app):
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()

2
pygal/css/base.css

@ -23,7 +23,7 @@
*/
.title {
font-family: sans;
font-family: monospace;
font-size: {{ font_sizes.title }};
}

22
pygal/graph/base.py

@ -26,8 +26,8 @@ from pygal.view import Margin, Box
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):
@ -65,6 +65,19 @@ class BaseGraph(object):
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"""
@ -91,8 +104,8 @@ class BaseGraph(object):
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(
@ -148,6 +161,7 @@ class BaseGraph(object):
def _draw(self):
"""Draw all the things"""
self._compute()
self._split_title()
self._compute_margin()
self._decorate()
self._plot()

21
pygal/graph/graph.py

@ -122,8 +122,8 @@ class Graph(BaseGraph):
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',
@ -207,8 +207,8 @@ class Graph(BaseGraph):
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
@ -255,13 +255,12 @@ class Graph(BaseGraph):
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"""

2
pygal/util.py

@ -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):

Loading…
Cancel
Save