2011-07-11 126 views
2

過去幾天我一直在遇到一些問題,在某些外部文件的示例json數據中使用ajaxing來使用Highcharts庫填充餅圖。Ajax JSON進入Highcharts餅圖

這裏是我的示例JSON數據文件:data.json

[ 
    ["Apples", 43.0], 
    ["Pears", 57.0] 
    ] 

這是我實現highcharts和我的AJAX調用: (我省略了不相關的代碼)

<script type="text/javascript"> 
    $(function() { 
     var options = { 
      chart: { 
       renderTo: 'Chart', 
       defaultSeriesType: 'pie' 
      }, 
      title: { 
       text:'Fruits' 
      }, 

      plotOptions: { 
       pie: { 
        allowPointSelect: true, 
        cursor: 'pointer', 
        dataLabels: { 
         enabled: true, 
         color: '#000000', 
         connectorColor: '#000000', 

        } 
       } 
      }, 
      series: [{ 
       type: 'pie', 
       name: 'Fruits', 
       data: [] 
      }] 
     }; 

     $.getJSON('data.json', function(json) { 

      options.series.push(json);    
      var chart = new Highcharts.Chart(options); 
     }).error(function() {console.log('error');}); 


    }); 
</script> 

基本上,我想通過JSON傳入options.series []。data []。當

options.series.push(json); 

進行獲取:

[Object, Array[2]] // where the Object contains .name and .type and the Array[2] is my data 

我敢肯定,我需要這樣的:

[Object] // which contains .data , .name, .type 
+0

你解決了這個問題嗎我也有同樣的問題,如果你確實與我分享 – ddw147

回答

5

我實際上是能夠通過結構化JSON我像這樣解決我的問題:

[ 
    { 
      "type" : "pie", 
      "name" : "Fruits", 
      "data" : [ 
       [ 
        "Apple", 
        43.0 
       ], 
       [ 
        "Pear", 
        "57.0" 
       ] 
      ] 
    } 
] 

而不是做一個數組推,

我設置了一系列參數的JSON這樣的:

$.getJSON("data.json", function(json)) { 
    options.series = json; 
    var chart = new Highcharts.chart(options); 
} 
0

以防萬一任何人碰到這個運行像我一樣。我解決了與series.data.push相同的問題,因爲series也是一個數組,highcharts不會知道我們實際上正試圖將值推入數據中。