myChart.getZr().on('click', function (params) {
let pointInPixel = [params.offsetX, params.offsetY]
// console.log(myChart.containPixel('grid', pointInPixel))
let pointInGrid = myChart.convertFromPixel({ seriesIndex: 0 }, pointInPixel)
// console.log(pointInGrid)
// 也可以通过params.offsetY 来判断鼠标点击的位置是否是图表展示区里面的位置
// 也可以通过name[xIndex] != undefined,name是x轴的坐标名称来判断是否还是点击的图表里面的内容
// x轴数据的索引
let xIndex = pointInGrid[0]
// y轴数据的索引
let yIndex = pointInGrid[1]
// 拿到x轴和y轴的下标之后可以通过下标拿到坐标的字段名或者字段值
})
myChart.getZr().on('mousemove', params => {
var pointInPixel = [params.offsetX, params.offsetY];
if (myChart.containPixel('grid', pointInPixel)) {//若鼠标滑过区域位置在当前图表范围内 鼠标设置为小手
myChart.getZr().setCursorStyle('pointer')
} else {
myChart.getZr().setCursorStyle('default')
}
})
项目中遇到一个需求,echarts横向柱状图设置点击事件进行操作,使用常用的myChart.on(“click”)的话,只有柱状图有值的时候才有点击事件,当柱状图的数据为0时,无法进行点击。后来用getZr()方法可以点击柱子之外的地方进行点击操作。
使用上面的代码已解决项目中的需求!问题虽小,以此记录,希望可以帮助到有需要的人!