Json反对谷歌饼图

时间:2014-11-21 16:36:46

标签: javascript c# html google-visualization

我无法绘制一个Google饼图,其中DataTable由Json对象填充。

这是我生成数据对象的代码(来自我后面的代码)。

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>

有人可以向我解释为什么没有显示。感谢。

1 个答案:

答案 0 :(得分:0)

如果没有看到你的json,很难说这是唯一的错误。但是没有正确指定回调函数。你需要改变这个:

 google.setOnLoadCallback(drawChart);

对此:

 google.setOnLoadCallback(drawPieChart);

您传递给setOnLoadCallback的参数应该是您指定的将数据与html结合的函数。在这种情况下,它是drawPieChart。