解决方法
这是使用chart.js 2.5的方法:
只需将此回调函数添加到yAxes ticks属性即可
只需将此回调函数添加到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,'rgba(255,206,86,'rgba(75,192,'rgba(153,102,255,159,64,0.2)' ],borderColor: [ 'rgba(255,1)',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>