[Chartjs]-Bar Chart Not Stacking When Using ChartJs

3👍

Stacked option only works if you use multiple datasets. You can put each stacked value in a seperate dataset so you can stack them.

const ctx = document.getElementById('chart-canvas').getContext('2d');
const chartCanvas = () => {
   
const chart = new Chart(ctx, {

    type: 'bar',
    // The data for our dataset
    data: {
        labels: ['First Data'],
        datasets: [
            {
                label: 'a',
                data: [49999], 
                backgroundColor: ['rgba(255, 99, 132'],
                borderColor:     ['rgb(255, 99, 132)'],
            },
            {
                label: 'b',
                data: [20000], 
                backgroundColor: ['rgba(25, 99, 132, .5)'],
                borderColor:     ['rgb(25, 99, 132)'],
            },
            {
                label: 'c',
                data: [35000], 
                backgroundColor: ['rgba(255, 9, 12, .5)'],
                borderColor:     ['rgb(255, 9, 12)'],
            }
        ]
    },

    // Configuration options go here
    options: {
        scales: {
            xAxes: [{
              stacked: true
            }],
            yAxes: [{
              stacked: true
            }]
          },
        legend: {
            display: false,
        },
        maintainAspectRatio: false,
    }
});
}
chartCanvas()

Leave a comment