Commits

Ralph Bean committed 3423202

Time series for stacked area chart

  • Participants
  • Parent commits 3247dd2

Comments (0)

Files changed (3)

 
 setup(
     name='tw2.protovis.conventional',
-    version='0.1.a8',
+    version='0.1.a9',
     description='toscawidgets2 wrapper for the stanford protovis toolkit',
     long_description=long_description,
     author='Ralph Bean',

tw2/protovis/conventional/samples.py

     p_data = [random.random() for i in range(10)]
 
 
-class AutoRefreshingData(object):
-    """ Helper class for DemoLineChart.  Refreshes its data dynamically. """
-    def __iter__(self):
-        n = 20.0
-        T_scale = 1000.0
-        now = int(time.time())
-        tspan = range(now-100, now)
-        funcs = [
-            lambda t : math.sin(t/n),
-            lambda t : abs(math.sin(t/n))**((t%(2*n))/n),
-            lambda t : math.cos(t/(n+1))*1.5,
-        ]
-        funcs.append(
-            lambda t : funcs[1](t) * funcs[2](t)
-        )
-
-        for i in range(len(funcs)):
-            yield [ { 'x': t*1000.0, 'y' : funcs[i](float(t)) } for t in tspan ]
-
-    def __repr__(self):
-        print "repr"
-        return str(self)
-
-    def __str__(self):
-        return str([ele for ele in self])
-
-    def __len__(self):
-        return 4
 
 class DemoLineChart(LineChart):
+    class AutoRefreshingData(object):
+        """ Helper class for DemoLineChart.  Refreshes its data dynamically. """
+        def __iter__(self):
+            n = 20.0
+            T_scale = 1000.0
+            now = int(time.time())
+            tspan = range(now-100, now)
+            funcs = [
+                lambda t : math.sin(t/n),
+                lambda t : abs(math.sin(t/n))**((t%(2*n))/n),
+                lambda t : math.cos(t/(n+1))*1.5,
+            ]
+            funcs.append(
+                lambda t : funcs[1](t) * funcs[2](t)
+            )
+
+            for i in range(len(funcs)):
+                yield [ { 'x': t*1000.0, 'y' : funcs[i](float(t)) }
+                       for t in tspan ]
+
+        def __repr__(self):
+            print "repr"
+            return str(self)
+
+        def __str__(self):
+            return str([ele for ele in self])
+
+        def __len__(self):
+            return 4
+
     p_data = AutoRefreshingData()
     p_labels = ["billy", "bobby", "sally", "suzie"]
     p_time_series = True
     p_time_series_format = "%I:%M:%S"
 
 class DemoStackedAreaChart(StackedAreaChart):
-    p_data = [
-        [
-            {
-                'series' : i,
-                'x': j / 10.0,
-                'y' : math.sin(j/10.0) + random.random() * .5  + 2
-            } for j in range(100)
-        ] for i in range(5)
-    ]
-    p_labels = ["billy", "bobby", "sally", "suzie", "balthazar"]
+    class AutoRefreshingData(object):
+        """ Helper class for DemoLineChart.  Refreshes its data dynamically. """
+        def __iter__(self):
+            n = 20.0
+            T_scale = 1000.0
+            now = int(time.time())
+            tspan = range(now-100, now)
+            funcs = [
+                lambda t : math.sin(t/n),
+                lambda t : abs(math.sin(t/n))**((t%(2*n))/n),
+                lambda t : math.cos(t/(n+1))*1.5,
+            ]
+            funcs.append(
+                lambda t : funcs[1](t) * funcs[2](t)
+            )
+
+            for i in range(len(funcs)):
+                yield [ { 'x': t*1000.0, 'y' : abs(funcs[i](float(t))) }
+                       for t in tspan ]
+
+        def __repr__(self):
+            print "repr"
+            return str(self)
+
+        def __str__(self):
+            return str([ele for ele in self])
+
+        def __len__(self):
+            return 4
+
+    p_data = AutoRefreshingData()
+    p_labels = ["billy", "bobby", "sally", "suzie"]
+    p_time_series = True
+    p_time_series_format = "%I:%M:%S"
 
 class DemoGroupedBarChart(GroupedBarChart):
     p_data = [

tw2/protovis/conventional/widgets.py

 
 class StackedAreaChart(twp.PVWidget):
     p_labels = twc.Param('list of label strings')
+    p_time_series = twc.Param('Convert from "seconds since the epoch"?',
+                              default=False)
+    p_time_series_format = twc.Param(
+        """strftime-type format for time strings.
+
+        See http://vis.stanford.edu/protovis/jsdoc/symbols/pv.Format.date.html
+        """, default="%T")
 
     def prepare(self):
         if self.p_labels and len(self.p_labels) != len(self.p_data):
         # Sizing and scales.
         self.init_js = js(
             """
-            var data = %s,
-                w = %i,
+            var data = %s;
+
+            var time_series = "%s".toLowerCase() == "true";
+            var time_series_format = "%s";
+            var formatter = pv.Format.date(time_series_format);
+            if ( time_series ) {
+                data.forEach(function(series){
+                    series.forEach(function(datum){
+                        var t = new Date();
+                        t.setTime(datum.x);
+                        datum.x = t;
+                    });
+                });
+            }
+
+            var w = %i,
                 h = %i,
-                x = pv.Scale.linear(0, 9.9).range(0, w),
-                y = pv.Scale.linear(0, 14).range(0, h),
+                x = pv.Scale.linear(data[0], function(d) { return d.x }).range(0, w),
+                y = pv.Scale.linear(0, %i).range(0, h),
                 labels = %s;
-            """ % (self.p_data, self.p_width, self.p_height, self.p_labels))
-        
+            """ % (simplejson.dumps(list(self.p_data)),
+                   self.p_time_series, self.p_time_series_format,
+                   self.p_width, self.p_height,
+                   sum([max([d['y'] for d in series])
+                        for series in self.p_data])+1,
+                   self.p_labels,
+                  ))
+
         self.setupRootPanel()
-        
+
         # X-axis and ticks.
         self.add(pv.Rule) \
             .data(js('x.ticks()')) \
             .bottom(-5) \
             .height(5) \
           .anchor("bottom").add(pv.Label) \
-            .text(js('x.tickFormat'))
+            .text(js('formatter'))
 
         # The stack layout.
         self.add(pv.Layout.Stack) \