javascript 谷歌图表 - 未捕获的错误:不是数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19400497/
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 15:28:03  来源:igfitidea点击:

Google Chart - uncaught error: not an array

javascriptphpgoogle-visualization

提问by Latheesan

I have a php script that generates the google chart data and returns it in json encoded format. It's loaded into google chart using jQuery $.get() method. When I pass the return data to the google "arrayToDataTable" function like this:

我有一个 php 脚本,它生成谷歌图表数据并以 json 编码格式返回它。它使用 jQuery $.get() 方法加载到谷歌图表中。当我将返回数据传递给谷歌“ arrayToDataTable”函数时,如下所示:

var googleChartData = google.visualization.arrayToDataTable(chartData);

I get this error:

我收到此错误:

enter image description here

在此处输入图片说明

So, what I did was I dumped the value of my variable "chartData" and I got the following:

所以,我所做的是我转储了我的变量“ chartData”的值,我得到了以下信息:

enter image description here

在此处输入图片说明

So, what I did was, copy this data from console window into the "arrayToDataTable" function manually like this:

所以,我所做的是,将控制台窗口中的这些数据手动复制到“ arrayToDataTable”函数中,如下所示:

var googleChartData = google.visualization.arrayToDataTable([["Date Range","0001\/102\/0 Available","0001\/102\/0 Unavailable","0001\/102\/1 Available","0001\/102\/1 Unavailable"],["02\/10\/2013",0,1,110,11],["03\/10\/2013",0,1,189,11],["04\/10\/2013",0,1,189,11],["06\/10\/2013",0,1,189,10],["07\/10\/2013",0,1,187,10],["08\/10\/2013",186,11,0,1],["09\/10\/2013",186,11,0,1],["10\/10\/2013",0,1,186,11],["11\/10\/2013",0,1,204,11],["13\/10\/2013",0,1,204,11]]);

Now the chart renders (as expected, because the returned data from my php script is correct):

现在图表呈现(正如预期的那样,因为从我的 php 脚本返回的数据是正确的):

enter image description here

在此处输入图片说明

This is a bit bizarre; does any one know why the chart doesn't work when it's loaded from the variable?

这有点奇怪;有谁知道为什么图表在从变量加载时不起作用?

回答by Latheesan

I've solved it by changing arrayToDataTableline like this:

我已经通过像这样更改arrayToDataTable行来解决它:

var googleChartData = google.visualization.arrayToDataTable($.parseJSON(chartData));

Thanks to this post: https://stackoverflow.com/a/9420583/2332336

感谢这篇文章:https: //stackoverflow.com/a/9420583/2332336