-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpie.js
45 lines (44 loc) · 1.91 KB
/
pie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Raphael.fn.pieChart = function (cx, cy, r, values, labels, stroke) {
var paper = this,
rad = Math.PI / 180,
chart = this.set();
function sector(cx, cy, r, startAngle, endAngle, params) {
var x1 = cx + r * Math.cos(-startAngle * rad),
x2 = cx + r * Math.cos(-endAngle * rad),
y1 = cy + r * Math.sin(-startAngle * rad),
y2 = cy + r * Math.sin(-endAngle * rad);
return paper.path(["M", cx, cy, "L", x1, y1, "A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2, "z"]).attr(params);
}
var angle = -90,
total = 0,
start = 0,
process = function (j) {
var value = values[j],
angleplus = 270 * value / total,
popangle = angle + (angleplus / 2),
color = "hsb(" + start + ", 1, .5)",
ms = 500,
delta = 30,
bcolor = "hsb(" + start + ", 1, 1)",
p = sector(cx, cy, r, angle, angle + angleplus, {fill: color, stroke: stroke, "stroke-width": 3}),
txt = paper.text(cx + (r + delta + 55) * Math.cos(-popangle * rad), cy + (r + delta + 25) * Math.sin(-popangle * rad), labels[j]).attr({fill: bcolor, stroke: "none", opacity: 0, "font-family": 'Fontin-Sans, Arial', "font-size": "20px"});
p.mouseover(function () {
p.animate({scale: [1.02, 1.02, cx, cy]}, ms, "elastic");
txt.animate({opacity: 1}, ms, "elastic");
}).mouseout(function () {
p.animate({scale: [1, 1, cx, cy]}, ms, "elastic");
txt.animate({opacity: 0}, ms);
});
angle += angleplus;
chart.push(p);
chart.push(txt);
start += .1;
};
for (var i = 0, ii = values.length; i < ii; i++) {
total += values[i];
}
for (var i = 0; i < ii; i++) {
process(i);
}
return chart;
};