看起来这是最好的解决方案:
https://codepen.io/kurkle/pen/ExxdyXQ
var ctx = document.getElementById("chart").getContext("2d");
var chart = new Chart(ctx, {
type: "horizontalBar",
data: {
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
datasets: [
{
label: "Bar1",
data: [[2.5, 5.7]],
},
{
label: "Bar2",
data: [[6, 8]],
},
{
label: "Bar3",
data: [[9,10]],
},
{
label: "Line",
type: "line",
backgroundColor: "rgb(75, 192, 192, 0.5)",
borderColor: "rgb(75, 192, 192)",
fill: false,
tension: 0,
data: [11, 10, 9, 8, 7, 6, 5, 4, 3, 2],
yAxisID: "line"
}
]
},
options: {
datasets: {
horizontalBar: {
backgroundColor: "rgb(255, 99, 132, 0.5)",
borderColor: "rgb(255, 99, 132)",
borderWidth: 1,
barPercentage: 1,
categoryPercentage: 1,
yAxisID: "bar"
}
},
scales: {
yAxes: [
{
id: "bar",
type: 'category',
stacked: true,
labels: ['bar'],
offset: true
},
{
id: "line",
position: 'right',
ticks: {
min: 0,
stepSize: 1
}
}
]
}
}
});
感谢此代码的原作者(Jukka Kurkela aka kurkle)。他在chart.js GitHub issues page 上回答了这个问题。