How to detect the selected dataset in a Charts

Asked

Viewed 79 times

1

I am using the following code to detect the dataset, however INDEX comes wrong, where the value would be the Charts index and not the dataset. I need to retrieve the label of the dataset corresponding to the one I clicked on 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;
  }
}

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

0


I solved the problem by applying the getElementAtEvent instead of getElementsAtEvent.

The function was thus:

var activePoint = event._chart.controller.getElementAtEvent(click);
var _datasetIndex = activePoint[0]._datasetIndex;
var label = chartData.datasets[_datasetIndex].label;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.