如何在 Chartjs 中制作整数刻度

2022-01-22 00:00:00 javascript chart.js

我正在尝试制作具有整数比例的水平条形图.正如 this question 中的建议,我尝试设置 scaleOverridescaleSteps 等,但它似乎不起作用.我试图将它们放在数据集、选项中、xScales 中,但没有任何效果.

I'm trying to make a horizontal bar chart with an integer scale. As suggested in this question, I've tried to set scaleOverride, scaleSteps etc, but it seems not to be working. I tried to put them in dataset, in options, in xScales but nothing worked.

这个 jsfiddle 展示了我尝试做的事情.

This jsfiddle shows what I tried to do.

推荐答案

链接的问题是针对 Chart.js 版本 1.x.对于新版本,您需要使用 ticks 选项.请参阅 http://www.chartjs.org/docs/#scales

The linked question is for Chart.js version 1.x. For the new version you need to use the ticks option. See http://www.chartjs.org/docs/#scales

   ...
   xAxes: [{
      ticks: {
        min: 0,
        stepSize: 1,
        max: 4
      },
      ...

<小时>

小提琴 - https://jsfiddle.net/jkufz1b9/

相关文章