echarts柱状图点击功能

1.获取echarts 

let echarts =echarts.init(document.getElementById('id名'))

2.echarts添加点击事件

const getAReaIndex = () => {

  echarts .getZr().on('click', params => {

    let pointInPixel = [params.offsetX, params.offsetY]

    if (echarts .containPixel('grid', pointInPixel)) {

      //点击第几个柱子

      let pointInGrid = echarts .convertFromPixel({ seriesIndex: 0 }, pointInPixel)

      // console.log(pointInGrid)

      // 也可以通过params.offsetY 来判断鼠标点击的位置是否是图表展示区里面的位置

      // 也可以通过name[xIndex] != undefined,name是x轴的坐标名称来判断是否还是点击的图表里面的内容

      // x轴数据的索引

      let xIndex = pointInGrid[0]

      // y轴数据的索引

      let yIndex = pointInGrid[1]

     console.log('xIndex',xIndex ,'yIndex',yIndex)

    }

  })

}

补充:给echarts设置宽高

 Object.defineProperty(document.getElementById('id名'), 'clientWidth', {

    get: function () {

      return 400

    }

  })

  Object.defineProperty(document.getElementById('id名'), 'clientHeight', {

    get: function () {

      return 300

    }

  })

欢迎补充,一起进步。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值