我是否在 javascript 中正确执行此迭代?
考虑下面的两个实现,
data.addRows([
['2004', 1000],
['2005', 1170],
['2006', 660],
['2007', 1030]
]);
上面的工作原理(即)它让我得到了我想要的。
我的实现是,
for (var i = 0; i < 10; i++) {
data.addRows['row'+i,i];
}
这是一个有效的 for 循环还是我做错了什么?
我正在使用谷歌可视化 API 来绘制图表,但下面的答案不起作用,
http://jsbin.com /okafa3/edit 这是 http:// /code.google.com/apis/visualization/documentation/gallery/areachart.html
<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
for (var i = 0; i < 10; i++) {
data.addRows(['row'+i,i]);
}
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: '#FF0000'}}
});
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
Consider the two implementations below,
data.addRows([
['2004', 1000],
['2005', 1170],
['2006', 660],
['2007', 1030]
]);
The above works (ie) it gets me what i want.
and my implementation is,
for (var i = 0; i < 10; i++) {
data.addRows['row'+i,i];
}
Is this a valid for loop or what am i doing wrong?
I am using google visualization api for drawing charts but the answers below doesn't work out,
http://jsbin.com/okafa3/edit which is a copy of http://code.google.com/apis/visualization/documentation/gallery/areachart.html
<html>
<head>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
for (var i = 0; i < 10; i++) {
data.addRows(['row'+i,i]);
}
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240, title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: '#FF0000'}}
});
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
完全相同的代码将是
helios 建议的也可以工作,只不过它不是早期代码的精确表示,因为它会对 data.addRows 进行多次调用。
The exact equivalent code would be
What helios had suggested would also work except it is not exact representation of earlier code as it would make more than one call to data.addRows.
您缺少函数调用的括号:
You're missing the parenthesis of the function call:
看来你的方法 addRows 需要一个 [string, int] 数组。因此,您需要执行以下操作:(
为了清楚起见,添加了空格和输入)
It seems your method addRows needs an array of [string, int]. Thus you'll need to do:
(Spaces and enters added for clarity)