假如使用echarts顯示柱狀圖時,如果圖形數據懸殊比較大,就會出現某些柱形比較長,有些會非常短。那么短的部分會比較難點擊。
這時候,可以使用echarts提供的getZr來獲取點擊區域的位置,然后使用convertFromPixel轉換當前位置對應的x軸索引或y軸索引。
this.myChart.getZr().off('click') //最好加上
this.myChart.getZr().on('click', ({ offsetX, offsetY }) => {
const pointInPixel = [offsetX, offsetY];
if (this.myChart.containPixel('grid', pointInPixel)) {
const [, yIndex] = this.myChart.convertFromPixel({ seriesIndex: 0 }, pointInPixel);
// 邏輯代碼
}
});