admin管理员组文章数量:1023794
I have the following function that generates a Google chart, but it's not working:
function drawChart(a) {
alert(a.test);
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows(a.test);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
"a.test" retrieve a string that is the following:
test="[['2004', 1000, 400],['2005', 1170, 460],['2006', 660, 1120],['2007', 1030, 540]]";
But if i put directly the values in this way
data.addRows([['2004', 1000, 400],['2005', 1170, 460],['2006', 660, 1120],['2007', 1030, 540]]);
it works.
Could you please help me on understanding where i am wrong?
Thanks!
I have the following function that generates a Google chart, but it's not working:
function drawChart(a) {
alert(a.test);
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows(a.test);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
"a.test" retrieve a string that is the following:
test="[['2004', 1000, 400],['2005', 1170, 460],['2006', 660, 1120],['2007', 1030, 540]]";
But if i put directly the values in this way
data.addRows([['2004', 1000, 400],['2005', 1170, 460],['2006', 660, 1120],['2007', 1030, 540]]);
it works.
Could you please help me on understanding where i am wrong?
Thanks!
Share Improve this question edited Oct 8, 2013 at 11:42 user1723319 asked Oct 8, 2013 at 10:27 user1723319user1723319 812 silver badges8 bronze badges1 Answer
Reset to default 4The addRows
method accepts an array of arrays, not a string, so you need to either change a.test
into an array or change it into a JSON string and call JSON.parse
on it:
// note that the internal quotes are double-quotes here, as that is required by JSON
var foo = '[["2004", 1000, 400],["2005", 1170, 460],["2006", 660, 1120],["2007", 1030, 540]]';
data.addRows(JSON.parse(foo));
I have the following function that generates a Google chart, but it's not working:
function drawChart(a) {
alert(a.test);
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows(a.test);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
"a.test" retrieve a string that is the following:
test="[['2004', 1000, 400],['2005', 1170, 460],['2006', 660, 1120],['2007', 1030, 540]]";
But if i put directly the values in this way
data.addRows([['2004', 1000, 400],['2005', 1170, 460],['2006', 660, 1120],['2007', 1030, 540]]);
it works.
Could you please help me on understanding where i am wrong?
Thanks!
I have the following function that generates a Google chart, but it's not working:
function drawChart(a) {
alert(a.test);
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'Sales');
data.addColumn('number', 'Expenses');
data.addRows(a.test);
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
"a.test" retrieve a string that is the following:
test="[['2004', 1000, 400],['2005', 1170, 460],['2006', 660, 1120],['2007', 1030, 540]]";
But if i put directly the values in this way
data.addRows([['2004', 1000, 400],['2005', 1170, 460],['2006', 660, 1120],['2007', 1030, 540]]);
it works.
Could you please help me on understanding where i am wrong?
Thanks!
Share Improve this question edited Oct 8, 2013 at 11:42 user1723319 asked Oct 8, 2013 at 10:27 user1723319user1723319 812 silver badges8 bronze badges1 Answer
Reset to default 4The addRows
method accepts an array of arrays, not a string, so you need to either change a.test
into an array or change it into a JSON string and call JSON.parse
on it:
// note that the internal quotes are double-quotes here, as that is required by JSON
var foo = '[["2004", 1000, 400],["2005", 1170, 460],["2006", 660, 1120],["2007", 1030, 540]]';
data.addRows(JSON.parse(foo));
本文标签: javascriptPass string to Google Chart addRowsStack Overflow
版权声明:本文标题:javascript - Pass string to Google Chart addRows - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745583756a2157461.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论