Chartjs-Charts js have a bar spanning negative/positive on y-axis

1👍

This can be done since Chart.js v2.9.0, which now supports floating bars. Individual bars can now be specified with the syntax [min, max].

<html>
<head>
    <title>Floating Bars</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
    <style>
        canvas {
            -moz-user-select: none;
            -webkit-user-select: none;
            -ms-user-select: none;
        }
    </style>
</head>

<body>
    <div>
        <canvas id="canvas" height="120"></canvas>
    </div>
    <script>
    
      var chartData = {
      labels: [1,2,3,4,5,6],
      datasets: [{
        type: 'bar',
        label: 'Red Bar',
        borderColor: 'black',
        borderWidth: 1,
        backgroundColor: 'rgba(128,0,0,0.2)',
        data: [[0, 100], [-100, 100], [-30, 40], 0, 0, 0],
      }, {
        type: 'bar',
        label: 'Green Bar',
        backgroundColor: 'rgba(0,128,0,0.2)',   
        borderColor: 'black',
        borderWidth: 1,
        data: [0, 0, 0, 0, [-50, 70], 100],
      }
     ]
    };
      
      window.onload = function() {
            var ctx = document.getElementById('canvas').getContext('2d');
            window.myBar = new Chart(ctx, {
                type: 'bar',
                data: chartData,
                options: {
                  scaleBeginAtZero: false,
                  responsive: true,
                  spanGaps: true,
                  tooltips: {
                    mode: 'index',
                    intersect: true
                  },
                  scales: {
                    xAxes: [{                      
                      gridLines: {
                        display: false
                      },
                    }],
                    yAxes: [{
                      type: 'linear',
                      ticks: {
                        beginAtZero: false,
                        min:-100,
                        max:100,
                        stepSize:30
                      }
                    }],
                  }
                }
            });
        };
    </script>
</body>
</html>

Leave a comment