Chartjs-Chart js show levels on top. (Bar chart with Stacked group)

1👍

You need to define an additional x-axis of type: 'category' as follows:

xAxes: [{
    stacked: true,
    position: 'top'
  },
  {
    type: 'category',
    offset: true,
    labels: ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']
  }
],

Please take a look at your amended and runnable code and see how it works:

new Chart('myChart', {
  type: 'bar',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [{
        stack: 'one',
        label: 'one',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        stack: 'two',
        label: 'two',
        data: [19, 3, 5, 2, 3, 12],
        borderWidth: 1
      },
      {
        stack: 'one',
        label: 'one',
        data: [3, 5, 2, 3, 12, 19],
        borderWidth: 1
      },
      {
        stack: 'two',
        label: 'two',
        data: [2, 3, 12, 19, 3, 5],
        borderWidth: 1
      }
    ]
  },
  options: {
    legend: {
      display: false
    },
    scales: {
      xAxes: [{
          stacked: true,
          position: 'top'
        },
        {
          gridLines: {
                display: false
          },
          type: 'category',
          offset: true,
          labels: ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']         }
      ],
      yAxes: [{
        stacked: true,
      }]
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>

Leave a comment