react-chartjs-2 icon indicating copy to clipboard operation
react-chartjs-2 copied to clipboard

Attempting to update Chart.js chart in React using react-chartjs-2, doc is unclear

Open jscheinhorn opened this issue 5 years ago • 1 comments

Hello, I'm just getting started with chart-reactjs-2. I am dynamically loading data onto the chart and need it to update with each render.

I read the following:

 chartReference = {};
 
componentDidMount() {
  console.log(this.chartReference); // returns a Chart.js instance reference
}
 
render() {
  return (<Doughnut ref={(reference) => this.chartReference = reference } data={data} />)

and would like to do something like:

let lineChart = this.chartReference.chartInstance
lineChart.update();

but I don't know what to include within chartReference. Any guidance is greatly appreciated!

jscheinhorn avatar Jun 23 '19 13:06 jscheinhorn

@jscheinhorn Is there a specific reason you need to imperatively call the update() method on each render call? If your data prop changes then the wrapper will automatically update the chart: https://github.com/jerairrest/react-chartjs-2/blob/master/src/index.js#L237

Pringels avatar Dec 03 '19 14:12 Pringels