-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsector.js
More file actions
39 lines (32 loc) · 1001 Bytes
/
Copy pathsector.js
File metadata and controls
39 lines (32 loc) · 1001 Bytes
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
// source: https://github.com/marianc000/pieChart
// from https://stackoverflow.com/questions/5736398/how-to-calculate-the-svg-path-for-an-arc-of-a-circle
export function getD(radius, startAngle, endAngle) {
const isCircle = endAngle - startAngle === 360;
if (isCircle) {
endAngle--;
}
const start = polarToCartesian(radius, startAngle);
const end = polarToCartesian(radius, endAngle);
const largeArcFlag = endAngle - startAngle <= 180 ? 0 : 1;
const d = [
"M", start.x, start.y,
"A", radius, radius, 0, largeArcFlag, 1, end.x, end.y];
if (isCircle) {
d.push("Z");
} else {
d.push("L", radius, radius,
"L", start.x, start.y,
"Z");
}
return d.join(" ");
}
function round(n) {
return Math.round(n * 10) / 10;
}
function polarToCartesian(radius, angleInDegrees) {
var radians = (angleInDegrees - 90) * Math.PI / 180;
return {
x: round(radius + (radius * Math.cos(radians))),
y: round(radius + (radius * Math.sin(radians)))
};
}