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

Function drawVerticalBar

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

Source from the content-addressed store, hash-verified

465}
466
467function drawVerticalBar(host: GraphHost, palette: Palette): GraphDrawFn {
468 return (spreadsheet, range, gview, gtype, helpflag) => {
469 if (helpflag || !range) {
470 const hideHelp = host.SocialCalc.Constants['s_loc_hide_help'] ?? 'Hide Help';
471 const display = (host.SocialCalc.GraphTypesInfo![gtype] as { display: string }).display;
472 gview.innerHTML =
473 `<input type="button" value="${hideHelp}" onclick="DoGraph(false,false);"><br><br>` +
474 `This is the help text for graph type: ${display}.`;
475 return;
476 }
477 const { values, labels } = collectValues(host, spreadsheet, range);
478 let maxval: number | null = null;
479 let minval: number | null = null;
480 for (const v of values) {
481 if (maxval === null || v > maxval) maxval = v;
482 if (minval === null || v < minval) minval = v;
483 }
484 if ((maxval ?? 0) < 0) maxval = 0;
485 if ((minval ?? 0) > 0) minval = 0;
486 gview.innerHTML = '<table><tr><td><canvas id="myBarCanvas" width="500px" height="400px" style="border:1px solid black;"></canvas></td><td><span id="googleBarChart"></span></td></tr></table>';
487 const canv = host.doc.getElementById('myBarCanvas') as HTMLCanvasElement | null;
488 if (!canv) return;
489 const ctx = canv.getContext('2d') as CanvasRenderingContext2D | null;
490 if (!ctx) return;
491 ctx.font = '10pt bold Arial';
492 const maxheight = canv.height - 60;
493 const totalwidth = canv.width;
494 palette.reset();
495 const eachwidth = Math.floor(totalwidth / (values.length || 1)) - 4 || 1;
496 const zeroLine = maxheight * ((maxval ?? 0) / (((maxval ?? 0) - (minval ?? 0)) || 1)) + 30;
497 const yScale = maxheight / (((maxval ?? 0) - (minval ?? 0)) || 1);
498 ctx.lineWidth = 5;
499 ctx.moveTo(0, zeroLine);
500 ctx.lineTo(canv.width, zeroLine);
501 ctx.stroke();
502 for (let i = 0; i < values.length; i++) {
503 ctx.fillStyle = '#' + palette.getBarColor();
504 ctx.fillRect(i * eachwidth, zeroLine - yScale * values[i]!, eachwidth, yScale * values[i]!);
505 // Labels are pushed in lockstep with values; the `?? ''` fallback is a
506 // defensive guard never triggered by collectValues.
507 ctx.fillText(/* istanbul ignore next */ labels[i] ?? '', i * eachwidth + 4, zeroLine + 16);
508 }
509 };
510}
511
512function drawHorizontalBar(host: GraphHost, palette: Palette): GraphDrawFn {
513 return (spreadsheet, range, gview, gtype, helpflag) => {

Callers 1

installGraphFunction · 0.85

Calls 8

collectValuesFunction · 0.85
getContextMethod · 0.80
resetMethod · 0.80
moveToMethod · 0.80
lineToMethod · 0.80
strokeMethod · 0.80
fillRectMethod · 0.80
fillTextMethod · 0.80

Tested by

no test coverage detected