javascript 如何使用for循环将行动态添加到Google图表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28165195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How To dynamically Add row To Google Chart with for loop
提问by user3087357
I Have Problem in Google chart .
我在 Google 图表中有问题。
I am using asp.net Mvc . After I fetch data from controller , i pass it into Google chart .
我正在使用 asp.net Mvc 。从控制器获取数据后,我将其传递到 Google 图表中。
when i use manual data , for example
例如,当我使用手动数据时
when i use this `
当我使用这个`
['Month', 'Bolivia', 'Ecuador', 'Madagascar', 'Papua New Guinea', 'Rwanda', 'Average'],
['2004/05', 165, 938, 522, 998, 450, 614.6],
It Show me correctly .
它正确显示我。
but How can i add my data dynamically into Google chart
但是如何将我的数据动态添加到 Google 图表中
this is my Code
这是我的代码
success: function (chartsdata) {
debugger;
for (var i = 0; i < chartsdata.length; i++) {
var data = google.visualization.arrayToDataTable([
['Element', 'Density', { role: 'style' }],
[chartsdata[i].MonthValue , chartsdata[i].CountValue, '#b87333']
]);
debugger;
var options = {
title: '',
};
var chart = new google.visualization.ComboChart(document.getElementById('chartdiv'));
chart.draw(data,
options);
}
采纳答案by rabin prj
The way you are doing is wrong.You should do something like below:
您这样做的方式是错误的。您应该执行以下操作:
var data=[];
var Header= ['Element', 'Density', { role: 'style' }];
data.push(Header);
for (var i = 0; i < chartsdata.length; i++) {
var temp=[];
temp.push(chartsdata[i].MonthValue);
temp.push(chartsdata[i].CountValue);
data.push(temp);
}
var chartdata = new google.visualization.arrayToDataTable(data);
回答by Manoj De Mel
you can use "google.visualization.DataTable()
" and its "addRow()
" Google code
您可以使用“ google.visualization.DataTable()
”及其“ addRow()
” 谷歌代码
var data = new google.visualization.DataTable();
data.addColumn('string', 'Month');
data.addColumn('number', 'Count');
for (var i = 0; i < chartsdata.length; i++) {
data.addRow([chartsdata[i].month, chartsdata[i].count]);
}
回答by Mayur Nirmal
Solved for me:
为我解决:
var data=[];
var Header= ['Element', 'Density', { role: 'style' }];
data.push(Header);
for (var i = 0; i < chartsdata.length; i++) {
var temp=[];
temp.push(chartsdata[i].MonthValue);
temp.push(chartsdata[i].CountValue);
temp.push("red"); //this will change based on value of 'i' variable
data.push(temp);
}
var chartdata = new google.visualization.arrayToDataTable(data);