Skip to content

Commit c142f5e

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). Task: 5214007
1 parent ddd275d commit c142f5e

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 { MAX_CHAR_LABEL } from "../../../constants";
38
import { Figure } from "../../../types";
49
import { GaugeChartRuntime, ScorecardChartRuntime } from "../../../types/chart";
@@ -41,6 +46,10 @@ export function chartToImage(
4146
): string | undefined {
4247
// wrap the canvas in a div with a fixed size because chart.js would
4348
// fill the whole page otherwise
49+
const extensionsLoaded = areChartJSExtensionsLoaded();
50+
if (!extensionsLoaded) {
51+
registerChartJSExtensions();
52+
}
4453
const div = document.createElement("div");
4554
div.style.width = `${figure.width}px`;
4655
div.style.height = `${figure.height}px`;
@@ -50,27 +59,28 @@ export function chartToImage(
5059
canvas.setAttribute("height", figure.height.toString());
5160
// we have to add the canvas to the DOM otherwise it won't be rendered
5261
document.body.append(div);
62+
let imgContent: string | undefined = undefined;
5363
if ("chartJsConfig" in runtime) {
5464
const config = deepCopy(runtime.chartJsConfig);
5565
config.plugins = [backgroundColorChartJSPlugin];
5666
const chart = new window.Chart(canvas, config as ChartConfiguration);
57-
const imgContent = chart.toBase64Image() as string;
67+
imgContent = chart.toBase64Image() as string;
5868
chart.destroy();
5969
div.remove();
60-
return imgContent;
6170
} else if (type === "scorecard") {
6271
const design = getScorecardConfiguration(figure, runtime as ScorecardChartRuntime);
6372
drawScoreChart(design, canvas);
64-
const imgContent = canvas.toDataURL();
73+
imgContent = canvas.toDataURL();
6574
div.remove();
66-
return imgContent;
6775
} else if (type === "gauge") {
6876
drawGaugeChart(canvas, runtime as GaugeChartRuntime);
69-
const imgContent = canvas.toDataURL();
77+
imgContent = canvas.toDataURL();
7078
div.remove();
71-
return imgContent;
7279
}
73-
return undefined;
80+
if (!extensionsLoaded) {
81+
unregisterChartJsExtensions();
82+
}
83+
return imgContent;
7484
}
7585

7686
/**

0 commit comments

Comments
 (0)