0

我目前正在使用以下 JSON 使用 Google Chart API 生成图表:

{
"cols":[
    {
        "id":"DateOfBirth",
        "label":"Date of Birth",
        "type":"string"
    },
    {
        "id":"Number",
        "label":"Number",
        "type":"number"
    },
    {
        "id":"Gender",
        "label":"Gender",
        "type":"string"
    }
],
"rows":[
    {
        "c":[
            {
            "v":"F"
            },
            {
            "v":"2012-08-07"
            },
            {
            "v":"1"
            }
        ]
    },
    {
        "c":[
            {
            "v":"M"
            },
            {
            "v":"1988-07-28"
            },
            {
            "v":"1"
            }
        ]
    },
    {
        "c":[
            {
            "v":"F"
            },
            {
            "v":"1990-05-05"
            },
            {
            "v":"1"
            }
        ]
    }
]
}

但是,图表没有显示(它只显示为带有标题的白色画布)。我无法弄清楚我的 JSON 有什么问题。我还包含了用于呈现图表的代码:

// where json is the string literal
function drawChart(json) {
    // Create the data table.
    var data = new google.visualization.DataTable(json);

    // Set chart options
    var options = {'title':'Students',
        'width':400,
        'height':300,
        'view': {'columns': [0,2]}};

    // Instantiate and draw our chart, passing in some options.
    var chart = new google.visualization.PieChart($('#chart_div')[0]);
    chart.draw(data, options);
}

...我用 AJAX 调用将所有内容拼凑在一起。

$(function() {
    $.ajax({
        type: "GET",
        url: localUrl,
        success: function(data)
        {
            drawChart(data);
        }
    });
});

google.load('visualization', '1.0', {'packages':['corechart']});

我不太确定我哪里出错了。有人可以帮帮我吗?

4

1 回答 1

1

“数字”列的值不应是字符串:

'{"cols":[{"id":"Gender","label":"Gender","type":"string"},{"id":"DateOfBirth","label":"Date of Birth","type":"string"},{"id":"Number","label":"Number","type":"number"}],"rows":[{"c":[{"v":"F"},{"v":"1988-08-10"},{"v":1}]},{"c":[{"v":"M"},{"v":"1988-07-28"},{"v":1}]},{"c":[{"v":"F"},{"v":"1988-08-10"},{"v":1}]}]}'

您可以在 Google Playground 中为Table尝试此操作。如果你想要一个PieChart,3 列是行不通的;删除“DateOfBirth”列正在工作:

'{"cols":[{"id":"Gender","label":"Gender","type":"string"},{"id":"Number","label":"Number","type":"number"}],"rows":[{"c":[{"v":"F"},{"v":1}]},{"c":[{"v":"M"},{"v":1}]},{"c":[{"v":"F"},{"v":1}]}]}'
于 2012-08-12T00:02:10.800 回答