最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add padding to the vertical scale (X-axis) in Chart.js? - Stack Overflow

programmeradmin1浏览0评论

According to the "Tick Configuration" of Chart.js version 2.3 it is only possible to set a padding for the ticks on the Y-axis ("horizontal scale"):

padding | Number |10 | Padding between the tick label and the axis. Note: Only applicable to horizontal scales.

And this works like a charm:

scales: {
    yAxes: [{
        ticks: {
            padding: 20,
        }
    }],
    xAxes: [{
        ticks: {
             // how to set padding here?
        }
    }]
}

But the draft says, that I need some padding on the X-axis as well

How can I achieve this using Chart.js?

Maybe it's possible using a plugin?

According to the "Tick Configuration" of Chart.js version 2.3 it is only possible to set a padding for the ticks on the Y-axis ("horizontal scale"):

padding | Number |10 | Padding between the tick label and the axis. Note: Only applicable to horizontal scales.

And this works like a charm:

scales: {
    yAxes: [{
        ticks: {
            padding: 20,
        }
    }],
    xAxes: [{
        ticks: {
             // how to set padding here?
        }
    }]
}

But the draft says, that I need some padding on the X-axis as well

How can I achieve this using Chart.js?

Maybe it's possible using a plugin?

Share Improve this question edited Aug 26, 2019 at 10:08 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Nov 9, 2016 at 21:09 lampshadelampshade 2,7966 gold badges43 silver badges89 bronze badges 4
  • Were you able to get a solution for this? I'm in the same boat :-) – Jesse Schutt Commented Mar 3, 2017 at 15:51
  • @JesseSchutt Unfortunately not. – lampshade Commented Mar 29, 2017 at 12:53
  • how did you accomplish the space between tick 1 and 2? @lampshade – Alejandro Camba Commented Nov 12, 2017 at 20:34
  • @lampshade off-topic comment, but can you share your chart JS conf because I am trying to create the same chart. – Rohit Nishad Commented Jun 28, 2021 at 11:48
Add a comment  | 

5 Answers 5

Reset to default 13

Late to the game here, but the solution with latest Chart.js is to include this in your options parameter:

{
  scales: {
    xAxes: [
      {
        ticks: {
          padding: 20
        }
      }
    ]
  }
}

Add this to your options parameter:

options: {
   scales: {
       x: {
        ticks: {
           padding: 10
        }
    }
 }

Pass value to 'tickMarkLength' in options--> scales--> xAxes--> gridLines

xAxes: [{
  gridLines: {
   tickMarkLength: 10
},

The correct answer is the following for version 2.8:

options: {
    scales: {
        xAxes: [{
            ticks: {
                padding: 100
            }
        }], 
    }
}

For echarts version 5:

grid: {
    left: '3%',
    right: '10%',
    bottom: '3%',
    containLabel: true
  },
发布评论

评论列表(0)

  1. 暂无评论