2016-02-20 55 views
1

我想添加一个onclick监听器到HighCharts饼图。 饼图工作,onnclick也是如此。 我用返回json数据的php脚本返回图表的数据。HighCharts onclick this.point.name未定义

我的问题是,虽然我可以返回“this.x”和“this.y”值以及“this.series.name”,但我无法返回“this.point .name“值。当我尝试的时候,我可以在firebug中看到“this.point”未定义的错误。这个问题的一个例子是,如果我将Jquery警报附加到click事件处理程序,并使用“this.y”,我将返回与我的工具提示相同的值。但是,如果我尝试“this.point.name”,它会失败。下面你可以看到我的例子。你可以看到,对于工具提示和dataLabels,这个值是有效的,而不是针对系列点事件。有任何想法吗?

这里是代码:

$(document).ready(function() { 
    var options = { 
     chart: { 
      renderTo: 'pie_main', 
      plotBackgroundColor: null, 
      plotBorderWidth: null, 
      plotShadow: false 
     }, 
     title: { 
      text: 'Values by Type' 
     }, 
     tooltip: { 
      formatter: function() { 
       return '<b>'+ this.point.name +'</b>: '+ this.y; 
      } 
     }, 
     plotOptions: { 
      pie: { 
       allowPointSelect: true, 
       cursor: 'pointer', 
       dataLabels: { 
        enabled: true, 
        color: '#000000', 
        connectorColor: '#000000', 
        formatter: function() { 
         return '<b>'+ this.point.name +'</b>: '+ this.y; 
        } 
       } 
      }, 
      series: { 
       point: { 
        events: { 
         click: function() { 
          alert(this.point.name); 
         } 
        } 
       } 
      } 
     }, 
     series: [{ 
      type: 'pie', 
      name: 'Browser share', 
      data: [] 
     }] 
    } 

    $.getJSON("../php/get_pie_cnts.php", function(json) { 
     options.series[0].data = json; 
     chart = new Highcharts.Chart(options); 
    }); 

}); 

回答

4

对于您正在寻找this.name点click事件,因为this是指在其上进行点击,如the API定义的Point对象。

在两个格式化函数this中有一组非常具体的可用数据。它允许访问this.pointthis.series,其中包括the API中定义的。

请参阅this JSFiddle demonstration所有三个在使用中。

+0

谢谢!这正是我想要完成的。感谢您不仅解释,而且链接API位置。我一直在寻找它并找不到这个。 – luskbo