首页 > 解决方案 > Highcharts - 如何获得点击事件的最近点

问题描述

在这个 Highcharts 图表 ( jsfiddle ) 中有两个事件。mouseOverclick

通过在图表上水平移动鼠标,工具提示和mouseOver事件会捕捉到图表上最近的点。clicking我怎样才能在图表上的任何地方得到这一点?

tooltip: {
    shared: true
},
plotOptions: {
    series: {
        cursor: 'pointer',
        point: {
             events: {
                 click: function() {
                    console.log( 'click', this.x, this.y );
                },
                mouseOver: function() {
                    console.log( 'mouseover', this.x, this.y );
                }
            }
        }
    }
}

在这种情况下,它甚至不会触发点击事件。

为此,我需要将点击事件添加到主图表。

chart: {
    events: {
        click: function (event) {
            console.log( 'click', event.x, event.y );
        }
    }
}

但是我怎样才能得到最近的点呢?

标签: javascripthighcharts

解决方案


尝试以这种方式使用图表上的点击事件:

  chart: {
    events: {
      click: function() {
                console.log(this.hoverPoint)
      }
    }
  },

其中 hoverPoint 是一种附加悬停点的“状态”。

演示:https ://jsfiddle.net/BlackLabel/a4qjx91o/


推荐阅读