答案 0 :(得分:17)
这是使用chart.js 2.5的方法: 只需将此回调函数添加到yAxes ticks属性
即可function(value){if(Number.isInteger(value)){返回值; }}
或者只是在同一个地方使用stepSize:1。
jsFiddle:https://jsfiddle.net/alelase/6f8wrz03/2/
var ctx = document.getElementById("chart1");
var data = {
labels: ['John', 'Alex', 'Walter', 'James', 'Andrew', 'July'],
datasets: [
{
label: "Messages: ",
data: [1, 2 , 5, 4, 3, 6],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
]
}]
};
var myChart = new Chart(ctx, {
type: 'bar',
data: data,
beginAtZero: true,
options: {
responsive: true,
legend: {
display: false
},
title: {
display: false,
text: 'Chart.js bar Chart'
},
animation: {
animateScale: true
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function (value) { if (Number.isInteger(value)) { return value; } },
stepSize: 1
}
}]
}
}
});

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.5.0/Chart.min.js"></script>
<div>
<canvas id="chart1"></canvas>
</div>
&#13;
答案 1 :(得分:6)
var max= 100 //heighest value of your data
var step = 5;// According to your requirement
new Chart(ctx).Bar(plotData, {
scaleOverride: true,
scaleSteps: step,
scaleStepWidth: Math.ceil(max / step),
scaleStartValue: 0
});
答案 2 :(得分:3)
在您的示例页面中:http://qlu.in/0o3CTx/stats/
变化:
var myNewChart = new Chart(ctx).Bar(data,{});
要:
var myNewChart = new Chart(ctx).Bar(data,{
scaleOverride: true,
scaleStepWidth: 1,
scaleSteps: 10
});
可以在此处找到这些选项:http://www.chartjs.org/docs/#lineChart-chartOptions
您必须指定所有三个参数:scaleOverride
,scaleStepWidth
和scaleSteps
答案 3 :(得分:1)
对像我这样的人的快速解答:
ticks: {
beginAtZero: true,
callback: function(value) {if (value % 1 === 0) {return value;}}
}
答案 4 :(得分:0)
您可以简单地将ticks对象的precision
属性添加为:
yAxes: [
{
ticks: {
precision: 0,
},
},
],