Skip to content

Keep Instances intact when cloning datasets on update #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
11 changes: 9 additions & 2 deletions src/core/component.builder.ts
Original file line number Diff line number Diff line change
@@ -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 = <TType extends ChartType = ChartType>(

// 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<T extends unknown>(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;