谷歌图表不接受良好的数组 [英] Google charts not accepting well formed array

查看:107
本文介绍了谷歌图表不接受良好的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google图表API编写图表工具。
为了改变组成图表的数据的时间跨度,我向jsp servlet发送一个jquery帖子,该jsp servlet返回一个类似于

Im writing a charting tool using the Google charts API. To change the timespan for the data that makes up the charts i'm sending a jquery post to a jsp servlet that returns a string line that looks similar to

Swedish#68#Pasta#4#Vegetarian#2#Seafood#2#Salad#1

它存储在字符串数据中,然后被分割成一个二维数组,这是我用来创建图表行的数据。

it is stored in the string data that is then split into a two dimension array which is what I use to create the charts rows.

                data.trim();
                var dataArray = data.split("#");  
                var catChartArray = new Array((dataArray.length/2));
                for (var i = 0; i < dataArray.length/2;i++)
                    {catChartArray[i]=new Array(1);}    
                var j =0;
                for (var i = 0; i<dataArray.length/2; i++){
                    catChartArray[i][0]= dataArray[j];
                    catChartArray[i][1]= dataArray[parseInt(j+1)];
                    j+=2;
                }

然后我将它传递给google.visualization.DataTable()的一个实例addRows(catChartArray)方法。在这一点上,图表不会画,我不明白为什么。它在构建时使用了这种类型,然后工作正常。
使用Javascript很没经验,所以我可能忽略了一些非常明显的东西。
数组的打印输出结果显示:

Then I pass it to an instance of google.visualization.DataTable() with the addRows(catChartArray) method. At this point the chart will not draw and I cant understand why. It has used exactly that kind of when constructed and then it works fine. Im quite unexperienced using Javascript so I might have overlooked something very obvious. A printout of the array shows nothing spectacular:

Swedish,68,Pasta,4,Vegetarian,2,Seafood,2,Salad,1


推荐答案

明白了!
在系统中创建了两个错误。

Got it! Two things created the bugs in the system.

这当然不会产生任何错误:

This of course produces no errors:

catChartArray[i][1]= dataArray[parseInt(j+1)];

但它应该是

but it should be

catChartArray[i][1]= parseInt(dataArray[j+1]);

另外,servlet还返回了很多必须中断数组的空行。
已添加

Also the servlet returned alot of empty lines that must have disrupted the array. Added

 <%@page trimDirectiveWhitespaces="true"%>

到我用于发布请求的jsp文件。

to my the jsp file i used for the post request.

现在一切都按预期工作。

Now everything works as expected.

这篇关于谷歌图表不接受良好的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