[Chartjs]-How to make bar chart cover multiple labels?

1👍

You can achieve this by adding a second x axis where you link to.

Example:

var options = {
  type: 'bar',
  data: {
    labels: ["Month-1", "", "", "", "", "", "Month-2", "", "", "", "", "", "Month-3", "", "", "", "", "", "Month-4", "", "", "", "", ""],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5],
      borderWidth: 1,
      xAxisID: 'x2',
      categoryPercentage: 1,
    }]
  },
  options: {
    scales: {
      x: {},
      x2: {
        display: false,
        labels: ["", "", "", ""]
      }
    }
  }
}

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.3.0/chart.js"></script>
</body>

Leave a comment