我想在垂直条上绘制一个条(绿色条)。我正在使用 Chart JS V2 和 Angular 4。
![enter image description here](https://i.stack.imgur.com/yo2qh.png)
我找到了一些画线的代码,但它在 Angular 4 中不起作用。
我用过也尝试过使用annotation
但它不起作用。
添加注释的命令:npm 安装 Chartjs-plugin-annotation --save
下面是我的代码,可以很好地仅绘制垂直条。谁能帮我在上面画一条水平线。
Answer:
Install npm install chartjs-plugin-annotation --save
Then import 'chartjs-plugin-annotation'
;
this.ctx = document.getElementById("myChart");
this.myChart = new Chart(this.ctx, {
type: 'bar',
data: {
labels: this.barData.getLabels(),
datasets: [{
label: this.barData.actualLegendLabel,
data: this.barData.getLineData(),
backgroundColor: this.backgroundColorBarOne,
borderColor: [
'rgba(81,117, 194,1)',
]}]
},
options: {
scales: {
responsive: true,
scaleBeginAtZero: false,
barBeginAtOrigin: true,
yAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: false
}
}],
xAxes: [{
ticks: {
beginAtZero: true
},
gridLines: {
display: false
}
}]
},
legend: {
cursor: "line",
position: 'top',
labels: {
fontSize: 10,
}
},
layout: {
padding: {
left: 3,
right: 3,
top: 5,
bottom: 5
}
}, annotation: {
annotations: [{
drawTime: 'afterDraw', // overrides annotation.drawTime if set
id: 'a-line-1', // optional
type: 'line',
mode: 'horizontal',
scaleID: 'y-axis-0',
value: '25',
borderColor: 'red',
borderWidth: 2,
// Fires when the user clicks this annotation on the chart
// (be sure to enable the event in the events array below).
onClick: function(e) {
// `this` is bound to the annotation element
}
}]
}
}
});