如何在Chart.js 2中设置坐标轴步长?

15

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

正如您所看到的,最后一个从10到20没有任何数字。我该如何设置步长?

这是我添加轴的方法:

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

谢谢。


这个回答解决了你的问题吗?Chart.js yAxes Ticks stepSize not working (fiddle) - GalAbra
2个回答

25

我该如何在这里设置步长?

直接从示例(线性刻度,步长)中获取:

通过设置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
    }
  }]
}

这里是一个实时的例子:

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>


我在 CodePen 上做了它(链接在问题中),但它没有起作用。你能否向我发送一个在我的 CodePen 中运行的示例? - Damien Monni
我可以做到,但是你为什么要有多个Y轴?这是有意为之吗?如果不是,请给我一个只有一个Y轴的CodePen。 - nicholaswmin
是的,这是故意的。我目前在我的Web应用程序中使用d3js,并希望切换到chartjs。但我不明白为什么我的第三个轴会有这种行为。 - Damien Monni
给我一个带有你的图表的 CodePen 或 JSFiddle,我会尝试修复它。 - nicholaswmin
原始问题中已经有一个链接:https://codepen.io/anon/pen/dZVgKw - Damien Monni
1
从“刻度”中删除“min/max”可以解决它。不确定原因-在这里:https://codepen.io/anon/pen/dZJvqN - nicholaswmin

0
this.data = {
labels: Xaxis,
datasets: [   
  
  {
    label: "Primary",
    data:Primyaxis,
    type: 'line',
    borderColor:'#eddb1c',
    backgroundColor:'#FFF3D6',
    fill: false,
    pointBorderColor: 'yellow',
    pointBackgroundColor: 'yellow',
    borderWidth: 1.5

  },
  {
    label: "Secondary",
    data: Secyaxis,
    type: 'line',
    borderColor:'#FF7A96',
    backgroundColor:'#EAC3CC',
    fill: false,
    pointBorderColor: 'red',
    pointBackgroundColor: 'red'
   
  }
 
 
   ]
};


   this.options = {
    scales: {
  xAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Extra Distance Interval From Origin'
    }
  }],
  yAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Percentage of Employees'
    },
    ticks: {
      // min: 50,
      max: 100,

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

  pan: {
    enabled: true,
  },
  zoom: {
    enabled: true,         
  },
responsive: true,
}}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接