Highcharts饼图获取选定的饼图ID

时间:2013-03-20 20:17:01

标签: highcharts

如何获取Highcharts饼图获取所选饼图ID?

我有一个数据数组

var data= { name: series[i], y: Data[i],id:categorydata[i] };

渲染图表

new Highcharts.Chart({ ....

  ....  
series: [{
                type: 'pie',
                name: 'Category',
                data: data
            }]
  });

如何获取所选饼图的ID。

我在

中这样做
plotOptions: {
                series: {
                    animation: false,
                    events:{
                        click: function (event) {
                            var point = this;
                            //How do I Access the id??????? 
                            alert('value: ' + this.series);


                        }
                    }
                },

1 个答案:

答案 0 :(得分:25)

您希望在点配置上进行事件处理,而不是系列。每个楔形都是一个系列中的一个点:

   var data = [{ name: 'One', y: 10, id: 0 },{ name: 'Two', y: 10, id: 1 }];

   // some other code here...

   series:[
      {
         "data": data,
          type: 'pie',
          animation: false,
          point:{
              events:{
                  click: function (event) {
                      alert(this.x + " " + this.y);
                  }
              }
          }          
      }
   ],

小提琴here

完整运行代码:

var chart;
point = null;
$(document).ready(function () {

    var data = [{ name: 'One', y: 10, id: 0 },{ name: 'Two', y: 10, id: 1 }];
    
    chart = new Highcharts.Chart(
    {
       series:[
          {
             "data": data,
              type: 'pie',
              animation: false,
              point:{
                  events:{
                      click: function (event) {
                          alert(this.id);
                      }
                  }
              }          
          }
       ],
       "chart":{
          "renderTo":"container"
       },
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<div id="container" style="width: 320px; height: 200px"></div>