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
"""
import os
from pygal.style import DefaultStyle
@ -43,7 +43,12 @@ class Config(object):
#: If set to a filename, this will replace the default css
base_css = None
#: 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 = DefaultStyle
#: Various font sizes
@ -120,3 +125,14 @@ class Config(object):
('%dpx' % getattr(self, name)
) if with_unit else getattr(self, name))
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)
padding = 5
tooltip_timeout = 0
tooltip_font_size = parseInt("{{ font_sizes.tooltip }}")
anim_steps = parseInt("{{ animation_steps }}")
tooltip_font_size = @config.tooltip_font_size
anim_steps = @config.animation_steps
class Queue
constructor: (@delay) ->

4
pygal/js/graph.js

@ -15,9 +15,9 @@
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() {

10
pygal/style.py

@ -59,6 +59,16 @@ class Style(object):
'}}\n'.format(*tupl))
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')
LightStyle = Style(
background='white',

25
pygal/svg.py

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

Loading…
Cancel
Save