我正在尝试构建图形.
我的y轴在这里以0开头,我不知道如何配置它以及为什么它在说0 - 我看到其他帖子提到了scaleOverride:true,scaleStartValue:0.1,scaleStepWidth:5 - 我不知道怎么用在我的在代码下面,如何在chart.js中配置y轴.
任何指针都是
我有以下代码
var barChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: 'Dataset 1',
backgroundColor: "rgba(220,220,220,0.5)",
data: [6, 6, 6, 8, 6, 9, 8]
}]
};
function barChart() {
var context = document.getElementById('stacked').getContext('2d');
var myBar = new Chart(context, {
type: 'bar',
data: barChartData,
options: {
title:{
display:true,
text:"Chart.js Bar Chart - Stacked"
},
tooltips: {
mode: 'label'
},
responsive: true,
scales: {
xAxes: [{
stacked: true,
}],
yAxes: [{
stacked: true
}]
}
}
});
};
$(document).ready(function() {
$(document).ready(barChart);
});
Run Code Online (Sandbox Code Playgroud)
Sag*_*yas 13
谢谢大家帮忙,我观察到chart.js会根据你的数据自动获取y轴的值,你可以在Options> Scales下更改y/x轴设置,我还需要改变y轴的步长并且摆脱了x轴网格线,"ticks"是我正在寻找的东西,这是我的示例代码和实现所有这些的步骤.
步骤1)Canvas这是占位符,您的图表将显示在JSP上
<canvas width="1393px;" height="500px;" id="stacked"></canvas>
Run Code Online (Sandbox Code Playgroud)
步骤2)创建样本数据集(这是您需要根据您的要求创建的JSON,但请确保您提供与下面给出的完全相同的格式化JSON响应以及您的数据.
var barChartData = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: 'Dataset 1',
backgroundColor: "red",
data: [9, 6, 6, 8, 6, 9, 8]
},
{
label: 'Dataset 1',
backgroundColor: "rgba(225,226,228,0.5)",
data: [9, 6, 6, 8, 6, 9, 8]
}]
};
or you can call JSON from controller inside script as below
var jsonArry = <%=request.getAttribute("jsonArry")%>;
Run Code Online (Sandbox Code Playgroud)
步骤3)调用您在步骤2中创建的JSON
function barChart(){
var context = document.getElementById('stacked').getContext('2d');
var myBar = new Chart(context, {
type: 'bar',
data: jsonArry,
options: {
tooltips: {
mode: 'label'
},
responsive: true,
scales: {
xAxes: [{
ticks:{
stepSize : 10,
},
stacked: true,
gridLines: {
lineWidth: 0,
color: "rgba(255,255,255,0)"
}
}],
yAxes: [{
stacked: true,
ticks: {
min: 0,
stepSize: 1,
}
}]
}
}
});
};
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助,作为参考,我使用了以下文档 Chart JS
谢谢.
| 归档时间: |
|
| 查看次数: |
30975 次 |
| 最近记录: |