Skip to content

Commit fedfbaa

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). closes #7378 Task: 5214007 Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
1 parent 2e9a842 commit fedfbaa

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/helpers/figures/charts/chart_ui_common.ts

Lines changed: 17 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 { Figure } from "../../../types";
38
import { GaugeChartRuntime, ScorecardChartRuntime } from "../../../types/chart";
49
import { ChartRuntime } from "../../../types/chart/chart";
@@ -39,27 +44,32 @@ export function chartToImage(
3944
canvas.setAttribute("height", figure.height.toString());
4045
// we have to add the canvas to the DOM otherwise it won't be rendered
4146
document.body.append(div);
47+
let imgContent: string | undefined = undefined;
4248
if ("chartJsConfig" in runtime) {
49+
const extensionsLoaded = areChartJSExtensionsLoaded();
50+
if (!extensionsLoaded) {
51+
registerChartJSExtensions();
52+
}
4353
const config = deepCopy(runtime.chartJsConfig);
4454
config.plugins = [backgroundColorChartJSPlugin];
4555
const chart = new window.Chart(canvas, config as ChartConfiguration);
46-
const imgContent = chart.toBase64Image() as string;
56+
imgContent = chart.toBase64Image() as string;
4757
chart.destroy();
4858
div.remove();
49-
return imgContent;
59+
if (!extensionsLoaded) {
60+
unregisterChartJsExtensions();
61+
}
5062
} else if (type === "scorecard") {
5163
const design = getScorecardConfiguration(figure, runtime as ScorecardChartRuntime);
5264
drawScoreChart(design, canvas);
53-
const imgContent = canvas.toDataURL();
65+
imgContent = canvas.toDataURL();
5466
div.remove();
55-
return imgContent;
5667
} else if (type === "gauge") {
5768
drawGaugeChart(canvas, runtime as GaugeChartRuntime);
58-
const imgContent = canvas.toDataURL();
69+
imgContent = canvas.toDataURL();
5970
div.remove();
60-
return imgContent;
6171
}
62-
return undefined;
72+
return imgContent;
6373
}
6474

6575
/**

0 commit comments

Comments
 (0)