Chartjs-JavaScript array value to chart.js graph as stacked graph

0👍

Well, you will need to rearange your data. Data structure of chart is different than this you are sending to chart. See example:

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["ASS1", "ASS2", "ASS3"],
        datasets: [{
        stack: 'Stack 0',
        data: [7, 3, 10],
        backgroundColor: 'blue'
    },
    {
        stack: 'Stack 0',
        data: [0, 2, 0],
        backgroundColor: 'green'
    }]
    },
    options: {
        legend: {
            display: false
        },
        responsive: false,
        scales: {
            xAxes: [{
                stacked: true,
            }],
            yAxes: [{
                stacked: true
            }]
        }
    }
});

That’s how your chart should look like. Each data what you add to dataset is sent to labels, so when you send arrays in array it can’t be recognized, that you want to add multiple values for one label. Group your values in way, that you have 3 values for one dataset ([‘ASS1value’, ‘ASS2value’, ‘ASS3value’]), like I’ve added in sample.

Leave a comment