Jasmine Ibikunle
Jasmine Ibikunle

Reputation: 139

Remove padding from chartJs horizontal bar chart

Hey :) thanks in advance for helping me out on this issue.

I have a horizontal chart in ChartJs with some dummy info. I currently want to line it up with the statistic above it. In ChartJs the charts get rendered inside a <canvas> tag. I want to remove the spacing in the chart. I'm assuming this is padding but its not on the canvas it's within the chart itself. I've read up on the docs and tried a few of the choices.

Just some extra info, I customized the chart and it now looks like this:

enter image description here

HTML: <canvas id="myChart" width="400" height="150"></canvas>

    var ctx = document.getElementById('myChart').getContext('2d');
    var myChart = new Chart(ctx, {
        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: {
            scales: {
                yAxes: [{
                    ticks: {
                        fontColor: 'white',
                        display: true,
                        position: top,
                        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
            }
        }
    });

Upvotes: 4

Views: 13361

Answers (1)

uminder
uminder

Reputation: 26150

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>

Upvotes: 6

Related Questions