javascript 谷歌图表的 addRows() 函数不想接受数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17152770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 07:17:44  来源:igfitidea点击:

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

javascriptarraysgoogle-visualization

提问by Joseph

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

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

还是失败了。

回答by dlaliberte

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 examplein 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() 方法,该方法一次只处理一行。

回答by adityajain019

I had same trouble while I was adding rows using for loop, but then you could find in documentationitself, 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])