Highcharts通過allowPointSelect選取多個點

1、設置點可選中

設置plotOptions 的 allowPointSelect 爲true,表示點可以選中。

2、獲取選中的點

通過chart.getSelectedPoints();函數來獲取選中的點,該函數返回值是一個數組,包含選中的點的所有信息。

實例代碼如下:

<!doctype html>
<html lang="en">
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <script src="http://code.highcharts.com/highcharts.js"></script>
  <script>
    $(function () {
    $('#container').highcharts({
        chart: {
        },
        xAxis: {
            categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        plotOptions: {
            series: {
                allowPointSelect: true
            }
        },
        series: [{
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]        
        }]
    });
     
     
    // the button action
    $('#111').click(function() {
        // Highcharts構造函數
        var chart = $('#container').highcharts(),
        // 獲取選中的點
        selectedPoints = chart.getSelectedPoints();
        // 選中個數
        alert('You selected '+ selectedPoints.length +' points');
         
        // 遍歷選中的點
        $.each(selectedPoints, function(i, point) {
                // category x軸值,index x軸下標值,y 軸值
            alert("category=" + point.category + ", index=" + point.x+", y="+point.y);
        })
    });
});
</script>
</head>
<body>
  <div id="container" style="min-width:700px;height:400px"></div>
  <div style="margin:10px 0px 10px 20px;">
      <button id="111">獲取選中的點</button>
  </div>
</body>
</html>

效果圖如下:


demo實例下載



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章