moh*_*317 48 javascript chart.js
我正在使用这个库在我的网络应用程序中绘制图表.问题是我在y轴上有小数点.您可以在下图中看到
有没有办法可以限制它只有数字?
这是我的代码
var matches = $("#matches").get(0).getContext("2d");
var data = {
labels: labelsFromCurrentDateTillLastWeek,
datasets: [
{
label: "Last Weeks Matches",
fillColor: "rgba(220,220,220,0.2)",
strokeColor: "rgba(220,220,220,1)",
pointColor: "rgba(220,220,220,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: result
}
]
};
var options = {
scaleLabel: function (label) {
return Math.round(label.value);
}
};
var myLineChart = new Chart(matches, {
type: 'bar',
data: data,
options: options
})
Run Code Online (Sandbox Code Playgroud)
Qui*_*nce 106
在chartjs 2.x中,您可以将a的选项传递@DreamTeK
给yaxis tick字段.在此,您可以检查标签是否为整数
这是一个例子
options = {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
userCallback: function(label, index, labels) {
// when the floored value is the same as the value we have a whole number
if (Math.floor(label) === label) {
return label;
}
},
}
}],
},
}
Run Code Online (Sandbox Code Playgroud)
Dre*_*TeK 32
现在可以使用以下precision
选项轻松实现:
ticks: {
precision:0
}
Run Code Online (Sandbox Code Playgroud)
根据文档。
如果已定义并且未指定stepSize,则步长将舍入到许多小数位。
Joh*_*Rix 24
另一种方法是使用fixedStepSize选项,如下所示:
options = {
scales: {
yAxes: [{
ticks: {
fixedStepSize: 1
}
}],
},
};
Run Code Online (Sandbox Code Playgroud)
小智 19
到最新版本,此选项更改为
scales: {
yAxes: [{
ticks: {
stepSize: 1,
beginAtZero: true,
},
}],
},
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28801 次 |
最近记录: |