垂直バー(グリーンバー)上に単一のバーを描きたい。またannotation
が、その動作していないを使用してみました、私は線を描画するために、私はいくつかのコードを見つけグラフJs V2描画水平バー(平均)垂直バー上
にチャートJS V2を使用していますが、その私が使用角度4.
で働いていません。 コマンドは、注釈を追加する:NPMインストールchartjs-プラグイン注釈以下--save
は垂直バーを描画するために正常に動作し、私のコードです。誰も私にそれの上に水平線を描くのを助けることができますか?
回答:
その後import 'chartjs-plugin-annotation'
npm install chartjs-plugin-annotation --save
をインストールします。
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
}
}]
}
}
});
緑色のバーは正確にy軸上に配置する必要がありますか? – maxpaj
@maxpaj平均値として、どこでもかまいません。 –
chart.jsでは、レイヤーを使用して複数のチャートをまとめて描画することができます。棒グラフの上に折れ線グラフを配置することができます。これをチェックしてください:http://www.chartjs.org/docs/latest/charts/mixed.html – pokeybit