react-chartjs-2
react-chartjs-2 copied to clipboard
Attempting to update Chart.js chart in React using react-chartjs-2, doc is unclear
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 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