[Chartjs]-Using Chart.js onClick function, can you execute code from an undefined or null result?

1👍

The problem is caused by this:

element = i[0];
var xValue = this.data.labels[element._index]; 
var yValue = this.data.datasets[0].data[element._index];

When clicking outside of a bar element, i[0] equals undefined and this causes setting of xValue and yValue to ‘fail’ (an error is shown in the console).

So, the code should be testing earlier for undefined and it should be testing element, not xValue:

element = i[0];
if (element === undefined) {
    document.ball.submit();
    return;
}
var xValue = this.data.labels[element._index]; 
var yValue = this.data.datasets[0].data[element._index];

Leave a comment