2

I am trying to give space between the first represented data bar and the yAxes.

Working example: https://codesandbox.io/s/gracious-breeze-sc4zb?file=/src/App.js

Expected behaviour: (trying to add padding where the red is) increase the padding on the red

Matteobikk90
  • 185
  • 1
  • 14

1 Answers1

2

You can add an empty data and field to the dataset or you can use the padding.

Extra field:

var options = {
  type: 'bar',
  data: {
    labels: ["", "Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [null, 12, 19, 3, 5, 2, 3],
      backgroundColor: 'pink'
    }]
  },
  options: {}
}

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>

Padding with cros alight start:

var options = {
  type: 'bar',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      backgroundColor: 'orange'
    }]
  },
  options: {
    scales: {
      y: {
        ticks: {
          padding: 20,
          crossAlign: 'start'
        }
      }
    }
  }
}

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>
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69