admin管理员组

文章数量:1432352

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 badges
Add a ment  | 

1 Answer 1

Reset to default 4

The 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