How to detect the selected dataset in a charts

1

I'm using the following code to detect the dataset, but INDEX is wrong, where the value would be the index of the charts and not the dataset. I need to retrieve the dataset label corresponding to what I clicked on the charts.

function GetChartLabel(events, click, dataset) {
  var event = events[0];
  if (event) {
    var activePoints = event._chart.controller.getElementsAtEvent(click);
    var chartData = activePoints[0]._chart.config.data;
    var idx = activePoints[0]._index;
    var label = chartData.datasets[idx].label;
  }
}

    
asked by anonymous 22.03.2017 / 21:18

1 answer

0

I solved the problem by applying getElementAtEvent instead of getElementsAtEvent .

The function looks like this:

var activePoint = event._chart.controller.getElementAtEvent(click);
var _datasetIndex = activePoint[0]._datasetIndex;
var label = chartData.datasets[_datasetIndex].label;
    
23.03.2017 / 20:47