Add GWT Dygraphs loader class and supporting files.
authorAdam Vartanian <flooey@gmail.com>
Fri, 9 Apr 2010 18:45:04 +0000 (14:45 -0400)
committerAdam Vartanian <flooey@gmail.com>
Fri, 9 Apr 2010 18:45:04 +0000 (14:45 -0400)
Makefile
docs/index.html
generate-jar.sh [new file with mode: 0755]
gwt/org/danvk/Dygraphs$Resources.class [new file with mode: 0644]
gwt/org/danvk/Dygraphs.class [new file with mode: 0644]
gwt/org/danvk/Dygraphs.java [new file with mode: 0644]
gwt/org/danvk/dygraph-combined.js [new symlink]
gwt/org/danvk/dygraphs.gwt.xml [new file with mode: 0644]

index b71596c..2614ea3 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,4 +8,10 @@ all: generate-combined
 
 generate-combined:
        @echo Generating dygraph-combined.js
-       @generate-combined.sh
+       @./generate-combined.sh
+
+gwt: generate-gwt
+
+generate-gwt:
+       @echo Generating GWT JAR file
+       @./generate-jar.sh
index 71df56b..1a86ecc 100644 (file)
@@ -19,6 +19,7 @@
         <li><a href="#baseball">Baseball chart</a></li>
         <li><a href="#stock">Stock chart</a></li>
         <li><a href="#options">Options Reference</a></li>
+        <li><a href="#gwt">GWT Compatibility</a></li>
         <li><a href="#policy">Data Policy</a></li>
       </ul>
 
@@ -828,6 +829,26 @@ perl -ne 'BEGIN{print "Month,Nominal,Real\n"} chomp; ($m,$cpi,$low,$close,$high)
       <li>Make sure you don't have any trailing commas in your call to the Dygraph constructor or in the options parameter. Firefox, Chrome and Safari ignore these but they can cause a graph to not display in Internet Explorer.</li>
     </ul>
 
+    <h2 id="gwt">GWT Compatibility</h2>
+    <p>There is currently no GWT wrapper around Dygraphs, however there is a class that can be used to easily load Dygraphs into the browser.  To use it, include the generated dygraph-gwt.jar file in your classpath and add the following line to your GWT module:</p>
+
+<pre>
+&lt;inherits name=&quot;org.danvk.dygraphs&quot;/&gt;    
+</pre>
+
+    <p>Call org.danvk.Dygraphs.install() when your application starts to install the JavaScript code into the browser.  You can use <a href="http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsJSNI.html">JSNI</a> to call Dygraphs from your GWT code, as in the example below.  The example uses the <a href="http://code.google.com/p/gwt-google-apis/wiki/VisualizationGettingStarted">Visualization API for GWT</a> and the <a href="#gviz">Dygraphs GViz API.</a></p>
+
+<pre>
+  public static native JavaScriptObject drawDygraph(Element element, DataTable data, double minY, double maxY) /*-{
+    var chart = new $wnd.Dygraph.GVizChart(element);
+    chart.draw(dataTable,
+      {
+        valueRange: [minY, maxY]
+      });
+    return chart;
+  }-*/;
+</pre>
+
     <h2 id="policy">Data Policy</h2>
     <p>dygraphs is purely client-side JavaScript. It does not send your data to any servers &ndash; the data is processed entirely in the client's browser.</p>
 
diff --git a/generate-jar.sh b/generate-jar.sh
new file mode 100755 (executable)
index 0000000..2c1d12d
--- /dev/null
@@ -0,0 +1,4 @@
+#!/bin/bash
+# Generates a JAR file that can be used from GWT to load Dygraphs
+
+jar -cf dygraph-gwt.jar -C gwt org
diff --git a/gwt/org/danvk/Dygraphs$Resources.class b/gwt/org/danvk/Dygraphs$Resources.class
new file mode 100644 (file)
index 0000000..7f4bdda
Binary files /dev/null and b/gwt/org/danvk/Dygraphs$Resources.class differ
diff --git a/gwt/org/danvk/Dygraphs.class b/gwt/org/danvk/Dygraphs.class
new file mode 100644 (file)
index 0000000..084f8fa
Binary files /dev/null and b/gwt/org/danvk/Dygraphs.class differ
diff --git a/gwt/org/danvk/Dygraphs.java b/gwt/org/danvk/Dygraphs.java
new file mode 100644 (file)
index 0000000..f581577
--- /dev/null
@@ -0,0 +1,41 @@
+package org.danvk;
+
+import com.google.gwt.core.client.GWT;
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.ScriptElement;
+import com.google.gwt.resources.client.ClientBundle;
+import com.google.gwt.resources.client.TextResource;
+
+/**
+ * Methods for installing Dygraphs source in a GWT document.
+ *
+ * @author flooey@google.com (Adam Vartanian)
+ */
+public class Dygraphs {
+
+  // Protected because the GWT compiler has to generate a subclass.
+  protected interface Resources extends ClientBundle {
+    @Source("org/danvk/dygraph-combined.js")
+    TextResource dygraphs();
+  }
+
+  private static final Resources RESOURCES = GWT.create(Resources.class);
+  private static boolean installed = false;
+
+  /**
+   * Install the Dygraphs JavaScript source into the current document.  This
+   * method is idempotent.
+   */
+  public static synchronized void install() {
+    if (!installed) {
+      ScriptElement e = Document.get().createScriptElement();
+      e.setText(RESOURCES.dygraphs().getText());
+      Document.get().getBody().appendChild(e);
+      installed = true;
+    }
+  }
+
+  // Prevent construction
+  private Dygraphs() { }
+
+}
diff --git a/gwt/org/danvk/dygraph-combined.js b/gwt/org/danvk/dygraph-combined.js
new file mode 120000 (symlink)
index 0000000..d64425f
--- /dev/null
@@ -0,0 +1 @@
+../../../dygraph-combined.js
\ No newline at end of file
diff --git a/gwt/org/danvk/dygraphs.gwt.xml b/gwt/org/danvk/dygraphs.gwt.xml
new file mode 100644 (file)
index 0000000..679765e
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.0.0/distro-source/core/src/gwt-module.dtd">
+<module>
+  <inherits name="com.google.gwt.user.User" />
+  <inherits name="com.google.gwt.dom.DOM" />
+  <inherits name="com.google.gwt.resources.Resources" />
+  <source path="" />
+</module>