Skip to content

Commit cd4a145

Browse files
committed
[FIX] Charts: Ensure Chart js extension are loaded on chart creation
When calling the method chartToImage, the chartJs extensions might not be loaded as we load them conditionally when mounting a chart. Hence, calling `ChartToImage` when there are no visible charts in the viewport or if we just instantiate a model without loading a component `Spreadsheet`, the extensions will be missing. Right now, the plugins/extensions are not fundamental to convert a chart to an image but this becomes problematic with the arrival of future charts (for instance Funnel). This commit adds a check to ensure that the extensisons are loaded and unloads them after use as they can cause crashes when using the global `ChartJs` variable elsewhere (see #6076). Task: 5214007
1 parent ddd275d commit cd4a145

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/helpers/figures/charts/chart_ui_common.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import type { ChartConfiguration, ChartOptions } from "chart.js";
2+
import {
3+
areChartJSExtensionsLoaded,
4+
registerChartJSExtensions,
5+
unregisterChartJsExtensions,
6+
} from "../../../components/figures/chart/chartJs/chart_js_extension";
27
import { MAX_CHAR_LABEL } from "../../../constants";
38
import { Figure } from "../../../types";
49
import { GaugeChartRuntime, ScorecardChartRuntime } from "../../../types/chart";
@@ -50,27 +55,33 @@ export function chartToImage(
5055
canvas.setAttribute("height", figure.height.toString());
5156
// we have to add the canvas to the DOM otherwise it won't be rendered
5257
document.body.append(div);
58+
let imgContent: string | undefined = undefined;
59+
let extensionsLoaded = false;
5360
if ("chartJsConfig" in runtime) {
61+
extensionsLoaded = areChartJSExtensionsLoaded();
62+
if (!extensionsLoaded) {
63+
registerChartJSExtensions();
64+
}
5465
const config = deepCopy(runtime.chartJsConfig);
5566
config.plugins = [backgroundColorChartJSPlugin];
5667
const chart = new window.Chart(canvas, config as ChartConfiguration);
57-
const imgContent = chart.toBase64Image() as string;
68+
imgContent = chart.toBase64Image() as string;
5869
chart.destroy();
5970
div.remove();
60-
return imgContent;
6171
} else if (type === "scorecard") {
6272
const design = getScorecardConfiguration(figure, runtime as ScorecardChartRuntime);
6373
drawScoreChart(design, canvas);
64-
const imgContent = canvas.toDataURL();
74+
imgContent = canvas.toDataURL();
6575
div.remove();
66-
return imgContent;
6776
} else if (type === "gauge") {
6877
drawGaugeChart(canvas, runtime as GaugeChartRuntime);
69-
const imgContent = canvas.toDataURL();
78+
imgContent = canvas.toDataURL();
7079
div.remove();
71-
return imgContent;
7280
}
73-
return undefined;
81+
if (!extensionsLoaded) {
82+
unregisterChartJsExtensions();
83+
}
84+
return imgContent;
7485
}
7586

7687
/**

0 commit comments

Comments
 (0)