javascript与json和谷歌图表使用while循环

时间:2015-01-20 20:34:34

标签: javascript json google-visualization

我开始使用javascript阅读本地json文件并试图将其放入谷歌图表但我遇到了while循环问题,如下面的错误评论中所述。 Var c调用一个函数来计算json文件中的数据行。

 function drawChart() {
    var soData = JSON.parse(staffOrders);
    var i = 0;
    var c = getRowCount(); //returns 5

    // Create the data table.
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Staff');
    data.addColumn('number', 'Orders Filled');
    //works
    //data.addRows([
      //['John', 3],
      //['Tommy', 1],
      //['Ken', 1],
      //['Sally', 1],
      //["Missy", 2]
    //]);

    //works
    data.addRows([
        [soData[0].staffName, Number(soData[0].orders)],
        [soData[1].staffName, Number(soData[1].orders)],
        [soData[2].staffName, Number(soData[2].orders)],
        [soData[3].staffName, Number(soData[3].orders)],
        [soData[4].staffName, Number(soData[4].orders)]
    ]);

    //***error - below doesn't render
    //also need to remove comma in last row as it isn't needed
    //data.addRows([
        //while (i <= c) {
            //[soData[i].staffName, Number(soData[i].orders)],
            //i++;
        //}
    //]);

1 个答案:

答案 0 :(得分:0)

您的代码正在尝试将while循环发送到data.addRows()。将函数调用移动到while循环

while (i <= c) {
    data.addRows([[soData[i].staffName, Number(soData[i].orders)]]);
    i++;
}