Browse Source

Refactor some init

pull/8/head
Florian Mounier 12 years ago
parent
commit
bc08708c4c
  1. 5
      pygal/ghost.py
  2. 20
      pygal/graph/base.py
  3. 33
      pygal/svg.py

5
pygal/ghost.py

@ -61,8 +61,13 @@ class Ghost(object):
self.series.append( self.series.append(
Serie(title, values, len(self.series), self.cls.__value__)) Serie(title, values, len(self.series), self.cls.__value__))
def _check(self):
if self.config.logarithmic and self.config.zero == 0:
self.config.zero = 1
def make_instance(self): def make_instance(self):
self.config(**self.__dict__) self.config(**self.__dict__)
self._check()
self._last__inst = self.cls(self.config, self.series) self._last__inst = self.cls(self.config, self.series)
return self._last__inst return self._last__inst

20
pygal/graph/base.py

@ -44,18 +44,10 @@ class BaseGraph(object):
self.svg = Svg(self) self.svg = Svg(self)
self._x_labels = None self._x_labels = None
self._y_labels = None self._y_labels = None
self._box = None
self.nodes = {} self.nodes = {}
self.margin = None
self.view = None
def reinit(self):
"""(Re-)Init the graph"""
self.margin = Margin(*([20] * 4)) self.margin = Margin(*([20] * 4))
self._box = Box() self._box = Box()
if self.logarithmic and self.zero == 0: self.view = None
# If logarithmic, default zero to 1
self.zero = 1
def __getattr__(self, attr): def __getattr__(self, attr):
"""Search in config, then in self""" """Search in config, then in self"""
@ -79,12 +71,12 @@ class BaseGraph(object):
if self.show_legend: if self.show_legend:
h, w = get_texts_box( h, w = get_texts_box(
map(lambda x: truncate(x, self.truncate_legend or 15), map(lambda x: truncate(x, self.truncate_legend or 15),
cut(self.series, 'title')), cut(self.series, 'title')),
self.legend_font_size) self.legend_font_size)
if self.legend_at_bottom: if self.legend_at_bottom:
h_max = max(h, self.legend_box_size) h_max = max(h, self.legend_box_size)
self.margin.bottom += 10 + h_max * round( self.margin.bottom += 10 + h_max * round(
sqrt(len(self.series)) - 1) * 1.5 + h_max sqrt(len(self.series)) - 1) * 1.5 + h_max
else: else:
self.margin.right += 10 + w + self.legend_box_size self.margin.right += 10 + w + self.legend_box_size
@ -121,7 +113,7 @@ class BaseGraph(object):
return [val return [val
for serie in self.series for serie in self.series
for val in serie.values for val in serie.values
if val != None] if val is not None]
@cached_property @cached_property
def _len(self): def _len(self):
@ -170,13 +162,11 @@ class BaseGraph(object):
serie.metadata += diff * [self.__value__(0)] serie.metadata += diff * [self.__value__(0)]
for metadata in serie.metadata: for metadata in serie.metadata:
if metadata.value == None: if metadata.value is None:
metadata.value = 0 metadata.value = 0
def _render(self): def _render(self):
"""Make the graph internally""" """Make the graph internally"""
self.reinit()
self.svg.reinit()
if self._has_data(): if self._has_data():
self._draw() self._draw()
self.svg.pre_render(False) self.svg.pre_render(False)

33
pygal/svg.py

@ -25,6 +25,7 @@ from __future__ import division
import io import io
import os import os
import json import json
from datetime import date
from lxml import etree from lxml import etree
from math import cos, sin, pi from math import cos, sin, pi
from urlparse import urlparse from urlparse import urlparse
@ -40,19 +41,16 @@ class Svg(object):
self.graph = graph self.graph = graph
self.processing_instructions = [ self.processing_instructions = [
etree.PI(u'xml', u"version='1.0' encoding='utf-8'")] etree.PI(u'xml', u"version='1.0' encoding='utf-8'")]
self.root = None
self.defs = None
def reinit(self):
"""(Re-)initialization"""
self.root = etree.Element( self.root = etree.Element(
"{%s}svg" % self.ns, "{%s}svg" % self.ns,
nsmap={ nsmap={
None: self.ns, None: self.ns,
'xlink': 'http://www.w3.org/1999/xlink', 'xlink': 'http://www.w3.org/1999/xlink',
}) })
self.root.append(etree.Comment( self.root.append(
u'Generated with pygal %s ©Kozea 2012' % __version__)) etree.Comment(u'Generated with pygal %s ©Kozea 2012 on %s' % (
__version__, date.today().isoformat())))
self.root.append(etree.Comment(u'http://pygal.org'))
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')
@ -135,7 +133,8 @@ class Svg(object):
self.node(node, 'path', self.node(node, 'path',
d=root % (origin, line), **kwargs) d=root % (origin, line), **kwargs)
def slice(self, serie_node, node, radius, small_radius, def slice(
self, serie_node, node, radius, small_radius,
angle, start_angle, center, val): angle, start_angle, center, val):
"""Draw a pie slice""" """Draw a pie slice"""
project = lambda rho, alpha: ( project = lambda rho, alpha: (
@ -144,14 +143,14 @@ class Svg(object):
fmt = lambda x: '%f %f' % x fmt = lambda x: '%f %f' % x
get_radius = lambda r: fmt(tuple([r] * 2)) get_radius = lambda r: fmt(tuple([r] * 2))
absolute_project = lambda rho, theta: fmt( absolute_project = lambda rho, theta: fmt(
diff(center, project(rho, theta))) diff(center, project(rho, theta)))
if angle == 2 * pi: if angle == 2 * pi:
self.node(node, 'circle', self.node(node, 'circle',
cx=center[0], cx=center[0],
cy=center[1], cy=center[1],
r=radius, r=radius,
class_='slice reactive tooltip-trigger') class_='slice reactive tooltip-trigger')
else: else:
to = [absolute_project(radius, start_angle), to = [absolute_project(radius, start_angle),
absolute_project(radius, start_angle + angle), absolute_project(radius, start_angle + angle),
@ -165,7 +164,7 @@ class Svg(object):
get_radius(small_radius), int(angle > pi), to[3]), get_radius(small_radius), int(angle > pi), to[3]),
class_='slice reactive tooltip-trigger') class_='slice reactive tooltip-trigger')
x, y = diff(center, project( x, y = diff(center, project(
(radius + small_radius) / 2, start_angle + angle / 2)) (radius + small_radius) / 2, start_angle + angle / 2))
self.graph._tooltip_data(node, val, x, y, classes="centered") self.graph._tooltip_data(node, val, x, y, classes="centered")
self.graph._static_value(serie_node, val, x, y) self.graph._static_value(serie_node, val, x, y)
@ -181,9 +180,9 @@ class Svg(object):
self.root.set('height', str(self.graph.height)) 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
def render(self, is_unicode=False, pretty_print=False): def render(self, is_unicode=False, pretty_print=False):

Loading…
Cancel
Save