Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/helpers/figures/charts/chart_ui_common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { ChartConfiguration, ChartOptions } from "chart.js";
import {
areChartJSExtensionsLoaded,
registerChartJSExtensions,
unregisterChartJsExtensions,
} from "../../../components/figures/chart/chartJs/chart_js_extension";
import { Figure } from "../../../types";
import { GaugeChartRuntime, ScorecardChartRuntime } from "../../../types/chart";
import { ChartRuntime } from "../../../types/chart/chart";
Expand Down Expand Up @@ -39,27 +44,32 @@ export function chartToImage(
canvas.setAttribute("height", figure.height.toString());
// we have to add the canvas to the DOM otherwise it won't be rendered
document.body.append(div);
let imgContent: string | undefined = undefined;
if ("chartJsConfig" in runtime) {
const extensionsLoaded = areChartJSExtensionsLoaded();
if (!extensionsLoaded) {
registerChartJSExtensions();
}
const config = deepCopy(runtime.chartJsConfig);
config.plugins = [backgroundColorChartJSPlugin];
const chart = new window.Chart(canvas, config as ChartConfiguration);
const imgContent = chart.toBase64Image() as string;
imgContent = chart.toBase64Image() as string;
chart.destroy();
div.remove();
return imgContent;
if (!extensionsLoaded) {
unregisterChartJsExtensions();
}
} else if (type === "scorecard") {
const design = getScorecardConfiguration(figure, runtime as ScorecardChartRuntime);
drawScoreChart(design, canvas);
const imgContent = canvas.toDataURL();
imgContent = canvas.toDataURL();
div.remove();
return imgContent;
} else if (type === "gauge") {
drawGaugeChart(canvas, runtime as GaugeChartRuntime);
const imgContent = canvas.toDataURL();
imgContent = canvas.toDataURL();
div.remove();
return imgContent;
}
return undefined;
return imgContent;
}

/**
Expand Down