我正在尝试使用带有多个y轴的Charts.js添加图表,但无法正常工作。我尝试遵循所有文档,但是无论我做什么,第二个y轴都不会显示。我知道数据很好,因为两个数据集都在显示-但它仅使用第一个数据集的一个轴。这是我的JavaScript:
var myLineChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: "Total Commission",
data: d[0],
backgroundColor: background_colors,
hoverBackgroundColor: hover_background_colors,
yAxyesID: "id1"
},{
label: "# of Commissions",
data: d[1],
type: 'line',
yAxesID: "id2"
}],
},
options: {
responsive: true,
elements: {
line :{
fill: false
}
},
title: {
display: true,
position: 'bottom',
text: 'Commissions Paid',
fontSize: 14
},
scales: [{
yAxes: [{
display: true,
position: 'left',
type: "linear",
scaleLabel: {
display: true,
labelString: 'USD',
beginAtZero: true,
},
yAxisID: "id1"
},{
scaleLabel: {
display: true,
labelString: 'Commissions',
beginAtZero: true,
},
display: false,
type: "linear",
position:"right",
gridLines: {
display: false
},
yAxisID: "id2"
}]
}]
}
});
当您将鼠标悬停时,底部的小灰色圆圈会显示正确的数字,但不会创建单独的y轴以使其作为刻度。
答案 0 :(得分:4)
您的问题是错别字和错误的属性名称/类型的组合。
这是固定版本,带有注释的更改:
var myLineChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: "Total Commission",
data: d[0],
backgroundColor: background_colors,
hoverBackgroundColor: hover_background_colors,
//yAxyesID: "id1"
yAxisID: "id1" // typo in property name.
},{
label: "# of Commissions",
data: d[1],
type: 'line',
//yAxesID: "id2"
yAxisID: "id2" // typo in property name.
}],
},
options: {
responsive: true,
elements: {
line :{
fill: false
}
},
title: {
display: true,
position: 'bottom',
text: 'Commissions Paid',
fontSize: 14
},
//scales: [{
scales: { // Shouldn't be an array.
yAxes: [{
display: true,
position: 'left',
type: "linear",
scaleLabel: {
display: true,
labelString: 'USD',
beginAtZero: true,
},
//yAxisID: "id1"
id: "id1" // incorrect property name.
},{
scaleLabel: {
display: true,
labelString: 'Commissions',
beginAtZero: true,
},
//display: false,
display: true, // Hopefully don't have to explain this one.
type: "linear",
position:"right",
gridLines: {
display: false
},
//yAxisID: "id2"
id: "id2" // incorrect property name.
}]
//}]
} // Shouldn't be an array.
}
});
对我来说,这样会产生(带有伪造的输入,因为您没有提供这些输入):