reactchartjs / react-chartjs-2

React components for Chart.js, the most popular charting library
https://react-chartjs-2.js.org
MIT License
6.49k stars 2.38k forks source link

[Bug]: Chart is flickering when update with new datapoint and using redraw={true}. #1167

Open donavan-tangible opened 1 year ago

donavan-tangible commented 1 year ago

Would you like to work on a fix?

Current and expected behavior

Hi, the only way that I found to update the chart is to use “redraw={true}” The problem is: when the graph update with new datapoint ( each second), the complete graph is flickering. This is probably because react-chartjs-2 “destroy” the chart and rerender the graph? https://github.com/reactchartjs/react-chartjs-2/blob/38f6403cc3951c4fdf0b1c398fd1347489ea2970/src/chart.tsx#L78 Do someone here experiment this issue?

Reproduction

...

chart.js version

v5.2.0

react-chartjs-2 version

v5.2.0

Possible solution

rework on redraw={true} or make ref.update() possible;

carldr commented 2 months ago

I had this same problem, but solved it by re-generating my options and data options for the Chart component in a useEffect when the data passed to the component changed.

So like :

export default function MonitorGraph({ graph_data } : PropsType ) {
  const [options, setOptions] = useState({} as ChartOptions<'line'>);
  const [data, setData] = useState({} as ChartData<'line'>);

  useEffect(() => {
    setData(...);
    setOptions(...);
  }, [ graph_data ] );

  return <Line options={options} data={data} redraw={true} />
}

The flickering went away after this. Previously, I wasn't using the useEffect, and just creating data and objects objects in the component main body.