<button>+</button>
<button>-</button>
<div id="graph"></div>
// Custom Arc Attribute, position x&y, value portion of total, total value, Radius
var archtype = Raphael('graph', 100, 100);
// Manages creating the Path string based on angle of the arc
archtype.customAttributes.arc = function (value) {
var xloc = 50,
yloc = 50,
total = 100,
R = 30,
alpha = 360 / total * value,
a = (90 - alpha) * Math.PI / 180,
x = xloc + R * Math.cos(a),
y = yloc - R * Math.sin(a),
path;
if (total == value) {
path = [
["M", xloc, yloc - R],
["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R]
];
} else {
path = [
["M", xloc, yloc - R],
["A", R, R, 0, +(alpha > 180), 1, x, y]
];
}
return {
path: path
};
};
//make an arc at 50,50 with a radius of 30 that grows from 0 to 40 of 100 with a bounce
var my_arc = archtype.path().attr({
"stroke": "#0f0",
"stroke-width": 14,
arc: 0
});
var my_text = archtype.text().attr({
x : 50,
y : 50,
text: '0%'
});
function setAngle (x) {
var color = '#0f0';
if (x > 33) {
color = '#FF0';
}
if (x > 66) {
color = '#F00';
}
my_text.attr({
text: '' + x + '%'
});
my_arc.animate({
"stroke": color,
arc: x
}, 1000, "bounce");
}
var x = 30;
setAngle(x);
$('button').on('click', function (e) {
if ($(e.target).html() === '+') {
x = Math.min(100, x + 10);
} else {
x = Math.max(0, x - 10);
}
setAngle(x);
})
External resources loaded into this fiddle: