如何将变量作为字符串参数放入谷歌的API data.addRows('string',value)

时间:2012-12-01 20:28:57

标签: javascript asp.net

我想在data.AddRows中使用变量作为字符串参数,如此...

   function LanguagesChart(Names, Nums) {

        var data = new google.visualization.DataTable();
        data.addColumn('string', 'Language');
        data.addColumn('number', 'Employees');

        for (i = 0; i < Names.length - 1; i++) {
            data.addRows([  [Names[i], Nums[i] ] ]);  //This doesn't work
            data.addRows([  [' ' +Names[i], Nums[i] ] ]);  //This doesn't work
            data.addRows([  ['English', Nums[i] ] ]);   //This works
        }
        var options = { 'title': 'Employees Languages', 'width': 400, 'height': 300 };

        // Instantiate and draw our chart, passing in some options.
        var chart = new google.visualization.PieChart(document.getElementById('LanguageChart'));
        chart.draw(data, options);
    }

我明白了问题所在。 Javascrip没有通过参数列表获得正确的数组。我正在使用asp.net在Page_Load上发送这些数组,我从数据库中获取这些值...

      string[] langs = languages.Split(' ');
      string[] Nums = nums.Split(' ');

      string serializedLat = (new JavaScriptSerializer()).Serialize(langs);
      string serializedLong = (new JavaScriptSerializer()).Serialize(Nums);

      ScriptManager.RegisterStartupScript(this, this.GetType(), "LanguagesChart",  "LanguagesChart( " + serializedLat + ", " + serializedLong + ");", true);

Nums数组适用于js脚本,但Names数组不适用,尽管我使用alert(Names [i] + Nums [i])

进行了测试

1 个答案:

答案 0 :(得分:0)

我通过asp.net向javascript发送字符串数组,在js中我将它用作int数组,尽管它们不存在于js中。获得这些价值的正确方法是......

    var num = parseInt(Nums[i]);
    data.addRows([  [Names[i], num ] ]);

很高兴结束了!