[Chartjs]-Remove padding from chartJs horizontal bar chart

5👍

You can define the layout with negative left padding as follows:

options: {
    layout: {
      padding: {
        left: -10
      }
    },
    ...

Please have a look at the following code snippet where the chart canvas is defined with a border in order to emphasize the alignment with the preceding text.

var myChart = new Chart(document.getElementById('myChart'), {
  type: 'horizontalBar',
  data: {
    labels: ['1', '2'],
    datasets: [{
      label: 'Money in',
      data: [5, 19],
      backgroundColor: ['rgb(0,51,160)', 'rgb(26,121,191)'],
      borderWidth: 0
    }]
  },
  options: {
    layout: {
      padding: {
        left: -10
      }
    },
    scales: {
      yAxes: [{
        ticks: {
          fontColor: 'white',
          mirror: true,
          beginAtZero: true,
          fontSize: 17,
          padding: -9,
          z: 1
        },
        gridLines: {
          display: false
        }
      }],
      xAxes: [{
        gridLines: {
          display: false
        },
        ticks: {          
          beginAtZero: true,
          display: false
        }
      }]
    },
    legend: {
      display: false
    }
  }
});
canvas{ 
  max-width: 300px;
  border: 1px solid #000;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.js"></script> 
<p>Text here</p>
<canvas id="myChart" width="400" height="150"></canvas>

Leave a comment