Chart.js - Draw charts with opposite bars and set both the y-axis ends to positive number - Javascript Chart.js

Javascript examples for Chart.js:Bar Chart

Description

Chart.js - Draw charts with opposite bars and set both the y-axis ends to positive number

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.4.0/Chart.min.js"></script> 
      <style id="compiled-css" type="text/css">

#wrapper {/*www.  j ava 2  s  .c o  m*/
   width: 100%;
   height: auto;
}
      </style> 
      <script type="text/javascript">
    window.onload=function(){
var ctx = document.getElementById('wrapper').getContext('2d');
var chart = new Chart(ctx, {
  type: 'bar',
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "My First dataset",
      backgroundColor: 'rgb(255, 99, 132)',
      borderColor: 'rgb(255, 99, 132)',
      data: [0, 10, 5, 2, 20, 30, 45],
      stack: 'a'
    }, {
      label: "My Second dataset",
      backgroundColor: 'rgb(2, 99, 132)',
      borderColor: 'rgb(2, 99, 132)',
      data: [-10, -110, -15, -12, -120, -130, -145],
      stack: 'b'
    }]
  },
  options: {
    scales: {
      xAxes: [{
        stacked: true
      }],
      yAxes: [{
        stacked: true,
        ticks: {
          // Include a dollar sign in the ticks
          callback: function(value, index, values) {
            return value < 0 ? -value : value;
          }
        }
      }]
    }
  }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="wrapper"> 
      </canvas>  
   </body>
</html>

Related Tutorials