最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google charts' addRows() function does not want to accept an array - Stack Overflow

programmeradmin3浏览0评论

I am trying to make a line chart, but Google Charts keeps throwing this error when I try to add a row of data:

Error: Every row given must be either null or an array. @ ...corechart.I.js:162

Here are some example columns I tried. Making the columns works fine, and displays an empty graph as long as I don't add any rows.

var data = new google.visualization.DataTable();
        data.addColumn('number', 'timestamp');
        data.addColumn('number', 'JPY');
        data.addColumn('number', 'EUR');
        data.addColumn('number', 'SEK');
        data.addColumn('number', 'HKD');
        data.addColumn('number', 'CHF');
//So far so good

Now, no matter how I try to pass an array with addRows(), I get the error. I have found similar questions here, but they've all failed for reasons of malformed code or used a different methodology to pass the code in. So here is a simplified test case, which still fails.

data.addRows([1,2,3,4,5,6]); //Breaks the chart

I also tried:

var myrow = new Array(1,2,3,4,5,6);
data.addRows(myrow);

I don't see how I can make this any more literally an array. I also passed two at once, because all the example code seems to pass multiple rows.

data.addRows([1,2,3,4,5,6],
             [7,8,9,10,11,12]);

Still fails.

I am trying to make a line chart, but Google Charts keeps throwing this error when I try to add a row of data:

Error: Every row given must be either null or an array. @ ...corechart.I.js:162

Here are some example columns I tried. Making the columns works fine, and displays an empty graph as long as I don't add any rows.

var data = new google.visualization.DataTable();
        data.addColumn('number', 'timestamp');
        data.addColumn('number', 'JPY');
        data.addColumn('number', 'EUR');
        data.addColumn('number', 'SEK');
        data.addColumn('number', 'HKD');
        data.addColumn('number', 'CHF');
//So far so good

Now, no matter how I try to pass an array with addRows(), I get the error. I have found similar questions here, but they've all failed for reasons of malformed code or used a different methodology to pass the code in. So here is a simplified test case, which still fails.

data.addRows([1,2,3,4,5,6]); //Breaks the chart

I also tried:

var myrow = new Array(1,2,3,4,5,6);
data.addRows(myrow);

I don't see how I can make this any more literally an array. I also passed two at once, because all the example code seems to pass multiple rows.

data.addRows([1,2,3,4,5,6],
             [7,8,9,10,11,12]);

Still fails.

Share Improve this question asked Jun 17, 2013 at 16:50 JosephJoseph 8031 gold badge6 silver badges22 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 22

Easy one. The addRows() method expects you to provide an array of arrays, not a single array for one row, and not separate parameters for each row. See the example in the docs. Fixing your example, it should look like this:

data.addRows([[1,2,3,4,5,6], [7,8,9,10,11,12]]);

You might also prefer to use the addRow() method, which takes just one row at a time.

I had same trouble while I was adding rows using for loop, but then you could find in documentation itself, addRows() expect array of Arrays, the thing you're trying to do can be achieved with simple addRow() without an 'S'. addRow([1,2,3,4,5,6])

发布评论

评论列表(0)

  1. 暂无评论