我正在使用 Chartjs 开发一个小型 React 项目。 我想显示一条水平线来与数据进行比较。
我使用与 CodePen 相同的代码:https://codepen.io/jordanwillis/pen/qrXJLW
const myChartRef = this.chartRef.current.getContext("2d");
new Chart(myChartRef, {
type: "line",
data: {
labels: this.state.chartLabels,
datasets: [
{
label: "Quantités",
data: this.state.chartQt,
}
]
},
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js Drsw Line on Chart'
},
tooltips: {
mode: 'index',
intersect: true
},
annotation: {
annotations: [{
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: 5,
borderColor: 'rgb(75, 192, 192)',
borderWidth: 4,
label: {
enabled: false,
content: 'Test label'
}
}]
}
}
});
我做了很多研究,但我找不到什么可能有问题..
最佳答案
您的注释位于图表底部的 y 值:5 处。
这是一个codepen与注释工作。
我假设你的标签看起来像这样:
labels: [6,7,8,9,10,11],
和您的数据
data: [38, 8, 32, 25, 47,30],
然后做出注释值:
value: 30,
关于javascript - ChartJs水平线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60753605/