javascript - How to increase space between label and chart area? - Stack Overflow

All my labels are on the top of the bars. I can see this:but I want that would be like this:Padding doe

All my labels are on the top of the bars. I can see this:

but I want that would be like this:

Padding doesn't work for xAxes but works for yAxes.

    legend: {display: false},
    scales: {
      xAxes: [{
        position: 'top',
        stacked: true,
        ticks: {
          stepSize: 1,
          min: 0,
          autoSkip: false,
          fontColor: '#3f7ba2',
          fontStyle: 600,
          fontFamily: 'Italic',
        },
        gridLines: {
          color: '#dedfe7',
          tickMarkLength: 0,
        }
      }],
      yAxes: [{
        stacked: true,
        ticks: {
          min: 0,
          fontColor: '#62aae8',
          padding: 5
        },
        gridLines: {
          color: '#dedfe7' //b5cce2
        }
      }],
    }

All my labels are on the top of the bars. I can see this:

but I want that would be like this:

Padding doesn't work for xAxes but works for yAxes.

    legend: {display: false},
    scales: {
      xAxes: [{
        position: 'top',
        stacked: true,
        ticks: {
          stepSize: 1,
          min: 0,
          autoSkip: false,
          fontColor: '#3f7ba2',
          fontStyle: 600,
          fontFamily: 'Italic',
        },
        gridLines: {
          color: '#dedfe7',
          tickMarkLength: 0,
        }
      }],
      yAxes: [{
        stacked: true,
        ticks: {
          min: 0,
          fontColor: '#62aae8',
          padding: 5
        },
        gridLines: {
          color: '#dedfe7' //b5cce2
        }
      }],
    }
Share Improve this question edited Feb 19, 2024 at 12:32 Владислав asked Jan 27, 2022 at 13:01 ВладиславВладислав 551 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

This is an issue that only persists in chart.js version 2.6.0 and lower. As you can read in the release notes under the bug fixes it says that in PR 4403 ticks.padding option now applies to vertical and horizontal scales.

So to solve your issue you will need to update to a version of chart.js higher or equal to 2.7.0

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      xAxes: [{
        position: 'top',
        ticks: {
          padding: 100
        }
      }],
    }
  }
}

const ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare./ajax/libs/Chart.js/2.7.0/Chart.js"></script>
</body>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745225299a4617430.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信