Akhil Kintali
Akhil Kintali

Reputation: 546

Make the x-axis values clickable in eCharts

I am using eCharts for my project and need to make them clickable to that I can call a function from there. Is there any way to do that?

enter image description here

In the above image, I should be able to click on any of the RTVP... numbers in the X-Axis to trigger a function.

Upvotes: 0

Views: 1421

Answers (1)

Sergey Fedorov
Sergey Fedorov

Reputation: 4420

Yes, all you need is love to add event handler.

  myChart.on('click', 'series', (e) => {
    console.log(e);
  })

See live example:

var myChart = echarts.init(document.getElementById('chart'));

var option = {
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'shadow'
    }
  },
  grid: {
    top: 10,
    left: '2%',
    right: '2%',
    bottom: '3%',
    containLabel: true
  },
  xAxis: [{
    type: 'category',
    data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
    axisTick: {
      alignWithLabel: true
    }
  }],
  yAxis: [{
    type: 'value',
    axisTick: {
      show: false
    }
  }],
  series: [{
    name: 'pageA',
    type: 'bar',
    data: [79, 52, 200, 334, 390, 330, 220],
    color: 'steelBlue'
  }]
};

myChart.setOption(option);

// Here you can exec any function under handler,
// OR
// _
myChart.on('click', 'series', (e) => {
    console.log(e.name)
 })
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/echarts.min.js"></script>
<div id="chart" style="width: 600px;height:400px;"></div>

Upvotes: 2

Related Questions