Chartjs-How can I set the category height of a horizontalBar chart?

0👍

You can define an array of values for barPercentage and/or categoryPercentage inside each dataset of your stacked horizontal bar chart. The arrays should contain an entry for each data value, all of them identical except the last one.

categoryPercentage: [0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 1]

Please have a loot at the code sample below.

new Chart(document.getElementById('myChart'), {
  type: 'horizontalBar',
  data: {
    labels: ['A', 'B', 'C', 'D', 'E', 'F', 'Others'],
    datasets: [{
      label: 'Money in',
      data: [1, 1, 1, 1, 1, 1, 2],
      backgroundColor: 'rgb(26,121,191)',
      categoryPercentage: [0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 1]
    },
    {
      label: 'Money in',
      data: [1, 1, 1, 1, 1, 1, 0],      
      backgroundColor: 'rgb(0,51,160)',
      categoryPercentage: [0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 1]
    }]
  },
  options: {
    scales: {
      yAxes: [{
        stacked: true   
      }],
      xAxes: [{
        stacked: true,
        ticks: {          
          beginAtZero: true,
          stepSize: 1
        }
      }]
    },
    legend: {
      display: false
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script> 
<canvas id="myChart" height="100"></canvas>

Leave a comment