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

Function drawHorizontalBar

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

Source from the content-addressed store, hash-verified

510}
511
512function drawHorizontalBar(host: GraphHost, palette: Palette): GraphDrawFn {
513 return (spreadsheet, range, gview, gtype, helpflag) => {
514 if (helpflag || !range) {
515 const hideHelp = host.SocialCalc.Constants['s_loc_hide_help'] ?? 'Hide Help';
516 gview.innerHTML = `<input type="button" value="${hideHelp}" onclick="DoGraph(false,false);"><br><br>Horizontal bar help.`;
517 return;
518 }
519 const { values, labels } = collectValues(host, spreadsheet, range);
520 gview.innerHTML = '<canvas id="myBarCanvas" width="500px" height="400px" style="border:1px solid black;"></canvas>';
521 const canv = host.doc.getElementById('myBarCanvas') as HTMLCanvasElement | null;
522 if (!canv) return;
523 const ctx = canv.getContext('2d') as CanvasRenderingContext2D | null;
524 if (!ctx) return;
525 ctx.font = '10pt bold Arial';
526 palette.reset();
527 const each = Math.floor(canv.height / (values.length || 1)) - 4 || 1;
528 let max = 0;
529 for (const v of values) if (v > max) max = v;
530 for (let i = 0; i < values.length; i++) {
531 ctx.fillStyle = '#' + palette.getBarColor();
532 ctx.fillRect(0, i * each, (values[i]! / (max || 1)) * (canv.width - 50), each);
533 ctx.fillStyle = '#000';
534 // Labels pushed 1:1 with values — `?? ''` is defensive-only.
535 ctx.fillText(/* istanbul ignore next */ labels[i] ?? '', 4, i * each + each / 2);
536 }
537 };
538}
539
540function drawPieChart(host: GraphHost, palette: Palette): GraphDrawFn {
541 return (spreadsheet, range, gview) => {

Callers 1

installGraphFunction · 0.85

Calls 5

collectValuesFunction · 0.85
getContextMethod · 0.80
resetMethod · 0.80
fillRectMethod · 0.80
fillTextMethod · 0.80

Tested by

no test coverage detected