|
1 | 1 | import type { ChartConfiguration, ChartOptions } from "chart.js"; |
| 2 | +import { |
| 3 | + areChartJSExtensionsLoaded, |
| 4 | + registerChartJSExtensions, |
| 5 | + unregisterChartJsExtensions, |
| 6 | +} from "../../../components/figures/chart/chartJs/chart_js_extension"; |
2 | 7 | import { MAX_CHAR_LABEL } from "../../../constants"; |
3 | 8 | import { Figure } from "../../../types"; |
4 | 9 | import { GaugeChartRuntime, ScorecardChartRuntime } from "../../../types/chart"; |
@@ -50,27 +55,33 @@ export function chartToImage( |
50 | 55 | canvas.setAttribute("height", figure.height.toString()); |
51 | 56 | // we have to add the canvas to the DOM otherwise it won't be rendered |
52 | 57 | document.body.append(div); |
| 58 | + let imgContent: string | undefined = undefined; |
| 59 | + let extensionsLoaded = false; |
53 | 60 | if ("chartJsConfig" in runtime) { |
| 61 | + extensionsLoaded = areChartJSExtensionsLoaded(); |
| 62 | + if (!extensionsLoaded) { |
| 63 | + registerChartJSExtensions(); |
| 64 | + } |
54 | 65 | const config = deepCopy(runtime.chartJsConfig); |
55 | 66 | config.plugins = [backgroundColorChartJSPlugin]; |
56 | 67 | const chart = new window.Chart(canvas, config as ChartConfiguration); |
57 | | - const imgContent = chart.toBase64Image() as string; |
| 68 | + imgContent = chart.toBase64Image() as string; |
58 | 69 | chart.destroy(); |
59 | 70 | div.remove(); |
60 | | - return imgContent; |
61 | 71 | } else if (type === "scorecard") { |
62 | 72 | const design = getScorecardConfiguration(figure, runtime as ScorecardChartRuntime); |
63 | 73 | drawScoreChart(design, canvas); |
64 | | - const imgContent = canvas.toDataURL(); |
| 74 | + imgContent = canvas.toDataURL(); |
65 | 75 | div.remove(); |
66 | | - return imgContent; |
67 | 76 | } else if (type === "gauge") { |
68 | 77 | drawGaugeChart(canvas, runtime as GaugeChartRuntime); |
69 | | - const imgContent = canvas.toDataURL(); |
| 78 | + imgContent = canvas.toDataURL(); |
70 | 79 | div.remove(); |
71 | | - return imgContent; |
72 | 80 | } |
73 | | - return undefined; |
| 81 | + if (!extensionsLoaded) { |
| 82 | + unregisterChartJsExtensions(); |
| 83 | + } |
| 84 | + return imgContent; |
74 | 85 | } |
75 | 86 |
|
76 | 87 | /** |
|
0 commit comments