Skip to content

Instantly share code, notes, and snippets.

@MD4
Last active August 29, 2015 14:20
Show Gist options
  • Save MD4/295bbbf05d588490df37 to your computer and use it in GitHub Desktop.
Save MD4/295bbbf05d588490df37 to your computer and use it in GitHub Desktop.
Bonsaï.js pie chart
var slices = [
{value: 100, color: "red"},
{value: 33, color: "green"},
{value: 41, color: "blue"},
{value: 103, color: "yellow"},
{value: 145, color: "orange"},
];
var origin = {x: 200, y: 200},
radius = 100,
srtokeWidth = radius / 20;
var sum = slices.reduce(function(memo, slice) {
memo += slice.value;
return memo;
}, 0);
var currentSum = 0;
slices.forEach(function(slice) {
var path = new Path();
var percent = slice.value / sum;
path.moveTo(origin.x, origin.y);
path.arc(
origin.x, origin.y,
radius,
currentSum * Math.PI * 2,
(percent + currentSum) * Math.PI * 2,
false
);
path.lineTo(origin.x, origin.y);
path.stroke("#222", srtokeWidth).fill(slice.color).addTo(stage);
currentSum += percent;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment