Chartjs-ChartJS: How to return the closest x-axis label on click

0👍

You can add a custom onclick function to the chart and then ask chartjs for the elements at that location.

See this issue for the complete answer with example: get yLabel value onclick chart js

document.getElementById("myChart").onclick = function (evt) {
        var activePoints = myChart.getElementsAtEventForMode(evt, 'point', myChart.options);
        var firstPoint = activePoints[0];
        var xLabel = myChart.data.labels[firstPoint._index];
        // Do things with your x label
    };

Leave a comment