Chart.js 如何使多个堆叠图表共享同一yAxis?
我有两个不同数据的图表,相互叠加。如何使这两个图表共享1个y轴的相同比例 我将y轴配置如下:Chart.js 如何使多个堆叠图表共享同一yAxis?,chart.js,chartjs-2.6.0,Chart.js,Chartjs 2.6.0,我有两个不同数据的图表,相互叠加。如何使这两个图表共享1个y轴的相同比例 我将y轴配置如下: yAxes: [{ display: true, stacked: true, type: "linear", scaleLabel: { display: true, labelString: "Amount in USD" }, ticks: { beginAtZero: true, max: 15000
yAxes: [{
display: true,
stacked: true,
type: "linear",
scaleLabel: {
display: true,
labelString: "Amount in USD"
},
ticks: {
beginAtZero: true,
max: 150000,
callback: function(label, index, labels) {
return label / 1000 + "k";
}
}
}]
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
backgroundColor: "#FFE0B2",
datasets: [{
label: "Amount of Sales - YTD",
data: [0, 0, 0, 0, 0, 0, 0, 104920, 87461, 22700, 0, 0],
backgroundColor: [
"#FFE0B2"
],
borderColor: [
"#FF9800"
],
borderWidth: 1,
pointBackgroundColor: "#FFE0B2",
pointBorderColor: "#FF9800",
pointBorderWidth: 2
}, {
label: "Amount of Commission - YTD",
data: [0, 0, 0, 0, 0, 0, 0, 1896, 4373, 1135, 0, 0],
backgroundColor: [
"#F8BBD0"
],
borderColor: [
"#E91E63"
],
borderWidth: 1,
pointBackgroundColor: "#F8BBD0",
pointBorderColor: "#E91E63",
pointBorderWidth: 2
}]
数据集如下所示:
yAxes: [{
display: true,
stacked: true,
type: "linear",
scaleLabel: {
display: true,
labelString: "Amount in USD"
},
ticks: {
beginAtZero: true,
max: 150000,
callback: function(label, index, labels) {
return label / 1000 + "k";
}
}
}]
labels: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
backgroundColor: "#FFE0B2",
datasets: [{
label: "Amount of Sales - YTD",
data: [0, 0, 0, 0, 0, 0, 0, 104920, 87461, 22700, 0, 0],
backgroundColor: [
"#FFE0B2"
],
borderColor: [
"#FF9800"
],
borderWidth: 1,
pointBackgroundColor: "#FFE0B2",
pointBorderColor: "#FF9800",
pointBorderWidth: 2
}, {
label: "Amount of Commission - YTD",
data: [0, 0, 0, 0, 0, 0, 0, 1896, 4373, 1135, 0, 0],
backgroundColor: [
"#F8BBD0"
],
borderColor: [
"#E91E63"
],
borderWidth: 1,
pointBackgroundColor: "#F8BBD0",
pointBorderColor: "#E91E63",
pointBorderWidth: 2
}]
请看一看:
如您所见,在8月份,点(橙色)设置为~100k,这是正确的
然而,根据图表,该点(粉红色)的值仅为~1.8k,并且该点几乎为~100k
问题:如何设置它,使粉色图形与橙色图形共享相同的y轴?问题在于y轴中的这条线:
stacked: true,
如果将其删除,则不会将值堆叠在一起。
但是,由于您在数据集上指定了
backgroundColor
,并且第一个数据集是具有较高值的“销售额-年初至今”,因此它将阻止第二个数据集的可视性。如果两个数据集的值彼此太远/太远(如您的示例中所示),您可以考虑在数据集上使用比例因子,使用多个Y轴,或者使用支持缩放比例的不同图表组件(如DevExtreme或CANVAJS)。