6

We are using Chart.js 2.1.4 in a project we are working on. When trying to get the point where a line chart was clicked on, the getElementsAtEvent function is returning an empty Array. I understand this is expected behavior if the user clicks arbitrarily on the chart, but it is also occurring even if the user clicks on a datapoint. Here is our onClick event handling:

options: {
    onClick: function(evt){
        var firstPoint = this.getElementAtEvent(evt);

        if (firstPoint) {
            console.log(firstPoint);
        } else {
            console.log('No point returned');
        }
    },

In the above code, firstPoint is an Array object with no items in it.

remnar
  • 61
  • 5
  • Try: var firstPoint = this.whateverYourChartVariableIs.chart.getElementAtEvent(event); Don't think this.getelementAtEvent is a function. – Aphire Jan 16 '19 at 11:30

1 Answers1

0

getElementsAtXAxis will solve your problem.

  1. https://stackoverflow.com/a/61962574/2251840
ABCD
  • 7,914
  • 9
  • 54
  • 90