Dan*_*ana 6 bar-chart chart.js
我是ChartJS的新手,我对这个传奇有一些问题.我有一个简单的条形图,只有3个条形图,如:
<div class="x_panel">
<div class="x_title">
<h2>Bar graph</h2>
<ul class="nav navbar-right panel_toolbox" style="padding-left:5%">
<li>
<a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
</li>
<li>
<a class="close-link"><i class="fa fa-close"></i></a>
</li>
</ul>
<div class="clearfix"></div>
</div>
<div class="x_content">
<canvas id="mybarChart"></canvas>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
var mybarChart = new Chart(ctx, {
type: 'bar',
data: {
labels: [],
datasets: [{
label: '# of Votes',
backgroundColor: "#000080",
data: [80]
}, {
label: '# of Votes2',
backgroundColor: "#d3d3d3",
data: [90]
},
{
label: '# of Votes3',
backgroundColor: "#add8e6",
data: [45]
}]
},
options: {
legend: {
display: true,
labels: {
fontColor: "#000080",
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
Run Code Online (Sandbox Code Playgroud)
但我显示的图表是空的:(我也尝试通过在画布上添加另一个div并通过以下方式调用它来显示图例:
document.getElementById('barlegend').innerHTML = mybarChart.generateLegend();
Run Code Online (Sandbox Code Playgroud)
结果相同:(
我究竟做错了什么?
根据您在问题中提供的代码,您似乎忘记labels在图表数据对象中添加数据.如果没有此信息,chartjs将无法生成您的轴并将每个数据集数据映射到该轴.
此外,既然你提到你希望图例在图表下方,我添加了display: bottom选项.这是工作代码.
var ctx = document.getElementById("mybarChart").getContext("2d");
var mybarChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Votes'],
datasets: [{
label: '# of Votes',
backgroundColor: "#000080",
data: [80]
}, {
label: '# of Votes2',
backgroundColor: "#d3d3d3",
data: [90]
}, {
label: '# of Votes3',
backgroundColor: "#add8e6",
data: [45]
}]
},
options: {
legend: {
display: true,
position: 'bottom',
labels: {
fontColor: "#000080",
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个工作的codepen示例.
| 归档时间: |
|
| 查看次数: |
35072 次 |
| 最近记录: |