I am looking for a way to register a click event on xAxis labels. User should be able to click on xAxis label(not on the series) and we should be able to call a function.
Either a direct way or indirect way should be fine.
You can listen almost any events. Just enable the Axis events and filter events by params. Namely, you need to do:
option = {
  // ...
  xAxis: {
    // ...
    triggerEvent: true
  }
}
click events from the chart instance and check targetType to get all of the label's events:myChart.on('click', params => {
  if(params.targetType === 'axisLabel'){
    if(params.value === 'MyLabel #3'){
      console.log("Hello, is it me you're looking for");
      // Do something ...
    }
  }
})
Also you can use the shorthands and drop the if:
myChart.on('click', { targetType: 'axisLabel', value: 'Category1' }, params => {
  console.log(params.value);
})
var myChart = echarts.init(document.getElementById('main'));
  var option = {
      title: {
          text: 'ECharts'
      },
      tooltip: {},
      xAxis: {
          data: ["Category1","Category2","Category3"],
          triggerEvent: true
      },
      yAxis: {},
      series: [{
          name: 'Series',
          type: 'bar',
          data: [5, 20, 36]
      }]
  };
  myChart.setOption(option);
  
  myChart.on('click', params => {
  	if(params.targetType === 'axisLabel'){
      console.log(params.value);    	
    }
  })
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script>
<div id="main" style="width: 600px;height:400px;"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With