11๐
The canvas is not destroyed when ever the component is unmounted and the ref
still holds a previous value. Try this instead:
const TapsGraphCard = (props: any) => {
useEffect(() => {
var myChart = new Chart(table, config);
// when component unmounts
return () => {
myChart.destroy()
}
}, [])
// ...rest of your code
};
The cleanup makes sure that when the component is unmounted the canvas is destroyed and a new instance is created once the component is mounted again.
6๐
if using react with typescript import this
import "chart.js/auto";
this will be fine for eslint @typescript
3๐
var myChart = null;
const TapsGraphCard = (props: any) =>
{
var table = "myChart";
if(myChart){
myChart.clear();
myChart.destroy();
}
myChart = new Chart(table, {
//rest of your code
});
}
- [Chartjs]-Chart.js โ draw horizontal line
- [Chartjs]-How to add images as labels to Canvas Charts using chart.js
2๐
If youโre using react-chartjs-2 make sure to add this import: import { Chart as ChartJS } from 'chart.js/auto'
This fixed the issue for me.
Refer to this answer: https://stackoverflow.com/a/70142666/6022510
0๐
When you are updating the chart make sure to call the this.chart.destroy() function first, This fixed the issue for me.
- [Chartjs]-Chart.js: Make part of labels bold
- [Chartjs]-How to modify chartjs tooltip so i can add customized strings in tooltips
Source:stackexchange.com