10

如何获取 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);


                        }
                    }
                },
4

1 回答 1

29

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

   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);
                  }
              }
          }          
      }
   ],

在这里拉小提琴。

完整运行代码:

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>

于 2013-03-20T21:09:12.283 回答