1
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
    );


}

This is my code and i keep getting this error message:

"Uncaught Error: Canvas is already in use. Chart with ID '0' must be destroyed before the canvas with ID 'myChart' can be reused."

Apart from this error code, my chart is able to display perfectly with no problems.

7
  • Please mention the error. The above link is not working Commented Aug 6, 2022 at 9:52
  • alright i've edited it! Commented Aug 6, 2022 at 9:58
  • 1
    In order to be able to draw another chart on the same canvas, is .destroy(). You must call it on the previously created chart object. You may also use the same variable for both charts. So before creating new chart you should destroy the canvas. Maybe calling this before making new chart will do the work myChart.destroy() Commented Aug 6, 2022 at 10:02
  • When i added myChart.destroy() before making new chart it displays another error message that states "Cannot access 'myChart' before initialization". But if i were to add myChart.destroy() at the end after i initialized 'myChart', my chart is gone and there isn't any error code for it. Commented Aug 6, 2022 at 10:14
  • Do you have two charts? Commented Aug 6, 2022 at 10:16

1 Answer 1

1

Call myChart.destroy() before creating the new Chart. First declare myChart it globally and then initialize it.

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
    );

}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi this doesn't work for me. I tried it and now its just creating many instances over and over again. etc: ID:1 ID:2 .....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.