Google charts' addRows() function does not want to accept an array
Asked Answered
P

2

8

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.

Pronator answered 17/6, 2013 at 16:50 Comment(0)
K
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.

Khamsin answered 17/6, 2013 at 20:13 Comment(1)
Thanks! addRow() is more convenient for my case as well, thanks for that too.Pronator
C
2

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

Coarse answered 16/3, 2018 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.