Google图表的addRows()函数不希望接受数组

时间:2022-04-26 15:28:01

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

我正在尝试制作折线图,但是当我尝试添加一行数据时,Google Charts不断抛出此错误:

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

错误:给定的每一行必须为null或数组。 @ ... 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.

现在,无论我如何尝试使用addRows()传递数组,我都会收到错误。我在这里发现了类似的问题,但是由于代码格式错误或者使用不同的方法来传递代码,它们都失败了。所以这里是一个简化的测试用例,但仍然失败。

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.

2 个解决方案

#1


18  

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:

容易一个。 addRows()方法要求您提供一个数组数组,而不是一行的单个数组,而不是每行的单独参数。请参阅文档中的示例。修复你的例子,它应该是这样的:

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.

您可能还希望使用addRow()方法,该方法一次只能使用一行。

#2


1  

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

我在使用for循环添加行时遇到了同样的麻烦,但是你可以在文档中找到,addRows()期望数组的数组,你想要做的事情可以通过简单的addRow()实现,而不是'S ”。 addRow([1,2,3,4,5,6])

#1


18  

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:

容易一个。 addRows()方法要求您提供一个数组数组,而不是一行的单个数组,而不是每行的单独参数。请参阅文档中的示例。修复你的例子,它应该是这样的:

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.

您可能还希望使用addRow()方法,该方法一次只能使用一行。

#2


1  

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

我在使用for循环添加行时遇到了同样的麻烦,但是你可以在文档中找到,addRows()期望数组的数组,你想要做的事情可以通过简单的addRow()实现,而不是'S ”。 addRow([1,2,3,4,5,6])