javascript - Pass string to Google Chart addRows - Stack Overflow

I have the following function that generates a Google chart, but it's not working:function drawCha

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));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745583757a4634406.html

相关推荐

  • javascript - Pass string to Google Chart addRows - Stack Overflow

    I have the following function that generates a Google chart, but it's not working:function drawCha

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信