(host: GraphHost, palette: Palette)
| 465 | } |
| 466 | |
| 467 | function 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 | |
| 512 | function drawHorizontalBar(host: GraphHost, palette: Palette): GraphDrawFn { |
| 513 | return (spreadsheet, range, gview, gtype, helpflag) => { |
no test coverage detected