score:1

Accepted answer

yes, you can use floating bars together with faking the scales, you can just use negative numbers for 1 of the 2 datasets and just invert all those numbers with all the callbacks:

var options = {
  type: 'bar',
  data: {
    labels: ["0-10", "10-20", "20-30"],
    datasets: [{
        label: 'girls',
        data: [
          [
            0,
            12
          ],
          [
            0,
            19
          ],
          [
            0,
            3
          ]
        ],
        backgroundcolor: 'pink',
        barpercentage: 1,
        categorypercentage: 1,
        bordercolor: 'black',
        borderwidth: 1
      },
      {
        label: 'boys',
        data: [
          [
            0, -7
          ],
          [
            0, -11
          ],
          [
            0, -5
          ]
        ],
        backgroundcolor: 'lightblue',
        barpercentage: 1,
        categorypercentage: 1,
        bordercolor: 'black',
        borderwidth: 1,
        borderskipped: 'none' // leave this line out to remove middle border at 0 line
      }
    ]
  },
  options: {
    plugins: {
      tooltip: {
        callbacks: {
          label: (ttitem) => (math.abs(ttitem.parsed.x)) // inverse negatives to positive numbers
        }
      }
    },
    indexaxis: 'y',
    scales: {
      y: {
        stacked: true,
        grid: {
          display: false
        }
      },
      x: {
        min: -20,
        max: 20,
        ticks: {
          callback: (val) => (math.abs(val)) // inverrt negatives to positive numbers
        }
      }
    }
  }
}

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


More Query from same tag