动态添加事件到 Highcharts 系列

1 events highcharts

我能够创建我的 Highchart:

  chart1 = new Highcharts.Chart(WhateverOptions);
Run Code Online (Sandbox Code Playgroud)

我能够动态设置当用户将光标放在某个点上时要执行的函数:

   chart1.tooltip.options.formatter = function() {
     return 'Whatever I want to display in the tooltip';
   }
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种方法来指定用户单击某个点时要执行的函数。
我需要动态指定此函数,就像工具提示一样。
我不想在创建图表时将事件设置为选项的一部分。

任何帮助,将不胜感激。
谢谢。

mab*_*e02 5

我在我的申请中遇到了类似的情况。

实际上,我使用 json 生成器在服务器端生成所有数据,这不允许我在模板中编写纯 JavaScript。此外,我在多个页面上对 10 多个不同的图表使用相同的模板,但我不想为所有这些图表分配相同的行为。

因此,我需要访问和编辑图表中所有点的onclick事件。

var chart = $('#graph-container').highcharts();
// Iterate over the chart series in order to assign a point click event option
chart.series.forEach(function(s){
  s.options.point.events = {
    click: function(){
       // Store the point object into a variable
       var $$ = this;
       // Get the corresponding date in the from my x value
       var date = new Date($$.series.points[$$.index].x);
       // Do something with the date found
       console.log(date);
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

这应该适用于 Highcharts 5.x