pie chart section generator
by STHayden
HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div style="width: 50%">
<svg id="svg" viewBox="0 0 45 45" style="width: 100%; height: 100%;">
<path id="arc1" fill="red" stroke-width="1" />
</svg>
</div>
</body>
</html>
JavaScript
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
var angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;
return {
x: centerX + (radius * Math.cos(angleInRadians)),
y: centerY + (radius * Math.sin(angleInRadians))
};
}
function describeArc(x, y, radius, startAngle, endAngle){
var end = polarToCartesian(x, y, radius, startAngle);
var start = polarToCartesian(x, y, radius, endAngle);
var largeArcFlag = endAngle - startAngle <= 180 ? "0" : "1";
var d = [
"M", end.x, end.y,
"A", radius, radius, 0, largeArcFlag, 1, start.x, start.y,
"L", 0, radius, 'Z'
].join(" ");
return d;
}
document.getElementById("svg").setAttribute("viewBox", `0 0 45 45`);
document.getElementById("arc1").setAttribute("d",
describeArc(0, 45, 45, 0, 360 * .75));