Browse Source

Change js inclusion, starting to extract js lib

pull/8/head
Florian Mounier 13 years ago
parent
commit
365f7dbf0c
  1. 20
      pygal/config.py
  2. 4
      pygal/js/graph.coffee
  3. 4
      pygal/js/graph.js
  4. 10
      pygal/style.py
  5. 25
      pygal/svg.py

20
pygal/config.py

@ -21,7 +21,7 @@
Config module with all options Config module with all options
""" """
import os
from pygal.style import DefaultStyle from pygal.style import DefaultStyle
@ -43,7 +43,12 @@ class Config(object):
#: If set to a filename, this will replace the default css #: If set to a filename, this will replace the default css
base_css = None base_css = None
#: or default js #: or default js
base_js = None included_js = [os.path.join(os.path.dirname(__file__), 'js', 'graph.js')]
external_js = [
# 'http://code.jquery.com/jquery.min.js',
# 'http://keith-wood.name/js/jquery.svg.js',
# 'http://keith-wood.name/js/jquery.svgdom.js'
]
#: Style holding values injected in css #: Style holding values injected in css
style = DefaultStyle style = DefaultStyle
#: Various font sizes #: Various font sizes
@ -120,3 +125,14 @@ class Config(object):
('%dpx' % getattr(self, name) ('%dpx' % getattr(self, name)
) if with_unit else getattr(self, name)) ) if with_unit else getattr(self, name))
return fs return fs
def to_dict(self):
config = {}
for attr in dir(self):
if not attr.startswith('__'):
value = getattr(self, attr)
if hasattr(value, 'to_dict'):
config[attr] = value.to_dict()
elif not hasattr(value, '__call__'):
config[attr] = value
return config

4
pygal/js/graph.coffee

@ -2,8 +2,8 @@ _ = (x) -> document.querySelectorAll(x)
__ = (x) -> document.getElementById(x) __ = (x) -> document.getElementById(x)
padding = 5 padding = 5
tooltip_timeout = 0 tooltip_timeout = 0
tooltip_font_size = parseInt("{{ font_sizes.tooltip }}") tooltip_font_size = @config.tooltip_font_size
anim_steps = parseInt("{{ animation_steps }}") anim_steps = @config.animation_steps
class Queue class Queue
constructor: (@delay) -> constructor: (@delay) ->

4
pygal/js/graph.js

@ -15,9 +15,9 @@
tooltip_timeout = 0; tooltip_timeout = 0;
tooltip_font_size = parseInt("{{ font_sizes.tooltip }}"); tooltip_font_size = this.config.tooltip_font_size;
anim_steps = parseInt("{{ animation_steps }}"); anim_steps = this.config.animation_steps;
Queue = (function() { Queue = (function() {

10
pygal/style.py

@ -59,6 +59,16 @@ class Style(object):
'}}\n'.format(*tupl)) '}}\n'.format(*tupl))
return '\n'.join(map(color, enumerate(self._colors))) return '\n'.join(map(color, enumerate(self._colors)))
def to_dict(self):
config = {}
for attr in dir(self):
if not attr.startswith('__'):
value = getattr(self, attr)
if not hasattr(value, '__call__'):
config[attr] = value
return config
DefaultStyle = Style(opacity_hover='.4', opacity='.8') DefaultStyle = Style(opacity_hover='.4', opacity='.8')
LightStyle = Style( LightStyle = Style(
background='white', background='white',

25
pygal/svg.py

@ -24,6 +24,7 @@ Svg helper
from __future__ import division from __future__ import division
import io import io
import os import os
import json
from lxml import etree from lxml import etree
from math import cos, sin, pi from math import cos, sin, pi
from pygal.util import template, coord_format from pygal.util import template, coord_format
@ -64,16 +65,19 @@ class Svg(object):
hidden='y' if self.graph.horizontal else 'x') hidden='y' if self.graph.horizontal else 'x')
style.text = templ style.text = templ
def add_script(self, js): def add_scripts(self):
"""Add the js to the svg""" """Add the js to the svg"""
script = self.node(self.defs, 'script', type='text/javascript') common_script = self.node(self.defs, 'script', type='text/javascript')
with io.open(js, encoding='utf-8') as f: common_script.text = " = ".join(
templ = template( ("window.config", json.dumps(self.graph.config.to_dict())))
f.read(),
font_sizes=self.graph.font_sizes(False), for external_js in self.graph.external_js:
animation_steps=self.graph.animation_steps self.node(
) self.defs, 'script', type='text/javascript', href=external_js)
script.text = templ for included_js in self.graph.included_js:
script = self.node(self.defs, 'script', type='text/javascript')
with io.open(included_js, encoding='utf-8') as f:
script.text = f.read()
def node(self, parent=None, tag='g', attrib=None, **extras): def node(self, parent=None, tag='g', attrib=None, **extras):
"""Make a new svg node""" """Make a new svg node"""
@ -167,8 +171,7 @@ class Svg(object):
"""Last things to do before rendering""" """Last things to do before rendering"""
self.add_style(self.graph.base_css or os.path.join( self.add_style(self.graph.base_css or os.path.join(
os.path.dirname(__file__), 'css', 'graph.css')) os.path.dirname(__file__), 'css', 'graph.css'))
self.add_script(self.graph.base_js or os.path.join( self.add_scripts()
os.path.dirname(__file__), 'js', 'graph.js'))
self.root.set( self.root.set(
'viewBox', '0 0 %d %d' % (self.graph.width, self.graph.height)) 'viewBox', '0 0 %d %d' % (self.graph.width, self.graph.height))
if self.graph.explicit_size: if self.graph.explicit_size:

Loading…
Cancel
Save