我想在 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]) 对其进行了测试