MCPcopy
hub / github.com/audreyt/ethercalc / drawPieChart

Function drawPieChart

packages/client/src/graph.ts:540–570  ·  view source on GitHub ↗
(host: GraphHost, palette: Palette)

Source from the content-addressed store, hash-verified

538}
539
540function drawPieChart(host: GraphHost, palette: Palette): GraphDrawFn {
541 return (spreadsheet, range, gview) => {
542 if (!range) return;
543 const { values, labels } = collectValues(host, spreadsheet, range);
544 const total = values.reduce((a, b) => a + b, 0);
545 gview.innerHTML = '<canvas id="myCanvas" width="500px" height="400px"></canvas>';
546 const canv = host.doc.getElementById('myCanvas') as HTMLCanvasElement | null;
547 if (!canv) return;
548 const ctx = canv.getContext('2d') as CanvasRenderingContext2D | null;
549 if (!ctx) return;
550 ctx.font = '10pt Arial';
551 const centerX = canv.width / 2;
552 const centerY = canv.height / 2;
553 const rad = centerY - 50;
554 let last = 0;
555 for (let i = 0; i < values.length; i++) {
556 if (!Number(values[i])) continue;
557 ctx.beginPath();
558 ctx.moveTo(centerX, centerY);
559 ctx.fillStyle = palette.getDrawColor();
560 const arc = 2 * Math.PI * (values[i]! / (total || 1));
561 ctx.arc(centerX, centerY, rad, last, last + arc, false);
562 ctx.closePath();
563 ctx.fill();
564 ctx.fillStyle = '#000';
565 // Labels pushed 1:1 with values — `?? ''` is defensive-only.
566 ctx.fillText(/* istanbul ignore next */ labels[i] ?? '', centerX + Math.cos(last + arc / 2) * rad, centerY + Math.sin(last + arc / 2) * rad);
567 last += arc;
568 }
569 };
570}
571
572function drawLineChart(host: GraphHost, palette: Palette): GraphDrawFn {
573 return (spreadsheet, range, gview) => {

Callers 1

installGraphFunction · 0.85

Calls 8

collectValuesFunction · 0.85
getContextMethod · 0.80
beginPathMethod · 0.80
moveToMethod · 0.80
arcMethod · 0.80
closePathMethod · 0.80
fillMethod · 0.80
fillTextMethod · 0.80

Tested by

no test coverage detected