Browse Source

Add some options and allow config after init

pull/8/head
Florian Mounier 13 years ago
parent
commit
deca835d6c
  1. 2
      pygal/__init__.py
  2. 5
      pygal/config.py
  3. 6
      pygal/graph/base.py
  4. 2
      pygal/graph/horizontal.py
  5. 24
      pygal/svg.py
  6. 2
      setup.py

2
pygal/__init__.py

@ -16,7 +16,7 @@
# #
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with pygal. If not, see <http://www.gnu.org/licenses/>. # along with pygal. If not, see <http://www.gnu.org/licenses/>.
__version__ = '0.9.12' __version__ = '0.9.13-dev'
from collections import namedtuple from collections import namedtuple
from pygal.graph.bar import Bar from pygal.graph.bar import Bar

5
pygal/config.py

@ -90,6 +90,11 @@ class Config(object):
print_zeroes = False print_zeroes = False
# Animate tooltip steps (0 disable animation) # Animate tooltip steps (0 disable animation)
animation_steps = 0 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): def __init__(self, **kwargs):
"""Can be instanciated with config kwargs""" """Can be instanciated with config kwargs"""

6
pygal/graph/base.py

@ -133,9 +133,11 @@ class BaseGraph(object):
def validate(self): def validate(self):
if self.x_labels: 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: 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): def _in_browser(self):
from lxml.html import open_in_browser from lxml.html import open_in_browser

2
pygal/graph/horizontal.py

@ -28,6 +28,8 @@ class HorizontalGraph(Graph):
super(HorizontalGraph, self).__init__(*args, **kwargs) super(HorizontalGraph, self).__init__(*args, **kwargs)
def _compute(self): def _compute(self):
if self.x_labels:
self.x_labels = reversed(self.x_labels)
super(HorizontalGraph, self)._compute() super(HorizontalGraph, self)._compute()
self._x_labels, self._y_labels = self._y_labels, self._x_labels self._x_labels, self._y_labels = self._y_labels, self._x_labels
self._box.swap() self._box.swap()

24
pygal/svg.py

@ -29,9 +29,6 @@ class Svg(object):
self.graph = graph self.graph = graph
self.root = etree.Element( self.root = etree.Element(
"{%s}svg" % self.ns, "{%s}svg" % self.ns,
attrib={
'viewBox': '0 0 %d %d' % (self.graph.width, self.graph.height)
},
nsmap={ nsmap={
None: self.ns, None: self.ns,
'xlink': 'http://www.w3.org/1999/xlink', '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'Generated with pygal ©Kozea 2012'))
self.root.append(etree.Comment(u'http://github.com/Kozea/pygal')) self.root.append(etree.Comment(u'http://github.com/Kozea/pygal'))
self.defs = self.node(tag='defs') 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): def add_style(self, css):
style = self.node(self.defs, 'style', type='text/css') style = self.node(self.defs, 'style', type='text/css')
@ -99,12 +92,25 @@ class Svg(object):
d=root % (origin, line), **kwargs) d=root % (origin, line), **kwargs)
def render(self, no_data=False): 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: if no_data:
no_data = self.node(self.root, 'text', no_data = self.node(self.root, 'text',
x=self.graph.width / 2, x=self.graph.width / 2,
y=self.graph.height / 2, y=self.graph.height / 2,
class_='no_data') class_='no_data')
no_data.text = self.graph.no_data_text no_data.text = self.graph.no_data_text
return etree.tostring( svg = etree.tostring(
self.root, pretty_print=True, 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

2
setup.py

@ -22,7 +22,7 @@ import pygal
setup( setup(
name="pygal", name="pygal",
version=pygal.__version__, version=pygal.__version__.replace('-dev', ''),
description="A python svg graph plotting library", description="A python svg graph plotting library",
author="Kozea", author="Kozea",
author_email="florian.mounier@kozea.fr", author_email="florian.mounier@kozea.fr",

Loading…
Cancel
Save