Skip to content

Commit

Permalink
fix(plugin-chart-echarts): fix unnecessary chart clearing (#1044)
Browse files Browse the repository at this point in the history
  • Loading branch information
villebro authored and zhaoyongjie committed Nov 26, 2021
1 parent a0d60c0 commit 53df21d
Showing 1 changed file with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,36 @@ export default function Echart({
height,
echartOptions,
eventHandlers,
selectedValues,
selectedValues = {},
}: EchartsProps) {
const divRef = useRef<HTMLDivElement>(null);
const chartRef = useRef<ECharts>();
const currentSelection = Object.keys(selectedValues) || [];
const previousSelection = useRef<string[]>([]);

useEffect(() => {
if (!divRef.current) return;
if (!chartRef.current) {
const chart = init(divRef.current);
chartRef.current = chart;
chartRef.current = init(divRef.current);
}

Object.entries(eventHandlers || {}).forEach(([name, handler]) => {
chartRef.current?.off(name);
chartRef.current?.on(name, handler);
});

chartRef.current.clear();
chartRef.current.setOption(echartOptions, true);

if (selectedValues) {
chartRef.current.dispatchAction({
type: 'highlight',
dataIndex: Object.keys(selectedValues),
});
}
}, [echartOptions, eventHandlers]);
chartRef.current.dispatchAction({
type: 'downplay',
dataIndex: previousSelection.current.filter(value => !currentSelection.includes(value)),
});
chartRef.current.dispatchAction({
type: 'highlight',
dataIndex: currentSelection,
});
previousSelection.current = currentSelection;
}, [echartOptions, eventHandlers, selectedValues]);

useEffect(() => {
if (chartRef.current) {
Expand Down

0 comments on commit 53df21d

Please sign in to comment.