2014-11-21 42 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。