From deca835d6cf96dcb752905c864430deb85168074 Mon Sep 17 00:00:00 2001 From: Florian Mounier Date: Mon, 19 Mar 2012 11:16:34 +0100 Subject: [PATCH] Add some options and allow config after init --- pygal/__init__.py | 2 +- pygal/config.py | 5 +++++ pygal/graph/base.py | 6 ++++-- pygal/graph/horizontal.py | 2 ++ pygal/svg.py | 24 +++++++++++++++--------- setup.py | 2 +- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/pygal/__init__.py b/pygal/__init__.py index dca6d8a..0a89ccd 100644 --- a/pygal/__init__.py +++ b/pygal/__init__.py @@ -16,7 +16,7 @@ # # You should have received a copy of the GNU Lesser General Public License # along with pygal. If not, see . -__version__ = '0.9.12' +__version__ = '0.9.13-dev' from collections import namedtuple from pygal.graph.bar import Bar diff --git a/pygal/config.py b/pygal/config.py index e341143..dda845f 100644 --- a/pygal/config.py +++ b/pygal/config.py @@ -90,6 +90,11 @@ class Config(object): print_zeroes = False # Animate tooltip steps (0 disable animation) animation_steps = 0 + # Don't write xml declaration and return unicode instead of string + # (usefull for writing output in html) + disable_xml_declaration = False + # Write width and height attributes + explicit_size = False def __init__(self, **kwargs): """Can be instanciated with config kwargs""" diff --git a/pygal/graph/base.py b/pygal/graph/base.py index 8b1869d..7753f3e 100644 --- a/pygal/graph/base.py +++ b/pygal/graph/base.py @@ -133,9 +133,11 @@ class BaseGraph(object): def validate(self): if self.x_labels: - assert len(self.series[0].values) == len(self.x_labels) + assert len(self.series[0].values) == len(self.x_labels), ( + 'Labels and series must have the same length') for serie in self.series: - assert len(self.series[0].values) == len(serie.values) + assert len(self.series[0].values) == len(serie.values), ( + 'All series must have the same length') def _in_browser(self): from lxml.html import open_in_browser diff --git a/pygal/graph/horizontal.py b/pygal/graph/horizontal.py index 483e5e6..6bf23db 100644 --- a/pygal/graph/horizontal.py +++ b/pygal/graph/horizontal.py @@ -28,6 +28,8 @@ class HorizontalGraph(Graph): super(HorizontalGraph, self).__init__(*args, **kwargs) def _compute(self): + if self.x_labels: + self.x_labels = reversed(self.x_labels) super(HorizontalGraph, self)._compute() self._x_labels, self._y_labels = self._y_labels, self._x_labels self._box.swap() diff --git a/pygal/svg.py b/pygal/svg.py index 90320f1..a88e984 100644 --- a/pygal/svg.py +++ b/pygal/svg.py @@ -29,9 +29,6 @@ class Svg(object): self.graph = graph self.root = etree.Element( "{%s}svg" % self.ns, - attrib={ - 'viewBox': '0 0 %d %d' % (self.graph.width, self.graph.height) - }, nsmap={ None: self.ns, 'xlink': 'http://www.w3.org/1999/xlink', @@ -40,10 +37,6 @@ class Svg(object): self.root.append(etree.Comment(u'Generated with pygal ©Kozea 2012')) self.root.append(etree.Comment(u'http://github.com/Kozea/pygal')) self.defs = self.node(tag='defs') - self.add_style(self.graph.base_css or os.path.join( - os.path.dirname(__file__), 'css', 'graph.css')) - self.add_script(self.graph.base_js or os.path.join( - os.path.dirname(__file__), 'js', 'graph.js')) def add_style(self, css): style = self.node(self.defs, 'style', type='text/css') @@ -99,12 +92,25 @@ class Svg(object): d=root % (origin, line), **kwargs) def render(self, no_data=False): + self.add_style(self.graph.base_css or os.path.join( + os.path.dirname(__file__), 'css', 'graph.css')) + self.add_script(self.graph.base_js or os.path.join( + os.path.dirname(__file__), 'js', 'graph.js')) + self.root.set( + 'viewBox', '0 0 %d %d' % (self.graph.width, self.graph.height)) + if self.graph.explicit_size: + self.root.set('width', str(self.graph.width)) + self.root.set('height', str(self.graph.height)) if no_data: no_data = self.node(self.root, 'text', x=self.graph.width / 2, y=self.graph.height / 2, class_='no_data') no_data.text = self.graph.no_data_text - return etree.tostring( + svg = etree.tostring( self.root, pretty_print=True, - xml_declaration=True, encoding='utf-8') + xml_declaration=not self.graph.disable_xml_declaration, + encoding='utf-8') + if self.graph.disable_xml_declaration: + svg = svg.decode('utf-8') + return svg diff --git a/setup.py b/setup.py index 2e51d59..25c1049 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,7 @@ import pygal setup( name="pygal", - version=pygal.__version__, + version=pygal.__version__.replace('-dev', ''), description="A python svg graph plotting library", author="Kozea", author_email="florian.mounier@kozea.fr",