function draw() { const labels = [ 'January', 'February', 'March', 'April', 'May', 'June', ]; const data = { labels: labels, datasets: [{ label: 'test', backgroundColor: 'rgb(255, 99, 132)', borderColor: 'rgb(255, 99, 132)', data: [0, 10, 5, 2, 20, 30, 45], }] }; const config = { type: 'doughnut', data: data, options: {} }; const myChart = new Chart( document.getElementById('myChart'), config ); }Este es mi código y sigo recibiendo este mensaje de error:
"Error no detectado: el lienzo ya está en uso. El gráfico con ID '0' debe destruirse antes de que el lienzo con ID 'myChart' pueda reutilizarse".
Aparte de este código de error, mi gráfico puede mostrarse perfectamente sin problemas.
Llame myChart.destroy() antes de crear el nuevo gráfico . Primero declare myChart globalmente y luego inicialícelo.
let myChart = null; function draw() { const labels = [ 'January', 'February', 'March', 'April', 'May', 'June', ]; const data = { labels: labels, datasets: [{ label: 'test', backgroundColor: 'rgb(255, 99, 132)', borderColor: 'rgb(255, 99, 132)', data: [0, 10, 5, 2, 20, 30, 45], }] }; const config = { type: 'doughnut', data: data, options: {} }; if(myChart !== null){ myChart.destroy(); } myChart = new Chart( document.getElementById('myChart'), config ); }