Skip to content

Commit 45a50cc

Browse files
authored
fix: data updating fix (#807)
#806
1 parent 50946e6 commit 45a50cc

File tree

4 files changed

+110
-8
lines changed

4 files changed

+110
-8
lines changed

src/chart.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import ChartJS from 'chart.js/auto';
44
import type { ChartData, ChartType, DefaultDataPoint } from 'chart.js';
55

66
import type { Props, TypedChartComponent } from './types';
7-
import { reforwardRef, setNextDatasets } from './utils';
7+
import {
8+
reforwardRef,
9+
cloneData,
10+
setOptions,
11+
setLabels,
12+
setDatasets,
13+
} from './utils';
814

915
const noopData = {
1016
datasets: [],
@@ -49,7 +55,7 @@ function ChartComponent<
4955

5056
chartRef.current = new ChartJS(canvasRef.current, {
5157
type,
52-
data,
58+
data: cloneData(data),
5359
options,
5460
plugins,
5561
});
@@ -119,19 +125,19 @@ function ChartComponent<
119125

120126
useEffect(() => {
121127
if (!redraw && chartRef.current && options) {
122-
chartRef.current.options = { ...options };
128+
setOptions(chartRef.current, options);
123129
}
124130
}, [redraw, options]);
125131

126132
useEffect(() => {
127133
if (!redraw && chartRef.current) {
128-
chartRef.current.config.data.labels = data.labels;
134+
setLabels(chartRef.current.config.data, data.labels);
129135
}
130136
}, [redraw, data.labels]);
131137

132138
useEffect(() => {
133139
if (!redraw && chartRef.current && data.datasets) {
134-
setNextDatasets(chartRef.current.config.data, data.datasets);
140+
setDatasets(chartRef.current.config.data, data.datasets);
135141
}
136142
}, [redraw, data.datasets]);
137143

src/utils.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type {
44
ChartData,
55
DefaultDataPoint,
66
ChartDataset,
7+
ChartOptions,
8+
Chart,
79
} from 'chart.js';
810

911
export function reforwardRef<T>(ref: ForwardedRef<T>, value: T) {
@@ -14,7 +16,26 @@ export function reforwardRef<T>(ref: ForwardedRef<T>, value: T) {
1416
}
1517
}
1618

17-
export function setNextDatasets<
19+
export function setOptions<
20+
TType extends ChartType = ChartType,
21+
TData = DefaultDataPoint<TType>,
22+
TLabel = unknown
23+
>(chart: Chart<TType, TData, TLabel>, nextOptions: ChartOptions<TType>) {
24+
chart.options = { ...nextOptions };
25+
}
26+
27+
export function setLabels<
28+
TType extends ChartType = ChartType,
29+
TData = DefaultDataPoint<TType>,
30+
TLabel = unknown
31+
>(
32+
currentData: ChartData<TType, TData, TLabel>,
33+
nextLabels: TLabel[] | undefined
34+
) {
35+
currentData.labels = nextLabels;
36+
}
37+
38+
export function setDatasets<
1839
TType extends ChartType = ChartType,
1940
TData = DefaultDataPoint<TType>,
2041
TLabel = unknown
@@ -30,10 +51,26 @@ export function setNextDatasets<
3051
);
3152

3253
// There is no original to update, so simply add new one
33-
if (!currentDataset || !nextDataset.data) return nextDataset;
54+
if (!currentDataset || !nextDataset.data) return { ...nextDataset };
3455

3556
Object.assign(currentDataset, nextDataset);
3657

3758
return currentDataset;
3859
});
3960
}
61+
62+
export function cloneData<
63+
TType extends ChartType = ChartType,
64+
TData = DefaultDataPoint<TType>,
65+
TLabel = unknown
66+
>(data: ChartData<TType, TData, TLabel>) {
67+
const nextData: ChartData<TType, TData, TLabel> = {
68+
labels: [],
69+
datasets: [],
70+
};
71+
72+
setLabels(nextData, data.labels);
73+
setDatasets(nextData, data.datasets);
74+
75+
return nextData;
76+
}

stories/Chart.data.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,48 @@ export const eventsData = {
108108
},
109109
],
110110
};
111+
112+
export const sameData1 = {
113+
labels: [
114+
'Jan',
115+
'Feb',
116+
'Mar',
117+
'Apr',
118+
'Mei',
119+
'Jun',
120+
'Jul',
121+
'Aug',
122+
'Sep',
123+
'Oct',
124+
'Nov',
125+
'Dec',
126+
],
127+
datasets: [
128+
{
129+
label: 'My First dataset',
130+
backgroundColor: 'rgba(75,192,192,0.4)',
131+
data: [33, 53, 85, 41, 44, 65, 61, 47, 52, 53, 62, 82],
132+
},
133+
{
134+
label: 'My Second dataset',
135+
backgroundColor: '#742774',
136+
data: [33, 25, 35, 51, 54, 76, 65, 40, 42, 39, 51, 55],
137+
},
138+
],
139+
};
140+
141+
export const sameData2 = {
142+
labels: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
143+
datasets: [
144+
{
145+
label: 'My First dataset',
146+
backgroundColor: 'rgba(75,192,192,0.4)',
147+
data: [42, 13, 45, 29, 44, 25, 27],
148+
},
149+
{
150+
label: 'My Second dataset',
151+
backgroundColor: '#742774',
152+
data: [33, 25, 35, 44, 50, 40, 48],
153+
},
154+
],
155+
};

stories/Chart.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React, { useState, useEffect, useReducer } from 'react';
22
import { InteractionItem } from 'chart.js';
33
import Chart from '../src';
44
import * as data from './Chart.data';
@@ -100,3 +100,17 @@ Redraw.args = {
100100
data: data.multiTypeData,
101101
redraw: true,
102102
};
103+
104+
export const SameDataToggle = args => {
105+
const [currentData, toggleData] = useReducer(
106+
prevState =>
107+
prevState === data.sameData1 ? data.sameData2 : data.sameData1,
108+
data.sameData1
109+
);
110+
111+
return <Chart {...args} data={currentData} onClick={toggleData} />;
112+
};
113+
114+
SameDataToggle.args = {
115+
type: 'bar',
116+
};

0 commit comments

Comments
 (0)