利用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]
alert(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)
}
})

本文详细介绍了如何使用ECharts的getZr()属性来监听和处理图表上的滑动和点击事件,通过具体代码示例展示了如何获取事件参数,并在'grid'区域中转换像素坐标到数据坐标。
1434

被折叠的 条评论
为什么被折叠?



