[Chartjs]-How to Reset a Chart of Chart.Js?

2👍

That is because you are creating a new instance of chart.js every time you call the function, create a outiside var chart:

   var chart;
function cargar_datos(datasL,dataP,dataR){
var ctx = $("#myChart")
chart = new Chart(ctx, {
type: 'line',
    data:
        {
        labels: datasL,
        datasets:
            [{
                label: "Rendimiento",
                borderColor: 'rgb(255, 99, 132)',
                backgroundColor: ['rgba(255,200,200,0)'],
                borderWidth: 2,
                pointBackgroundColor: "red",
                pointBorderColor: "rgba(250,10,10,0.1)",
                pointBorderWidth: "10",
                pointStyle: "rectRounded",
                data:dataP,
                },
                {
                label: "Aplicado",
                borderColor: 'rgb(0, 143, 255)',
                backgroundColor: ['rgba(112, 171, 219, 0.2)'],
                borderWidth: 2,
                pointBackgroundColor: "blue",
                pointBorderColor: "rgba(144, 140, 174, 0.3)",
                pointBorderWidth: "10",
                pointStyle: "rectRounded",
                data: dataR
            }]
    },
    options: {
        tooltips: {
                position: 'average',
                mode: 'index',
                intersect: false,
        },
    }
});
chart.destroy();
}

0👍

This works fine only when chart.destroy() is used before new Chart(), not after after it.

Leave a comment