9

I have to create stacked bar chart using react-chartjs-2.

options : {
    maintainAspectRatio: false,
    tooltips: {
      mode: 'x-axis'
    },
    scales: {
      yAxes: [{
        ticks: {
          beginAtZero: true
        }
      }],
      xAxes: [{
        stacked: true
      }]
    }
  }

The stacked in Bar doesn't seem to work.

I am using chartjs@2.4.0

Community
  • 1
  • 1
Rahul Kumar
  • 143
  • 1
  • 2
  • 7

2 Answers2

12

 const options = {
       scales: {
            xAxes: [{
                stacked: true
            }],
            yAxes: [{
                stacked: true
            }]
        }
    }

    let data ={ 
      datasets:[{
        label: 'test1',
          data :[1]
        },
        {
          label: 'test2',
          data:  [2]   
        }],
      labels:['label']
    }
    
    render(){
      return  <Bar  data={data} options={options} />
    }
Ellen Chernilov
  • 138
  • 1
  • 5
11

Add the stack option to your datasets.

Identical stack values are stacked together.

const arbitraryStackKey = "stack1";
const data = {
  labels: ['a', 'b', 'c', 'd', 'e'],
  datasets: [
    // These two will be in the same stack.
    {
      stack: arbitraryStackKey,
      label: 'data1',
      data: [1, 2, 3, 4, 5]
    },
    {
      stack: arbitraryStackKey,
      label: 'data2',
      data: [5, 4, 3, 2, 1]   
    }
  ]
}
Austin New
  • 113
  • 1
  • 6