我正在创建一个条形图来显示百分比,因此我只希望图表达到最大值 100。
我在选项中得到以下内容:
var options = {
legendTemplate: "<ul class=\"<%=name.toLowerCase()%>-legend\"><% for (var i=0; i<segments.length; i++){%><li><span style=\"background-color:<%=segments[i].fillColor%>\"></span><%if(segments[i].label){%><%=segments[i].label%><%}%></li><%}%></ul>",
tooltipTemplate: "<%if (label){%><%=label%>: <%}%><%= value %>",
maintainAspectRatio: false,
responsive: true,
animation: false,
scaleBeginAtZero: true,
scales: {
yAxis: [{
ticks: {
beginAtZero:true,
min: 0,
max: 100
}
}]
}
};
当图表加载时,我的数据包含 100% 的值,但条形图 yAxis 值不是 100,而是上升到 120。
这不起作用的原因仅与您使用的 ChartJS 版本有关。由于您使用的是 ChartJS 版本 1.x,当前的比例选项将不适用于图表。
您应该使用以下比例选项...
scaleOverride: true,
scaleSteps: 10,
scaleStepWidth: 10,
xᴀᴍᴘʟᴇ
var ctx = document.getElementById("canvas").getContext("2d");
var data = {
labels: ['Jan', 'Feb', 'Mar'],
datasets: [{
label: "# of votes",
fillColor: "#07C",
data: [50, 80, 200]
}]
};
var myBarChart = new Chart(ctx).Bar(data, {
scaleOverride: true,
scaleSteps: 10, // number of ticks
scaleStepWidth: 10, // tick interval
scaleBeginAtZero: true
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
<canvas id="canvas" width="350" height="200"></canvas>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句