Browse Source

Finish pie rewrite

pull/8/head
Florian Mounier 13 years ago
parent
commit
a5b8b0a3b1
  1. 53
      pygal/graph/pie.py
  2. 11
      pygal/js/graph.coffee
  3. 11
      pygal/js/graph.js

53
pygal/graph/pie.py

@ -32,8 +32,10 @@ class Pie(Graph):
(self.height - self.margin.y) / 2.) (self.height - self.margin.y) / 2.)
r = min(center) r = min(center)
if small: if small:
r *= .9 small_r = r * .9
center_str = '%f %f' % center else:
r = r * .9
small_r = 0
if perc == 1: if perc == 1:
self.svg.node(slice_, 'circle', self.svg.node(slice_, 'circle',
cx=center[0], cx=center[0],
@ -45,18 +47,27 @@ class Pie(Graph):
rho * sin(-alpha), rho * cos(-alpha)) rho * sin(-alpha), rho * cos(-alpha))
diff = lambda x, y: (x[0] - y[0], x[1] - y[1]) diff = lambda x, y: (x[0] - y[0], x[1] - y[1])
fmt = lambda x: '%f %f' % x fmt = lambda x: '%f %f' % x
to1 = fmt(diff(center, project(r, start_angle))) get_radius = lambda r: fmt(tuple([r] * 2))
to2 = fmt(diff(center, project(r, start_angle + angle))) absolute_project = lambda rho, theta: fmt(
rxy = fmt(tuple([r] * 2)) diff(center, project(rho, theta)))
to1 = absolute_project(r, start_angle)
to2 = absolute_project(r, start_angle + angle)
to3 = absolute_project(small_r, start_angle + angle)
to4 = absolute_project(small_r, start_angle)
self.svg.node(slice_, 'path', self.svg.node(slice_, 'path',
d='M%s L%s A%s 0 %d 1 %s z' % ( d='M%s A%s 0 %d 1 %s L%s A%s 0 %d 0 %s z' % (
center_str,
to1, to1,
rxy, get_radius(r), int(angle > pi), to2,
1 if angle > pi else 0, to3,
to2), get_radius(small_r), int(angle > pi), to4),
class_='slice reactive tooltip-trigger') class_='slice reactive tooltip-trigger')
self.svg.node(slice_, 'desc').text = val self.svg.node(slice_, 'desc', class_="value").text = val
tooltip_position = map(
str, diff(center, project(
(r + small_r) / 2., start_angle + angle / 2.)))
self.svg.node(slice_, 'desc', class_="x").text = tooltip_position[0]
self.svg.node(slice_, 'desc', class_="y").text = tooltip_position[1]
text_angle = pi / 2. - (start_angle + angle / 2.) text_angle = pi / 2. - (start_angle + angle / 2.)
text_r = r * .95 text_r = r * .95
self.svg.node(serie_node['text_overlay'], 'text', self.svg.node(serie_node['text_overlay'], 'text',
@ -81,14 +92,14 @@ class Pie(Graph):
self._serie(serie.index), self._serie(serie.index),
current_angle, current_angle,
angle, sum(serie.values) / total) angle, sum(serie.values) / total)
# 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):
# small_angle = 2 * pi * val / total small_angle = 2 * pi * val / total
# self.slice( self.slice(
# self._serie(serie.index), self._serie(serie.index),
# small_current_angle, small_current_angle,
# small_angle, val / total, small_angle, val / total,
# True) True)
# small_current_angle += small_angle small_current_angle += small_angle
current_angle += angle current_angle += angle

11
pygal/js/graph.coffee

@ -44,19 +44,16 @@ tooltip = (elt) ->
_tooltip = __('tooltip') _tooltip = __('tooltip')
_text = _tooltip.getElementsByTagName('text')[0] _text = _tooltip.getElementsByTagName('text')[0]
_rect = _tooltip.getElementsByTagName('rect')[0] _rect = _tooltip.getElementsByTagName('rect')[0]
_text.textContent = elt.nextElementSibling.textContent value = elt.nextElementSibling
_text.textContent = value.textContent
w = _text.offsetWidth + 2 * padding w = _text.offsetWidth + 2 * padding
h = _text.offsetHeight + 2 * padding h = _text.offsetHeight + 2 * padding
_rect.setAttribute('width', w) _rect.setAttribute('width', w)
_rect.setAttribute('height', h) _rect.setAttribute('height', h)
_text.setAttribute('x', padding) _text.setAttribute('x', padding)
_text.setAttribute('y', padding + tooltip_font_size) _text.setAttribute('y', padding + tooltip_font_size)
x = elt.getAttribute('cx') || elt.getAttribute('x') x = value.nextElementSibling.textContent - w / 2
y = elt.getAttribute('cy') || elt.getAttribute('y') y = value.nextElementSibling.nextElementSibling.textContent - h / 2
if x - w > 0
x -= w
if y - h > 0
y -= h
_tooltip.setAttribute('transform', "translate(#{x} #{y})") _tooltip.setAttribute('transform', "translate(#{x} #{y})")
untooltip = -> untooltip = ->

11
pygal/js/graph.js

@ -83,22 +83,21 @@
}; };
tooltip = function(elt) { tooltip = function(elt) {
var h, w, x, y, _rect, _text, _tooltip; var h, value, w, x, y, _rect, _text, _tooltip;
clearTimeout(tooltip_timeout); clearTimeout(tooltip_timeout);
_tooltip = __('tooltip'); _tooltip = __('tooltip');
_text = _tooltip.getElementsByTagName('text')[0]; _text = _tooltip.getElementsByTagName('text')[0];
_rect = _tooltip.getElementsByTagName('rect')[0]; _rect = _tooltip.getElementsByTagName('rect')[0];
_text.textContent = elt.nextElementSibling.textContent; value = elt.nextElementSibling;
_text.textContent = value.textContent;
w = _text.offsetWidth + 2 * padding; w = _text.offsetWidth + 2 * padding;
h = _text.offsetHeight + 2 * padding; h = _text.offsetHeight + 2 * padding;
_rect.setAttribute('width', w); _rect.setAttribute('width', w);
_rect.setAttribute('height', h); _rect.setAttribute('height', h);
_text.setAttribute('x', padding); _text.setAttribute('x', padding);
_text.setAttribute('y', padding + tooltip_font_size); _text.setAttribute('y', padding + tooltip_font_size);
x = elt.getAttribute('cx') || elt.getAttribute('x'); x = value.nextElementSibling.textContent - w / 2;
y = elt.getAttribute('cy') || elt.getAttribute('y'); y = value.nextElementSibling.nextElementSibling.textContent - h / 2;
if (x - w > 0) x -= w;
if (y - h > 0) y -= h;
return _tooltip.setAttribute('transform', "translate(" + x + " " + y + ")"); return _tooltip.setAttribute('transform', "translate(" + x + " " + y + ")");
}; };

Loading…
Cancel
Save