From be092d2527a685b83f4398133e67cf19eb91d9b4 Mon Sep 17 00:00:00 2001 From: Faton Ramadani Date: Wed, 5 Jun 2024 13:34:09 +0200 Subject: [PATCH] fix(frontend): Fix AgChart when the component is hidden (#3876) --- .../display/charts/AppAgCharts.svelte | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/frontend/src/lib/components/apps/components/display/charts/AppAgCharts.svelte b/frontend/src/lib/components/apps/components/display/charts/AppAgCharts.svelte index 00bc7fa134..4b8fe6fa0e 100644 --- a/frontend/src/lib/components/apps/components/display/charts/AppAgCharts.svelte +++ b/frontend/src/lib/components/apps/components/display/charts/AppAgCharts.svelte @@ -8,7 +8,7 @@ RichConfigurations } from '../../../types' import { initCss } from '../../../utils' - import { getContext, onMount } from 'svelte' + import { getContext } from 'svelte' import { initConfig, initOutput } from '../../../editor/appUtils' import { components } from '../../../editor/component' import ResolveConfig from '../../helpers/ResolveConfig.svelte' @@ -246,22 +246,34 @@ } } - onMount(() => { - loadLibrary().then(() => { - try { - // Chart Options - const options: AgChartOptions = { - container: document.getElementById(`agchart-${id}`) as HTMLElement, - data: [], - series: [] - } - - chartInstance = AgChartsInstance?.create(options) - } catch (error) { - console.error(error) + function initChart() { + try { + // Chart Options + const options: AgChartOptions = { + container: document.getElementById(`agchart-${id}`) as HTMLElement, + data: [], + series: [] } + + chartInstance = AgChartsInstance?.create(options) + } catch (error) { + console.error(error) + } + } + + function destroyChart() { + if (chartInstance) { + chartInstance.destroy() + chartInstance = undefined + } + } + + $: if (render) { + destroyChart() + loadLibrary().then(() => { + initChart() }) - }) + } {#if datasets}