Ng2-charts: How to get the value of the data after clicked

Created on 17 May 2017  路  4Comments  路  Source: valor-software/ng2-charts

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

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]);
}
}

All 4 comments

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?
active 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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

egervari picture egervari  路  4Comments

raychenfj picture raychenfj  路  3Comments

Maistho picture Maistho  路  3Comments

edarev picture edarev  路  5Comments

dslima90 picture dslima90  路  3Comments