From 367380e4d79b60ae62dee9255d27db2e68447d97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonatan=20M=C3=A4nnchen?= Date: Fri, 23 Sep 2022 14:23:02 +0200 Subject: [PATCH] Keep Instances intact when cloning datasets on update Fixes an issue where a CanvasGradient was falsely cloned as an empty object and thus breaking the background of the chart This should probably fix #120 as well. --- src/core/component.builder.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core/component.builder.ts b/src/core/component.builder.ts index de19741..8611268 100644 --- a/src/core/component.builder.ts +++ b/src/core/component.builder.ts @@ -1,6 +1,6 @@ import type { Chart, ChartData, ChartDataset, ChartOptions, ChartType, Plugin } from 'chart.js'; import * as Chartjs from 'chart.js'; -import { cloneDeep, isEmpty, isEqual, isObject } from 'lodash-es'; +import { cloneDeep, cloneDeepWith, isEmpty, isEqual, isObject, isArray } from 'lodash-es'; import { pascalCase } from '../utils'; import { ComponentOptionsMixin, @@ -121,7 +121,14 @@ export const defineChartComponent = ( // Update attributes individually to avoid re-rendering the entire chart for (const attribute in dataset) { - const attrValue = cloneDeep(dataset[attribute as keyof ChartDataset]); + function keepInstancesIntactCloneCustomizer(value: T): T|undefined { + if(!isObject(value)) return; + if(value.constructor.name === 'Object') return; + if(isArray(value)) return; + return value; + } + + const attrValue = cloneDeepWith(dataset[attribute as keyof ChartDataset], keepInstancesIntactCloneCustomizer); let datasetItem = chart.data.datasets[index] as any; if (!datasetItem) { chart.data.datasets[index] = {} as any;