利用getZr()属性获取滑动事件和点击事件,(需要支持es6,否则自行改写)
let obj = document.getElementById(‘xxxxxxxxxxxxxxId’) if (!obj) return let myChart = echarts.init(obj) myChart.setOption(item.option) // 点击 myChart.getZr().on(‘click’, params => { const pointInPixel = [params.offsetX, params.offsetY] if (myChart.containPixel(‘grid’, pointInPixel)) { let xIndex = myChart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0]; console.log(xIndex); } }) // 滑动 myChart.getZr().on(‘mousemove’, params => { const pointInPixel = [params.offsetX, params.offsetY] if (myChart.containPixel(‘grid’, pointInPixel)) { let xIndex = myChart.convertFromPixel({ seriesIndex: 0 }, [params.offsetX, params.offsetY])[0] alert(xIndex) } })