Browse Source

Update other graphs

pull/8/head
Florian Mounier 13 years ago
parent
commit
5c2f1ea08e
  1. 14
      pygal/graph/bar.py
  2. 3
      pygal/graph/line.py
  3. 24
      pygal/graph/pie.py
  4. 15
      pygal/js/graph.coffee
  5. 43
      pygal/js/graph.js

14
pygal/graph/bar.py

@ -36,8 +36,8 @@ class Bar(Graph):
bars = self.svg.node(serie_node['plot'], class_="bars") bars = self.svg.node(serie_node['plot'], class_="bars")
view_values = map(view, values) view_values = map(view, values)
for i, ((x, y), (X, Y)) in enumerate(view_values): for i, ((x, y), (X, Y)) in enumerate(view_values):
tag = '%d_%d' % (serie.index, i)
# x and y are left range coords and X, Y right ones # x and y are left range coords and X, Y right ones
val = self.format(values[i][1][1])
if self._horizontal: if self._horizontal:
x, y, X, Y = Y, X, y, x x, y, X, Y = Y, X, y, x
width = X - x width = X - x
@ -73,20 +73,18 @@ class Bar(Graph):
ry=self.rounded_bars * 1, ry=self.rounded_bars * 1,
width=bar_inner_width, width=bar_inner_width,
height=height, height=height,
id="active-%s" % tag, class_='rect reactive tooltip-trigger')
class_='rect reactive') self.svg.node(bar, 'desc').text = val
if self._horizontal: if self._horizontal:
x += .3 * self.value_font_size x += .3 * self.value_font_size
y += height / 2 y += height / 2
else: else:
y += height / 2 + .3 * self.value_font_size y += height / 2 + .3 * self.value_font_size
self.svg.transposable_node( self.svg.transposable_node(
serie_node['overlay'], 'text', serie_node['text_overlay'], 'text',
x=x + bar_inner_width / 2, x=x + bar_inner_width / 2,
y=y - shift, y=y - shift
id="reactive-%s" % tag, ).text = val
class_='reactive-text'
).text = self.format(values[i][1][1])
return stack_vals return stack_vals
def _compute(self): def _compute(self):

3
pygal/graph/line.py

@ -44,10 +44,9 @@ class Line(Graph):
def line(self, serie_node, serie): def line(self, serie_node, serie):
view_values = map(self.view, serie.points) view_values = map(self.view, serie.points)
if self.show_dots: if self.show_dots:
dots = self.svg.node(serie_node['overlay'], class_="dots")
for i, (x, y) in enumerate(view_values): for i, (x, y) in enumerate(view_values):
dots = self.svg.node(serie_node['overlay'], class_="dots")
val = self._get_value(serie.points, i) val = self._get_value(serie.points, i)
self.svg.node(dots, 'circle', cx=x, cy=y, r=2.5, self.svg.node(dots, 'circle', cx=x, cy=y, r=2.5,
class_='dot reactive tooltip-trigger') class_='dot reactive tooltip-trigger')

24
pygal/graph/pie.py

