Chartjs-How to align labels at same side chartjs React

0👍

As far as I know chart.js does not provide an option to set a minimum distance for the labels, a workaround to achieve this is to add spaces the the labels of the charts where the labels are shorter as the longest one so you will get something like this:

var options = {
  type: 'bar',
  data: {
    labels: ["                Red"],
    datasets: [{
      label: '# of Votes',
      data: [12],
      backgroundColor: 'pink'
    }]
  },
  options: {
    indexAxis: 'y'
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
</body>

0👍

var options = {
  type: 'bar',
  data: {
    labels: ["                Red"],
    datasets: [{
      label: '# of Votes',
      data: [12],
      backgroundColor: 'pink'
    }]
  },
  options: {
    indexAxis: 'y'
  }
}
var options2 = {
  type: 'bar',
  data: {
    labels: ["   Yellowwwww"],
    datasets: [{
      label: '# of Votes',
      data: [12],
      backgroundColor: 'pink'
    }]
  },
  options: {
    indexAxis: 'y'
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
var ctx2 = document.getElementById('chartJSContainer2').getContext('2d');
new Chart(ctx2, options2);
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <canvas id="chartJSContainer2" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.js"></script>
</body>

Leave a comment