https://jsfiddle.net/p4d57e8x/3/
const getArcPath = (start, end, innerRadius, outerRadius) => {
const startAngle = start * Math.PI * 2;
const endAngle = end * Math.PI * 2;
const x1 = innerRadius * Math.sin(startAngle);
const y1 = innerRadius * -Math.cos(startAngle);
const x2 = outerRadius * Math.sin(startAngle);
const y2 = outerRadius * -Math.cos(startAngle);
const x3 = outerRadius * Math.sin(endAngle);
const y3 = outerRadius * -Math.cos(endAngle);
const x4 = innerRadius * Math.sin(endAngle);
const y4 = innerRadius * -Math.cos(endAngle);
const bigArc = end - start >= 0.5;
const outerFlags = bigArc ? '1 1 1' : '0 0 1';
const innerFlags = bigArc ? '1 1 0' : '1 0 0';
return `M ${x1},${y1} L ${x2},${y2} A ${outerRadius} ${outerRadius} ${outerFlags} ${x3},${y3}
L ${x4},${y4} A ${innerRadius} ${innerRadius} ${innerFlags} ${x1},${y1} Z`;
};
This is the code for the path itself, and all of the paths they create a circle.
I am not sure how to get the rotation angle for each path in the circle (say there is red, green, blue, purple), I’ve tried many things but this seems more of a math problem more than anything else.
I tried a bunch of random code I found, this for example
const getRotationAngle = (start, end) => {
const startAngle = start * 360;
const endAngle = end * 360;
return (startAngle + endAngle) / 2;
};
``` but it doesn't work of course because it's just gibberish for me and I have no idea what I even need to calculate to get the angle.
2
Answers
Your JSFiddle references a 44 KB React solution; there is a 999 Bytes Web Component for it:
I wrote a Dev.to post for it
You current script works actually pretty fine. I guess the main confusion comes from the fact you start drawing pie chart segments at "12 o’clock" – which would actually be -90 degree when using CSS transforms.
Here’s an example where I place labels at the middle of each segment.
The "rotationAngle" variable gets an offset like so.
BTW. we can get rid of the first
L
segment