如何在 Chart.js 2 中设置轴的步长? [英] How to set axes' step size in Chart.js 2?

查看:12
本文介绍了如何在 Chart.js 2 中设置轴的步长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有 3 个 Y 轴的 chart.js 创建了一个简单的折线图:https://codepen.io/anon/pen/dZVgKw

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

如您所见,最后一个是从 10 到 20,中间没有任何数字.如何在此处设置步长?

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,
  },
}

谢谢.

推荐答案

如何在此处设置步长?

How can I set step size here?

直接来自样本(线性比例,步长):

通过设置 stepSize 值.

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>

这篇关于如何在 Chart.js 2 中设置轴的步长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