How to set axes' step size in Chart.js 2?

删除回忆录丶 提交于 2020-08-04 04:26:06

问题


I created a simple line chart using chart.js with 3 Y axes: https://codepen.io/anon/pen/dZVgKw

As you can see, the last one is going from 10 to 20 without any number between. How can I set step size here?

This is how I add an axe:

{
  id: 'C',
  type: 'linear',
  position: 'left',
  ticks: {
    max: 10,
    min: 20,
  },
}

Thanks.


回答1:


How can I set step size here?

Straight from the samples (Linear Scale, step size):

By setting a stepSize value.

scales: {
  xAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Month'
    }
  }],
  yAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Value'
    },
    ticks: {
      min: 0,
      max: 100,

      // forces step size to be 5 units
      stepSize: 5 // <----- This prop sets the stepSize
    }
  }]
}

Here's a live example:

var ctx = document.getElementById('chartJSContainer').getContext('2d')

new Chart(ctx, {
  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: {
      yAxes: [{
        ticks: {
          reverse: false,
          stepSize: 3
        },
      }]
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<body>
    <canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>


来源:https://stackoverflow.com/questions/47334236/how-to-set-axes-step-size-in-chart-js-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!