@ -23,8 +23,9 @@ from math import cos, sin, pi
class Pie(Graph): class Pie(Graph):
"""Pie graph""" """Pie graph"""
def slice(self, serie_node, start_angle, angle, perc, tag, def slice(self, serie_node, start_angle, angle, perc,
small=False): small=False):
val = '{:.2%}'.format(perc)
slices = self.svg.node(serie_node['plot'], class_="slices") slices = self.svg.node(serie_node['plot'], class_="slices")
slice_ = self.svg.node(slices, class_="slice") slice_ = self.svg.node(slices, class_="slice")
center = ((self.width - self.margin.x) / 2., center = ((self.width - self.margin.x) / 2.,
@ -38,8 +39,7 @@ class Pie(Graph):
cx=center[0], cx=center[0],
cy=center[1], cy=center[1],
r=r, r=r,
id="active-%s" % tag, class_='slice reactive tooltip-trigger')
class_='slice reactive')
else: else:
rxy = '%f %f' % tuple([r] * 2) rxy = '%f %f' % tuple([r] * 2)
to = '%f %f' % (r * sin(angle), r * (1 - cos(angle))) to = '%f %f' % (r * sin(angle), r * (1 - cos(angle)))
@ -49,18 +49,16 @@ class Pie(Graph):
rxy, rxy,
1 if angle > pi else 0, 1 if angle > pi else 0,
to), to),
id="active-%s" % tag,
transform='rotate(%f %s)' % ( transform='rotate(%f %s)' % (
start_angle * 180 / pi, center_str), start_angle * 180 / pi, center_str),
class_='slice reactive') class_='slice reactive tooltip-trigger')
self.svg.node(slice_, 'desc').text = val
text_angle = pi / 2. - (start_angle + angle / 2.) text_angle = pi / 2. - (start_angle + angle / 2.)
text_r = r * .8 text_r = r * .95
self.svg.node(serie_node['overlay'], 'text', self.svg.node(serie_node['text_overlay'], 'text',
x=center[0] + text_r * cos(text_angle), x=center[0] + text_r * cos(text_angle),
y=center[1] - text_r * sin(text_angle), y=center[1] - text_r * sin(text_angle)
id="reactive-%s" % tag, ).text = val
class_='reactive-text'
).text = '{:.2%}'.format(perc)
def _compute(self): def _compute(self):
for serie in self.series: for serie in self.series:
@ -78,8 +76,7 @@ class Pie(Graph):
self.slice( self.slice(
self._serie(serie.index), self._serie(serie.index),
current_angle, current_angle,
angle, sum(serie.values) / total, angle, sum(serie.values) / total)
'%d' % serie.index)
if len(serie.values) > 1: if len(serie.values) > 1:
small_current_angle = current_angle small_current_angle = current_angle
for i, val in enumerate(serie.values): for i, val in enumerate(serie.values):
@ -88,7 +85,6 @@ class Pie(Graph):
self._serie(serie.index), self._serie(serie.index),
small_current_angle, small_current_angle,
small_angle, val / total, small_angle, val / total,
'%d_%d' % (serie.index, i),
True) True)
small_current_angle += small_angle small_current_angle += small_angle
current_angle += angle current_angle += angle

15
pygal/js/graph.coffee

@ -32,11 +32,7 @@ deactivate = (elements...) ->
Function.prototype.bind = (scope) -> Function.prototype.bind = (scope) ->
_fun = @ _fun = @
-> -> _fun.apply(scope, arguments)
_fun.apply(scope, arguments)
reactive = (element) -> document.getElementById('re' + element.id)
active = (element) -> document.getElementById(element.id.replace(/re/, ''))
hover = (elts, over, out) -> hover = (elts, over, out) ->
for elt in elts for elt in elts
@ -70,17 +66,18 @@ untooltip = ->
@svg_load = -> @svg_load = ->
for text in _('.text-overlay .series') for text in _('.text-overlay .series')
text.setAttribute('display', 'none') text.setAttribute('display', 'none')
hover _('.reactive-text'), (-> activate(@, active(@))), (-> deactivate(@, active(@))) hover _('.reactive'), (-> activate(@)), (-> deactivate(@))
hover _('.reactive'), (-> activate(@, reactive(@))), (-> deactivate(@, reactive(@)))
hover _('.activate-serie'), ( hover _('.activate-serie'), (
-> ->
num = this.id.replace('activate-serie-', '') num = this.id.replace('activate-serie-', '')
_('.text-overlay .serie-' + num)[0].setAttribute('display', 'inline') for element in _('.text-overlay .serie-' + num)
element.setAttribute('display', 'inline')
for element in _('.serie-' + num + ' .reactive') for element in _('.serie-' + num + ' .reactive')
activate(element, reactive(element))), ( activate(element, reactive(element))), (
-> ->
num = this.id.replace('activate-serie-', '') num = this.id.replace('activate-serie-', '')
_('.text-overlay .serie-' + num)[0].setAttribute('display', 'none') for element in _('.text-overlay .serie-' + num)
element.setAttribute('display', 'none')
for element in _('.serie-' + num + ' .reactive') for element in _('.serie-' + num + ' .reactive')
deactivate(element, reactive(element))) deactivate(element, reactive(element)))
hover _('.tooltip-trigger'), (-> tooltip(@)), (-> untooltip()) hover _('.tooltip-trigger'), (-> tooltip(@)), (-> untooltip())

