作者:深耐猪老汉11_245 | 来源:互联网 | 2023-05-24 18:03
我见过其他支持分组条形图的Javascript图表库,如下图所示.我没有在chart.js的在线编辑器中看到这是一个明确的选项.
是否可以在chart.js中进行此类分组条形图?这简单吗?在他们的在线编辑器中是否有模板?
1> Jacob Budin..:
是的,您可以使用datasets
属性提供多个数据集,该属性是包含值分组的数组.每个数据集包含一系列与之data
对应的值labels
.
根据您的版本,请参阅下面两个略有不同的示例datasets
.
Chart.js v1.x
var ctx = document.getElementById("myChart").getContext("2d");
var data = {
labels: ["Chocolate", "Vanilla", "Strawberry"],
datasets: [
{
label: "Blue",
fillColor: "blue",
data: [3,7,4]
},
{
label: "Red",
fillColor: "red",
data: [4,3,5]
},
{
label: "Green",
fillColor: "green",
data: [7,2,6]
}
]
};
var myBarChart = new Chart(ctx).Bar(data, { barValueSpacing: 20 });
看到这个JSFiddle.
Chart.js v2.x
var ctx = document.getElementById("myChart").getContext("2d");
var data = {
labels: ["Chocolate", "Vanilla", "Strawberry"],
datasets: [
{
label: "Blue",
backgroundColor: "blue",
data: [3,7,4]
},
{
label: "Red",
backgroundColor: "red",
data: [4,3,5]
},
{
label: "Green",
backgroundColor: "green",
data: [7,2,6]
}
]
};
var myBarChart = new Chart(ctx, {
type: 'bar',
data: data,
options: {
barValueSpacing: 20,
scales: {
yAxes: [{
ticks: {
min: 0,
}
}]
}
}
});
看到这个JSFiddle.