$ wnd.google.visualization未定义 [英] $wnd.google.visualization is undefined
问题描述
我目前正在构建一个基于SmartGWT的Web应用程序(使用Portlet布局)。所以我有几个Portlet,它基本上扩展了GWT窗口的不同内容。现在我想让Portlet显示 Dygraphs 。所以我创建了一个RPC Service实现,它返回一个JSON字符串(基于DataTable对象)。
因为我不能直接序列化一个DataTable对象,所以我使用
字符串json = JsonRenderer.renderDataTable(data,true,true).toString();
其中data是DataTable类型的。
现在这个String被正确地传递到我想创建Dygraph的客户端。在这个线程中,有人建议使用
public static native DataTable toDataTable(String json)
/ - {return new $ wnd.google.visualization.DataTable(eval((+ json +) ))); } - /;
如果我在我的GWT客户端代码中使用了这个,我收到一个错误消息:
com.google.gwt.core.client.JavaScriptException:(TypeError):$ wnd.google.visualization未定义
我错过了一些可视化API的导入吗?我在哪里必须实例化它?
或者有另一种方法可以将JSON数据字符串导入Dygraph中?我找不到任何示例...
感谢您的任何提示!
我假设你已经在你的模块的XML中包含了visualization.jar和visualization命名空间。
< inherits NAME = com.google.gwt.visualization.Visualization/>
这会给你类。你可能已经这样做了,否则你会得到一个编译器错误。 然而,你也必须包含来自谷歌服务器的实际可视化JavaScript文件(可视化文件.jar只是一个包装)。这可以通过两种不同的方式完成:
1。)将其包含在主页中:
< script type =text / javascript>
google.load(visualization,1,{'packages':[corechart]});
< / script>
或
2。)加载它动态地在你需要它的地方:
VisualizationUtils.loadVisualizationApi(onLoadCallback,MotionChart.PACKAGE);
顺便说一句。我分叉了Dygraphs项目并将GWT包装器更改为更像其他可视化包装器。你可以在这里查看: https://github.com/timeu/dygraphs
编辑:我使用GWT 2.8的新JsInterop为dygraph创建了一个新的GWT包装器: https://github.com/timeu/dygraphs-gwt
注意:我改变了dygraph中的一些行为,并添加了一些上游代码中不可用的功能。
I'm currently building a SmartGWT-based web application (using the Portlet Layout). So I have several "Portlet", which basically extend GWT Window with different content. Now I want a Portlet to display Dygraphs. So I've created an RPC Service implementation which returns a JSON String (based on a DataTable object).
Since I cannot directly serialize a DataTable object I use
String json = JsonRenderer.renderDataTable(data, true, true).toString();
where "data" is of type DataTable.
Now this String gets correctly passed to the client side where I want to create the Dygraph. In this thread , someone suggested to use
public static native DataTable toDataTable(String json)
/-{ return new $wnd.google.visualization.DataTable(eval("(" + json + ")")); }-/;
If I use this in my GWT client code, i get an error saying
com.google.gwt.core.client.JavaScriptException: (TypeError): $wnd.google.visualization is undefined
Do i miss some "import" of the visualization API? Where do i have to instantiate it?
Or is there another way to get the JSON datastring into the Dygraph? I can't find any examples...
Thank you for any hint!
I assume you have included the visualization.jar and the visualization namespace in your module's XML
<inherits name="com.google.gwt.visualization.Visualization"/>
This will give you the Classes. You probably have done this otherwise you would have gotten a compiler error.
However you also have to include the actual visualization javascript file from the google servers (the visualization.jar is only a wrapper). This can be done in two different ways:
1.) Include it in the host page:
<script type="text/javascript">
google.load("visualization", "1", {'packages' : ["corechart"] });
</script>
or
2.) Load it dynamically where you need it:
VisualizationUtils.loadVisualizationApi(onLoadCallback, MotionChart.PACKAGE);
see http://code.google.com/docreader/#p=gwt-google-apis&s=gwt-google-apis&t=VisualizationGettingStarted
Btw. I have forked the Dygraphs Project and changed the GWT wrapper to more like the other visualization wrappers. You can check it out here: https://github.com/timeu/dygraphs
Edit: I have a new GWT wrapper for dygraphs that uses the GWT 2.8's new JsInterop: https://github.com/timeu/dygraphs-gwt
Note: I changed some behaviour in dygraphs and added some features which weren't available in the upstream code.
这篇关于$ wnd.google.visualization未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!