From d42500603730828b1bf0131314808cf4379d464b Mon Sep 17 00:00:00 2001 From: Christian Ledermann Date: Thu, 12 Sep 2013 17:22:42 +0300 Subject: [PATCH] add tests --- pygal/graph/graph.py | 17 +++++++++--- pygal/test/test_donut.py | 57 ++++++++++++++++++++++++++++++++++++++++ pygal/test/test_graph.py | 43 ++++++++++++++++++++++++++++++ pygal/util.py | 8 +++++- 4 files changed, 120 insertions(+), 5 deletions(-) create mode 100644 pygal/test/test_donut.py diff --git a/pygal/graph/graph.py b/pygal/graph/graph.py index de4e779..401723e 100644 --- a/pygal/graph/graph.py +++ b/pygal/graph/graph.py @@ -178,9 +178,15 @@ class Graph(BaseGraph): y=y, class_='major' if major else '' ) - text.text = truncate(label, truncation) + if isinstance(label, dict): + text.text = truncate(label['title'], truncation) + else: + text.text = truncate(label, truncation) if text.text != label: - self.svg.node(guides, 'title').text = label + if isinstance(label, dict): + self.svg.node(guides, 'title').text = label['title'] + else: + self.svg.node(guides, 'title').text = label if self.x_label_rotation: text.attrib['transform'] = "rotate(%d %f %f)" % ( self.x_label_rotation, x, y) @@ -245,7 +251,10 @@ class Graph(BaseGraph): y=y + .35 * self.label_font_size, class_='major' if major else '' ) - text.text = label + if isinstance(label, dict): + text.text = label['title'] + else: + text.text = label if self.y_label_rotation: text.attrib['transform'] = "rotate(%d %f %f)" % ( self.y_label_rotation, x, y) @@ -352,7 +361,7 @@ class Graph(BaseGraph): if isinstance(title, dict): truncated = truncate(title['title'], truncation) a = decorate(self.svg, legend, title) - legend_ = self.svg.node( + self.svg.node( a, 'text', x=col * x_step + self.legend_box_size + 5, y=1.5 * row * h diff --git a/pygal/test/test_donut.py b/pygal/test/test_donut.py new file mode 100644 index 0000000..00f973a --- /dev/null +++ b/pygal/test/test_donut.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# This file is part of pygal +# +# A python svg graph plotting library +# Copyright © 2012-2013 Kozea +# +# This library is free software: you can redistribute it and/or modify it under +# the terms of the GNU Lesser General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) any +# later version. +# +# This library is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +# details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with pygal. If not, see . +import os +import uuid +from pygal import Pie + +def test_donut(): + file_name = '/tmp/test_graph-%s.svg' % uuid.uuid4() + if os.path.exists(file_name): + os.remove(file_name) + chart = Pie(inner_radius=.3, pretty_print=True) + chart.title = 'Browser usage in February 2012 (in %)' + chart.add('IE', 19.5) + chart.add('Firefox', 36.6) + chart.add('Chrome', 36.3) + chart.add('Safari', 4.5) + chart.add('Opera', 2.3) + chart.render_to_file(file_name) + with open(file_name) as f: + assert 'pygal' in f.read() + os.remove(file_name) + +def test_multiseries_donut(): + #this just demos that the multiseries pie does not respect the inner_radius + file_name = '/tmp/test_graph-%s.svg' % uuid.uuid4() + if os.path.exists(file_name): + os.remove(file_name) + chart = Pie(inner_radius=.3, pretty_print=True) + chart.title = 'Browser usage by version in February 2012 (in %)' + chart.add('IE', [5.7, 10.2, 2.6, 1]) + chart.add('Firefox', [.6, 16.8, 7.4, 2.2, 1.2, 1, 1, 1.1, 4.3, 1]) + chart.add('Chrome', [.3, .9, 17.1, 15.3, .6, .5, 1.6]) + chart.add('Safari', [4.4, .1]) + chart.add('Opera', [.1, 1.6, .1, .5]) + chart.render_to_file(file_name) + with open(file_name) as f: + assert 'pygal' in f.read() + os.remove(file_name) + + + diff --git a/pygal/test/test_graph.py b/pygal/test/test_graph.py index 0040526..0ae11b9 100644 --- a/pygal/test/test_graph.py +++ b/pygal/test/test_graph.py @@ -284,3 +284,46 @@ def test_unicode_labels_python3(Chart): }]) chart.x_labels = eval("['&œ', '¿?', '††††††††', 'unicode <3']") q = chart.render_pyquery() + +def test_labels_with_links(Chart): + chart = Chart() + #link on chart and label + chart.add({'title':'Red','xlink': + {'href': 'http://en.wikipedia.org/wiki/Red'}}, + [{ + 'value': 2, + 'label': 'This is red', + 'xlink': {'href': 'http://en.wikipedia.org/wiki/Red'}}]) + #link on chart only + chart.add('Green', [{ + 'value': 4, + 'label': 'This is green', + 'xlink': { + 'href': 'http://en.wikipedia.org/wiki/Green', + 'target': '_top'} + }]) + #link on label only opens in new tab + chart.add({'title': 'Yellow', 'xlink': { + 'href': 'http://en.wikipedia.org/wiki/Yellow', + 'target': '_blank'}}, 7) + #link on chart only + chart.add('Blue', [{ + 'value': 5, + 'xlink': { + 'href': 'http://en.wikipedia.org/wiki/Blue', + 'target': '_blank'} + }]) + #link on label and chart with diffrent behaviours + chart.add({'title': 'Violet', + 'xlink': 'http://en.wikipedia.org/wiki/Violet_(color)'}, + [{ + 'value': 3, + 'label': 'This is violet', + 'xlink': { + 'href': 'http://en.wikipedia.org/wiki/Violet_(color)', + 'target': '_self'} + }]) + q = chart.render_pyquery() + links = q('a') + assert(len(links) == 8) # 7 links and 1 tooltip + diff --git a/pygal/util.py b/pygal/util.py index 0f82fa6..a42d8dc 100644 --- a/pygal/util.py +++ b/pygal/util.py @@ -197,7 +197,13 @@ def get_text_box(text, fs): def get_texts_box(texts, fs): """Approximation of multiple texts bounds""" - max_len = max(map(len, texts)) + def get_text_title(texts): + for text in texts: + if isinstance(text, dict): + yield text['title'] + else: + yield text + max_len = max(map(len, get_text_title(texts))) return (fs, text_len(max_len, fs))