Commits

Ralph Bean committed 66cd174

solid demo. need genshi + tests still.

  • Participants
  • Parent commits 71dcab0

Comments (0)

Files changed (3)

File tw2/jquery/plugins/jqplot/samples.py

 """
 
 import tw2.core as twc
+from tw2.core.resources import encoder
 
-from widgets import JQPlotWidget
+from widgets import JQPlotWidget, PollingJQPlotWidget
 from base import dateAxisRenderer_js
 
 from random import random
 
 from time import time
 
+def find_bounds(data):
+    minx = min( [min( [point[0] for point in series] ) for series in data])
+    maxx = max( [max( [point[0] for point in series] ) for series in data])
+    miny = min( [min( [point[1] for point in series] ) for series in data])
+    maxy = max( [max( [point[1] for point in series] ) for series in data])
+    miny -= 0.1
+    maxy += 0.1
+    return minx, maxx, miny, maxy
+
 def make_data():
     """ Sin of the times! """
     now = int(time())
             },
         },
     }
+
+class DemoPollingJQPlotWidget(PollingJQPlotWidget):
+    def prepare(self):
+        self.resources.append(dateAxisRenderer_js)
+        super(DemoPollingJQPlotWidget, self).prepare()
+
+    url = '/some_url/'
+    url_kwargs = {}
+    interval = 2000
+
+    data = data
+    options = {
+        'legend' : { 'show' : True },
+        'title' : '(Polling) Sine of the times (tw2)',
+        'series' : [ {'showMarker' : False} for d in data ],
+        'axes' : {
+            'xaxis' : {
+                'renderer' : twc.JSSymbol('$.jqplot.DateAxisRenderer'),
+                'tickOptions' : {
+                    'formatString' : '%T'
+                },
+            },
+        },
+    }
+
+    @classmethod
+    def request(cls, req):
+        import webob
+        data = make_data()
+        minx, maxx, miny, maxy = find_bounds(data)
+        options = {
+            'axes' : {
+                'xaxis' : { 'min' : minx, 'max' : maxx },
+                'yaxis' : { 'min' : miny, 'max' : maxy },
+            }
+        }
+        json = encoder.encode(dict(data=data, options=options))
+        return webob.Response(json, content_type="application/json")
+
+
+# Register the widget's controller
+import tw2.core as twc
+mw = twc.core.request_local()['middleware']
+mw.controllers.register(DemoPollingJQPlotWidget, 'some_url')
+
+

File tw2/jquery/plugins/jqplot/templates/pollster.mak

+<%namespace name="tw" module="tw2.core.mako_util"/>
+<div>
+<div ${tw.attrs(attrs=w.attrs)}></div>
+<script type="text/javascript">
+$(document).ready(
+    function(){
+		$.jqplot.config.enablePlugins = true;
+
+		// Setup our initial plot
+		var the_plot_thickens = $.jqplot(
+			'${w.selector}',
+			${w._data},
+			${w._options}
+		);
+
+		// Setup our callback function
+		callback = function (json) {
+		  pl = the_plot_thickens;
+		  
+          for ( _i = 0; _i < json.data.length; _i++ ) {
+                pl.series[_i].data = json.data[_i] ;
+          }
+          for (ax in json.options.axes) {
+                  pl.axes[ax]._ticks = []
+                  if ( 'axes' in json.options &&
+                        ax in json.options.axes &&
+                        'min' in json.options.axes[ax] ) {
+                        pl.axes[ax].min = json.options['axes'][ax].min;
+                        pl.axes[ax].max = json.options['axes'][ax].max;
+                  }
+                  pl.axes[ax].numberTicks = null
+                  pl.axes[ax].tickInterval = null
+                  pl.axes[ax]._tickInterval = null
+          }
+          pl.redraw();
+		};
+
+		// Finally make the JSON call
+		$.getJSON(
+			'${w.url}',
+			${w.url_kwargs},
+			callback
+		);
+
+		// And if we want to keep polling, do it
+		var interval = ${str(w.interval)};
+		if ( interval > 0 ) {
+			setInterval( function() {
+				$.getJSON(
+					'${w.url}',
+					${w.url_kwargs},
+					callback
+				);}, interval );
+		}
+    }
+);
+</script>
+</div>

File tw2/jquery/plugins/jqplot/widgets.py

         base.jqplot_js, base.jqplot_css,
     ]
     template = "tw2.jquery.plugins.jqplot.templates.jqplot"
-  
+
     data = twc.Param("A list of list of tuples to plot.", default=[])
     options = twc.Param("Configuration options to pass to jqplot", default={})
 
         self._data = encoder.encode(self.data)
         self._options = encoder.encode(self.options)
         super(JQPlotWidget, self).prepare()
+
+class PollingJQPlotWidget(JQPlotWidget):
+    template = "tw2.jquery.plugins.jqplot.templates.pollster"
+    url = twc.Param("(string) A url to poll")
+    url_kwargs = twc.Param("(dict) A dict for a query str", default={})
+    interval = twc.Param("(int) milliseconds between pulls", default=0)
+
+    def prepare(self):
+        self.url_kwargs = encoder.encode(self.url_kwargs)
+        super(PollingJQPlotWidget, self).prepare()