简体   繁体   中英

How to populate a charts.js pie chart using json array

My json array looks like this

[{"count":2,"DepartementNom":"Finance"},{"count":1,"DepartementNom":"Technique"}]

How can I populate a chart.js pie chart using that dataset? I've tried this code but there seems to be a problem with it somewhere.

    var d = {!! json_encode($json_deco) !!};
new Chart(document.getElementById("pie-chart"), {
    type: 'pie',
    data: d,
    options: {
      title: {
        display: true,
        text: "Nombre demployés par departement"
      }
    }
});

You can not do it that way. Here in the documentation there are some examples

One of the simple ways to set the data for a chart can be done that way:

data: {
        labels: ['Finance', 'Technique'],
        datasets: [{
            data: [1, 2],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)'
            ],
            borderColor: [
                'rgba(255, 99, 132, 1)',
                'rgba(54, 162, 235, 1)'
            ],
            borderWidth: 1
        }]
    },

Working fiddle example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM