[Chartjs]-Updating chartJS with dynamic data

17👍

This answer will depend on how you are trying to update the information. Assuming you are using exactly as you gave, you will put a timer for every 3 hours and run this function:

function updateWeatherChart(){
    WeatherChart.data.datasets[0].data = [1, 2, 3, 4];
    WeatherChart.update();  
}

You can have something that, like in the documentation says, runs through the data and deletes it first. Or, you can just reassign it with the new information you want to update the existing chart with.

Keep in mind that instead of [1, 2, 3, 4] you will have to run code before to get the new information and replace the array with your own array. [1, 2, 3, 4] was just a test case.

-1👍

If you want to update the whole chart, you can destroy the previous chart and draw the new one.

Destroy the previous chart using myChart.destroy()

Example:

var myChart = null;

myChart && myChart.destroy(); // destroy the previous chart.

myChart = new Chart("myChart", {
    type: "bar",
    data: {
        labels: xValues,
        datasets: [{
            backgroundColor: barColors,
            data: yValues
        }]
    }}
});

Leave a comment