Source

/*

This is an example of using the `arc()` function to create the wedges for a pie chart.

*/

function setup() {
  createCanvas(600, 600);
  angleMode(DEGREES);
  colorMode(HSB, 360, 100, 100);
  noLoop();
}

function draw() {
  background("#fafafa");

  pieChart([5, 2, 4, 10, 7], 200, 200, 200);

  // bonus arc to show how CHORD works
  fill("yellow");
  arc(400, 400, 100, 100, 0, 270, CHORD);
}

function pieChart(data, x, y, size) {
  const total = data.reduce((a, b) => a + b, 0);
  const colorOffset = 360 / data.length;

  let current = 0;
  data.forEach((d, i) => {
    const angle = (d / total) * 360;
    fill(colorOffset * i, 100, 100);

    arc(x, y, size, size, current, current + angle, PIE);
    current += angle;
  });
}