今天有个奇怪的需求,需要点击echart图表内容切换数据,所以百度研究了下相关的逻辑,现记个小记:
var myChart = echarts.init(document.getElementById("cardEchart"));
myChart.getZr().on("mouseup", function (param) {
let pointInPixel = [param.offsetX, param.offsetY]
let pointInGrid = myChart.convertFromPixel({ seriesIndex: 0 }, pointInPixel);
let y_index = pointInGrid[1];
console.log('坐标轴:');
console.log(pointInGrid);
var op=myChart.getOption();
console.log('x,y数据:');
console.log(op.xAxis);
console.log(op.yAxis);
console.log(op.yAxis[0].data[y_index]);
});
![点击图表](https://img-blog.csdnimg.cn/e75ff436989343b6961909fc110f4e76.png)
![点击后函数输出点击范围内X,Y轴的数据](https://img-blog.csdnimg.cn/274a2ad99d864896a0074abf41ba58a4.png)