迭代json对象绘制柱形图?

时间:2010-08-20 09:23:47

标签: json object charts iteration google-visualization

我用,

google.load('visualization', '1', {'packages': ['columnchart']});
//google.setOnLoadCallback(drawChart);
function drawChart(response) {
    alert(response.customerlist);
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'dbZipcode');
    data.addColumn('string', 'countusers');
    data.addRows(response.customerlist.length);
    for (var i = 0; i < response.customerlist.length; i++) {
        data.setValue(i, 0, response.customerlist[i].dbZipcode);
        data.setValue(i, 1, response.customerlist[i].countusers);
    }
    var container = document.getElementById('spnChart');
    container.innerHTML = "";
    var chart = new google.visualization.ColumnChart(container); 
    chart.draw(data, {width: 800, height: 300, is3D: true, title: 'Page Views',
                      legend:'none', titleX:'ZipCode', titleY:'Total Users'});
}

但我的图表不包含绘制的图表。任何建议。 alt text

和我的json数据,

{
    "customerlist": [
        { "dbZipcode": "21313213",     "countusers": "1" },
        { "dbZipcode": "2313213",      "countusers": "1" },
        { "dbZipcode": "23324324",     "countusers": "1" },
        { "dbZipcode": "2342432",      "countusers": "1" },
        { "dbZipcode": "32424422343",  "countusers": "1" },
        { "dbZipcode": "324324324",    "countusers": "1" },
        { "dbZipcode": "324324324324", "countusers": "1" },
        { "dbZipcode": "3244324",      "countusers": "1" },
        { "dbZipcode": "3422344",      "countusers": "1" },
        { "dbZipcode": "342424324",    "countusers": "1" },
        { "dbZipcode": "3424324",      "countusers": "1" },
        { "dbZipcode": "435345",       "countusers": "1" },
        { "dbZipcode": "627028",       "countusers": "1" },
        { "dbZipcode": "641030",       "countusers": "2" },
        { "dbZipcode": "642582",       "countusers": "1" }
    ]
}

1 个答案:

答案 0 :(得分:0)

改变,

data.addColumn('number', 'countusers');
    data.addRows(response.customerlist.length);
    for (var i = 0; i < response.customerlist.length; i++) {
        data.setValue(i, 0, response.customerlist[i].dbZipcode);
        data.setValue(i, 1, response.customerlist[i].countusers-0);
    }