[Chartjs]-ChartJs โ€“ displaying data dynamically from back-end

1๐Ÿ‘

If the data comes formatted in the right way, you can just write this:

var chart = new Chart(hourlyChart, {
    type: 'bar',
    data: data: data
}

If not you could do it like so:

var chart = new Chart(hourlyChart, {
    type: 'bar',
    data: {
        labels: data.labels,
        datasets: [
            {
                label: data.datasets[0].label,
                backgroundColor: gradientBlue,
                data: data.datasets[0].data
            },
            {
                label: data.datasets[1].label,
                backgroundColor: gradientGreen,
                data: data.datasets[1].data
            }
        ]
    }
}

Leave a comment