Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 556
Joseph Avatar asked Jun 17 '13 16:06

Joseph


2 Answers

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.

like image 121
dlaliberte Avatar answered Oct 09 '22 15:10

dlaliberte


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

like image 30
adityajain019 Avatar answered Oct 09 '22 17:10

adityajain019