2014-11-21 34 views
0

我無法繪製Google餅圖,它有一個由Json對象填充的DataTableJson對象到Google餅圖

這是我的代碼來生成數據對象(從我的代碼背後)。

public string getJsonObj() 
{ 
    Utility utils = new Utility(); 
    List<Item> dataList = new List<Item>(); 
    dataList.Add(new Item("Mosta",50)); 
    dataList.Add(new Item("Naxxar",60)); 
    string json = utils.JsonString(dataList); 
    this.ChartData.Value = json; 
    return json; 
} 

項目類

public class Item 
{ 
    private string location = ""; 
    private int frequency = 0; 

    public Item(string location, int frequency) 
    { 
     this.Location = location; 
     this.Frequency = frequency; 
    } 

    public string Location 
    { 
     get { return location; } 
     set { location = value; } 
    } 

    public int Frequency 
    { 
     get { return frequency; } 
     set { frequency = value; } 
    } 
} 

HTML

<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
    google.load("visualization", "1", { packages: ["corechart"] }); 
    google.setOnLoadCallback(drawChart); 

<script type="text/javascript"> 
    function drawPieChart() { 

     var data = google.visualization.DataTable(document.getElementById("ChartData").value)); 

     var options = { 
      title: 'Test Pie Chart' 
     }; 

     var chart = new google.visualization.PieChart(document.getElementById('piechart1')); 

     chart.draw(data, options); 
    } 
</script> 
<div id="piechart1" style="width: auto; height: auto;"></div> 

可有人請向我解釋這是爲什麼不顯示。謝謝。

回答

0

沒有看到你的json,很難說這是否是唯一的錯誤。但是你的回調函數沒有被正確指定。你需要改變這一點:

google.setOnLoadCallback(drawChart); 

要這樣:

google.setOnLoadCallback(drawPieChart); 

要傳遞到setOnLoadCallback應該是您所指定的數據嫁給HTML中的函數的參數。在這種情況下,它是drawPieChart。