1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
import { Box, type BoxProps, Column } from '@umami/react-zen';
import ChartJS, {
type ChartData,
type ChartOptions,
type LegendItem,
type UpdateMode,
} from 'chart.js/auto';
import { useEffect, useMemo, useRef, useState } from 'react';
import { Legend } from '@/components/metrics/Legend';
import { DEFAULT_ANIMATION_DURATION } from '@/lib/constants';
ChartJS.defaults.font.family = 'Inter';
export interface ChartProps extends BoxProps {
type?: 'bar' | 'bubble' | 'doughnut' | 'pie' | 'line' | 'polarArea' | 'radar' | 'scatter';
chartData?: ChartData & { focusLabel?: string };
chartOptions?: ChartOptions;
updateMode?: UpdateMode;
animationDuration?: number;
onTooltip?: (model: any) => void;
}
export function Chart({
type,
chartData,
animationDuration = DEFAULT_ANIMATION_DURATION,
updateMode,
onTooltip,
chartOptions,
...props
}: ChartProps) {
const canvas = useRef(null);
const chart = useRef(null);
const [legendItems, setLegendItems] = useState([]);
const options = useMemo(() => {
return {
responsive: true,
maintainAspectRatio: false,
animation: {
duration: animationDuration,
resize: {
duration: 0,
},
active: {
duration: 0,
},
},
plugins: {
legend: {
display: false,
},
tooltip: {
enabled: false,
intersect: true,
external: onTooltip,
},
},
...chartOptions,
};
}, [chartOptions]);
const handleLegendClick = (item: LegendItem) => {
if (type === 'bar') {
const { datasetIndex } = item;
const meta = chart.current.getDatasetMeta(datasetIndex);
meta.hidden =
meta.hidden === null ? !chart.current.data.datasets[datasetIndex]?.hidden : null;
} else {
const { index } = item;
const meta = chart.current.getDatasetMeta(0);
const hidden = !!meta?.data?.[index]?.hidden;
meta.data[index].hidden = !hidden;
chart.current.legend.legendItems[index].hidden = !hidden;
}
chart.current.update(updateMode);
setLegendItems(chart.current.legend.legendItems);
};
// Create chart
useEffect(() => {
if (canvas.current) {
chart.current = new ChartJS(canvas.current, {
type,
data: chartData,
options,
});
setLegendItems(chart.current.legend.legendItems);
}
return () => {
chart.current?.destroy();
};
}, []);
// Update chart
useEffect(() => {
if (chart.current && chartData) {
// Replace labels and datasets *in-place*
chart.current.data.labels = chartData.labels;
chart.current.data.datasets = chartData.datasets;
if (chartData.focusLabel !== null) {
chart.current.data.datasets.forEach((ds: { hidden: boolean; label: any }) => {
ds.hidden = chartData.focusLabel ? ds.label !== chartData.focusLabel : false;
});
}
chart.current.options = options;
chart.current.update(updateMode);
setLegendItems(chart.current.legend.legendItems);
}
}, [chartData, options, updateMode]);
return (
<Column gap="6">
<Box {...props}>
<canvas ref={canvas} />
</Box>
<Legend items={legendItems} onClick={handleLegendClick} />
</Column>
);
}
|