43
pygal/js/graph.js

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.2.1-pre // Generated by CoffeeScript 1.2.1-pre
(function() { (function() {
var activate, active, add_class, deactivate, hover, padding, reactive, rm_class, svg, tooltip, tooltip_font_size, tooltip_timeout, untooltip, _, __, var activate, add_class, deactivate, hover, padding, rm_class, svg, tooltip, tooltip_font_size, tooltip_timeout, untooltip, _, __,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; },
__slice = [].slice; __slice = [].slice;
@ -71,14 +71,6 @@
}; };
}; };
reactive = function(element) {
return document.getElementById('re' + element.id);
};
active = function(element) {
return document.getElementById(element.id.replace(/re/, ''));
};
hover = function(elts, over, out) { hover = function(elts, over, out) {
var elt, _i, _len, _results; var elt, _i, _len, _results;
_results = []; _results = [];
@ -123,35 +115,38 @@
text = _ref[_i]; text = _ref[_i];
text.setAttribute('display', 'none'); text.setAttribute('display', 'none');
} }
hover(_('.reactive-text'), (function() {
return activate(this, active(this));
}), (function() {
return deactivate(this, active(this));
}));
hover(_('.reactive'), (function() { hover(_('.reactive'), (function() {
return activate(this, reactive(this)); return activate(this);
}), (function() { }), (function() {
return deactivate(this, reactive(this)); return deactivate(this);
})); }));
hover(_('.activate-serie'), (function() { hover(_('.activate-serie'), (function() {
var element, num, _j, _len2, _ref2, _results; var element, num, _j, _k, _len2, _len3, _ref2, _ref3, _results;
num = this.id.replace('activate-serie-', ''); num = this.id.replace('activate-serie-', '');
_('.text-overlay .serie-' + num)[0].setAttribute('display', 'inline'); _ref2 = _('.text-overlay .serie-' + num);
_ref2 = _('.serie-' + num + ' .reactive');
_results = [];
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
element = _ref2[_j]; element = _ref2[_j];
element.setAttribute('display', 'inline');
}
_ref3 = _('.serie-' + num + ' .reactive');
_results = [];
for (_k = 0, _len3 = _ref3.length; _k < _len3; _k++) {
element = _ref3[_k];
_results.push(activate(element, reactive(element))); _results.push(activate(element, reactive(element)));
} }
return _results; return _results;
}), (function() { }), (function() {
var element, num, _j, _len2, _ref2, _results; var element, num, _j, _k, _len2, _len3, _ref2, _ref3, _results;
num = this.id.replace('activate-serie-', ''); num = this.id.replace('activate-serie-', '');
_('.text-overlay .serie-' + num)[0].setAttribute('display', 'none'); _ref2 = _('.text-overlay .serie-' + num);
_ref2 = _('.serie-' + num + ' .reactive');
_results = [];
for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) {
element = _ref2[_j]; element = _ref2[_j];
element.setAttribute('display', 'none');
}
_ref3 = _('.serie-' + num + ' .reactive');
_results = [];
for (_k = 0, _len3 = _ref3.length; _k < _len3; _k++) {
element = _ref3[_k];
_results.push(deactivate(element, reactive(element))); _results.push(deactivate(element, reactive(element)));
} }
return _results; return _results;

Loading…
Cancel
Save