diff --git a/pygal/graph/pie.py b/pygal/graph/pie.py index f59bea0..b49d15f 100644 --- a/pygal/graph/pie.py +++ b/pygal/graph/pie.py @@ -32,8 +32,10 @@ class Pie(Graph): (self.height - self.margin.y) / 2.) r = min(center) if small: - r *= .9 - center_str = '%f %f' % center + small_r = r * .9 + else: + r = r * .9 + small_r = 0 if perc == 1: self.svg.node(slice_, 'circle', cx=center[0], @@ -45,18 +47,27 @@ class Pie(Graph): rho * sin(-alpha), rho * cos(-alpha)) diff = lambda x, y: (x[0] - y[0], x[1] - y[1]) fmt = lambda x: '%f %f' % x - to1 = fmt(diff(center, project(r, start_angle))) - to2 = fmt(diff(center, project(r, start_angle + angle))) - rxy = fmt(tuple([r] * 2)) + get_radius = lambda r: fmt(tuple([r] * 2)) + absolute_project = lambda rho, theta: fmt( + 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', - d='M%s L%s A%s 0 %d 1 %s z' % ( - center_str, + d='M%s A%s 0 %d 1 %s L%s A%s 0 %d 0 %s z' % ( to1, - rxy, - 1 if angle > pi else 0, - to2), + get_radius(r), int(angle > pi), to2, + to3, + get_radius(small_r), int(angle > pi), to4), 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_r = r * .95 self.svg.node(serie_node['text_overlay'], 'text', @@ -81,14 +92,14 @@ class Pie(Graph): self._serie(serie.index), current_angle, angle, sum(serie.values) / total) - # if len(serie.values) > 1: - # small_current_angle = current_angle - # for i, val in enumerate(serie.values): - # small_angle = 2 * pi * val / total - # self.slice( - # self._serie(serie.index), - # small_current_angle, - # small_angle, val / total, - # True) - # small_current_angle += small_angle + if len(serie.values) > 1: + small_current_angle = current_angle + for i, val in enumerate(serie.values): + small_angle = 2 * pi * val / total + self.slice( + self._serie(serie.index), + small_current_angle, + small_angle, val / total, + True) + small_current_angle += small_angle current_angle += angle diff --git a/pygal/js/graph.coffee b/pygal/js/graph.coffee index 5229974..3c244cf 100644 --- a/pygal/js/graph.coffee +++ b/pygal/js/graph.coffee @@ -44,19 +44,16 @@ tooltip = (elt) -> _tooltip = __('tooltip') _text = _tooltip.getElementsByTagName('text')[0] _rect = _tooltip.getElementsByTagName('rect')[0] - _text.textContent = elt.nextElementSibling.textContent + value = elt.nextElementSibling + _text.textContent = value.textContent w = _text.offsetWidth + 2 * padding h = _text.offsetHeight + 2 * padding _rect.setAttribute('width', w) _rect.setAttribute('height', h) _text.setAttribute('x', padding) _text.setAttribute('y', padding + tooltip_font_size) - x = elt.getAttribute('cx') || elt.getAttribute('x') - y = elt.getAttribute('cy') || elt.getAttribute('y') - if x - w > 0 - x -= w - if y - h > 0 - y -= h + x = value.nextElementSibling.textContent - w / 2 + y = value.nextElementSibling.nextElementSibling.textContent - h / 2 _tooltip.setAttribute('transform', "translate(#{x} #{y})") untooltip = -> diff --git a/pygal/js/graph.js b/pygal/js/graph.js index 8998598..4ceec91 100644 --- a/pygal/js/graph.js +++ b/pygal/js/graph.js @@ -83,22 +83,21 @@ }; tooltip = function(elt) { - var h, w, x, y, _rect, _text, _tooltip; + var h, value, w, x, y, _rect, _text, _tooltip; clearTimeout(tooltip_timeout); _tooltip = __('tooltip'); _text = _tooltip.getElementsByTagName('text')[0]; _rect = _tooltip.getElementsByTagName('rect')[0]; - _text.textContent = elt.nextElementSibling.textContent; + value = elt.nextElementSibling; + _text.textContent = value.textContent; w = _text.offsetWidth + 2 * padding; h = _text.offsetHeight + 2 * padding; _rect.setAttribute('width', w); _rect.setAttribute('height', h); _text.setAttribute('x', padding); _text.setAttribute('y', padding + tooltip_font_size); - x = elt.getAttribute('cx') || elt.getAttribute('x'); - y = elt.getAttribute('cy') || elt.getAttribute('y'); - if (x - w > 0) x -= w; - if (y - h > 0) y -= h; + x = value.nextElementSibling.textContent - w / 2; + y = value.nextElementSibling.nextElementSibling.textContent - h / 2; return _tooltip.setAttribute('transform', "translate(" + x + " " + y + ")"); };