data point index will not work if the value is 0. For example, if you have an apex bar chart but the value of the bar chart is 0.Then datapoint index will be -1. You may still want to click on the x axis label. In that case, this will work.
chartOptions: {
chart: {
id:'barChart',
type: 'bar',
height: 250,
events: {
click: function (event, chartContext, config) {
if(config.dataPointIndex <0){
var chartName = event.target.firstChild.data
console.log(labelName)
}
else{
var chartName = config.dataPointIndex
}
}
}
}
}
Otherwise, go with config.datapointindex to click on the bar chart but to click on the labels the best is to use event.target.firstChild.data
I have written an article config.dataPointIndex and how to click on bar charts but with Vue JS. Same logic though https://samchowdhury.medium.com/how-do-you-make-a-bar-chart-clickable-in-apex-chart-with-vue-js-3ab3e0730e94 Hope this helps!