Skip to content

Commit b58dae9

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 #7562 Task: 5214007 X-original-commit: d146422 Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com> Signed-off-by: Rémi Rahir (rar) <rar@odoo.com>
1 parent 99a94ac commit b58dae9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/helpers/figures/charts/chart_ui_common.ts

Lines changed: 19 additions & 0 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 { ChartType, GaugeChartRuntime, ScorecardChartRuntime } from "../../../types/chart";
49
import { ChartRuntime } from "../../../types/chart/chart";
@@ -41,11 +46,18 @@ export function chartToImageUrl(
4146
// we have to add the canvas to the DOM otherwise it won't be rendered
4247
document.body.append(div);
4348
if ("chartJsConfig" in runtime) {
49+
const extensionsLoaded = areChartJSExtensionsLoaded();
50+
if (!extensionsLoaded) {
51+
registerChartJSExtensions();
52+
}
4453
const config = deepCopy(runtime.chartJsConfig);
4554
config.plugins = [backgroundColorChartJSPlugin];
4655
const chart = new window.Chart(canvas, config as ChartConfiguration);
4756
imageContent = chart.toBase64Image() as string;
4857
chart.destroy();
58+
if (!extensionsLoaded) {
59+
unregisterChartJsExtensions();
60+
}
4961
} else if (type === "scorecard") {
5062
const design = getScorecardConfiguration(figure, runtime as ScorecardChartRuntime);
5163
drawScoreChart(design, canvas);
@@ -78,11 +90,18 @@ export async function chartToImageFile(
7890
document.body.append(div);
7991
let chartBlob: Blob | null = null;
8092
if ("chartJsConfig" in runtime) {
93+
const extensionsLoaded = areChartJSExtensionsLoaded();
94+
if (!extensionsLoaded) {
95+
registerChartJSExtensions();
96+
}
8197
const config = deepCopy(runtime.chartJsConfig);
8298
config.plugins = [backgroundColorChartJSPlugin];
8399
const chart = new window.Chart(canvas, config as ChartConfiguration);
84100
chartBlob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, "image/png"));
85101
chart.destroy();
102+
if (!extensionsLoaded) {
103+
unregisterChartJsExtensions();
104+
}
86105
} else if (type === "scorecard") {
87106
const design = getScorecardConfiguration(figure, runtime as ScorecardChartRuntime);
88107
drawScoreChart(design, canvas);

0 commit comments

Comments
 (0)