Hi
I would like to get the data value of a bar(for example) after clicked.
I have managed to get the active elements, but I see data such as label/ datasetLabel ..
but not the the actual value of the data.
Any idea?
Thanks
Hi,
That's how you can get a value and a label of selected element in line chart.
private chartClicked(e:any):void {
if(e.active.length > 0) {
console.log("Index", e.active[0]._index);
console.log("Data" , e.active[0]._chart.config.data.datasets[0].data[e.active[0]._index]);
console.log("Label" , e.active[0]._chart.config.data.labels[e.active[0]._index]);
}
}
@artemkosenko There will be multiple acitve in an array, if more than one point at the same x coordinate. How to get the value of the specific point?
Got it.
options = {
hover: {
mode: "nearest",
intersec: true,
},
interaction: {
mode: "nearest",
},
}
And thanks to @artemkosenko , now get the value of data by the following code
let chartData = {
data:[],
}
chartData.data.push({
x:2,
y:3,
dataObject: someObject,
})
public chartClicked(e: any){
if (e.active.length > 0){
let datasetIndex = e.active[0]._datasetIndex
let dataIndex = e.active[0]._index
let dataObject = this.lineChartData[datasetIndex].data[dataIndex].dataObject
console.log(dataObject)
}
}
@sharpevo - This solution worked correctly. Thanks!
Most helpful comment
Hi,
That's how you can get a value and a label of selected element in line chart.
private chartClicked(e:any):void {
if(e.active.length > 0) {
console.log("Index", e.active[0]._index);
console.log("Data" , e.active[0]._chart.config.data.datasets[0].data[e.active[0]._index]);
console.log("Label" , e.active[0]._chart.config.data.labels[e.active[0]._index]);
}
}