Sandun Harshana
Sandun Harshana

Reputation: 729

ChartJs Stacked Bar Chart

I have data for different months as bellow

Jan : [1,5,3]

Feb : [10,5]

Mar : [4,8]

Apr : [7]

May : [3,1,5,0,7]

And I want to generate bar chart as bellow

enter image description here

Right now, I have the following code and I would like to know how can I generate the bar chart as above image.

new Chart(document.getElementById("bar-chart"), {
type: 'bar',
data: {
  labels: ['Jan','Feb','Mar','Apr','May'],
  datasets: [{
      data: [1,5,3],
      label: 'Jan',
      backgroundColor: "#3e95cd",
    }
            , {
      data: [10,5],
      label: 'Feb',
      backgroundColor: "#8e5ea2",
    }, {
      data: [4,8],
      label: 'Mar',
      backgroundColor: "#4287f5",
    }
            , {
      data: [7],
      label: 'Apr',
      backgroundColor: "#23ebbc",
    }
            , {
      data: [3,1,5,0,7],
      label: 'May',
      backgroundColor: "#e63057",
    }
  ]
},
options: {
  title: {
    display: true,
    text: 'This is title'
  },
  backgroundColor:'#cfcfcf',
        scales: {
      xAxes: [{ stacked: true }],
      yAxes: [{ stacked: true }]
    }
}

});

Thank you

Upvotes: 1

Views: 5608

Answers (2)

Thanh Trung
Thanh Trung

Reputation: 3804

First data set is [10,10,8,7,3] which is the red, second is [5,5,8,0,1], third [3,0,0,0,5], fourth [0,0,0,0,7]

Of course, you must use your own skill to transform data sent by web services. But this is the final result you look for.

Upvotes: 2

David George Smith
David George Smith

Reputation: 134

I have only used ChartJS once but judging by what you want I would assume you create a bar type chart and add each of those data sets separate. for example, datasets:[{data:yourRedData}] then append that to the chart.

seams as though they do have an example of what you desire, https://www.chartjs.org/samples/latest/charts/bar/stacked.html

I checked the source code of the example to get a better understanding on how they achieved the results and here is what I found.

var barChartData = {
        labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
        datasets: [{
            label: 'Dataset 1',
            backgroundColor: window.chartColors.red,
            data: [
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor()
            ]
        }, {
            label: 'Dataset 2',
            backgroundColor: window.chartColors.blue,
            data: [
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor()
            ]
        }, {
            label: 'Dataset 3',
            backgroundColor: window.chartColors.green,
            data: [
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor(),
                randomScalingFactor()
            ]
        }]

    };
    window.onload = function() {
        var ctx = document.getElementById('canvas').getContext('2d');
        window.myBar = new Chart(ctx, {
            type: 'bar',
            data: barChartData,
            options: {
                title: {
                    display: true,
                    text: 'Chart.js Bar Chart - Stacked'
                },
                tooltips: {
                    mode: 'index',
                    intersect: false
                },
                responsive: true,
                scales: {
                    xAxes: [{
                        stacked: true,
                    }],
                    yAxes: [{
                        stacked: true
                    }]
                }
            }
        });
    };

Upvotes: 0

Related Questions