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));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745583757a4634406.html
评论列表(0条)