Commits

Ralph Bean  committed 092033b

new sample to test multiply-chained exceptions

  • Participants
  • Parent commits ec9b371

Comments (0)

Files changed (1)

File tw2/protovis/core/samples.py

 from widgets import PVWidget, pv
 from tw2.core import JSSymbol
 
+import random
+
 class js(JSSymbol):
     def __init__(self, src):
         super(js, self).__init__(src=src)
 
 class DemoPVWidget(PVWidget):
     def __init__(self, *args, **kwargs):
+        self.p_data = [
+        [random.random() + 0.1 for j in range(4)] for i in range(3)]
         super(DemoPVWidget, self).__init__(*args, **kwargs)
-        data = [1, 1.2, 1.7, 1.5, .7, .3]
         # Sizing and scales.
         self.init_js = js(
             """
-            var data = %s,
-                w = %i,
+            var data = %s;
+            var n = data.length;
+            var m = data[0].length;
+            var w = %i,
                 h = %i,
-                x = pv.Scale.linear(0, 1,1).range(0, w),
-                y = pv.Scale.ordinal(pv.range(10)).splitBanded(0, h, 4/5);
-            """ % (data, self.p_width, self.p_height))
+                x = pv.Scale.linear(0, 1.1).range(0, w),
+                y = pv.Scale.ordinal(pv.range(n)).splitBanded(0, h, 4/5);
+            """ % (self.p_data, self.p_width, self.p_height))
 
         self.setupRootPanel()
 
         # The bars.
-        bar = self.add(pv.Bar).data(js('data'))\
-                .top(js('function() y(this.index)'))\
-                .height(js('y.range().band'))\
-                .left(0)\
-                .width(js('x'))
+        bar = self.add(pv.Panel) \
+            .data(js('data')) \
+            .top(js('function() y(this.index)')) \
+            .height(js('y.range().band')) \
+          .add(pv.Bar) \
+            .data(js('function(d) d')) \
+            .top(js('function() this.index * y.range().band / m')) \
+            .height(js('y.range().band / m')) \
+            .left(0) \
+            .width(js('x')) \
+            .fillStyle(js('pv.Colors.category20().by(pv.index)'))
 
         # The value label.
-        bar.anchor("right").add(pv.Label)\
-                .textStyle("white")\
-                .text(js('function(d) d.toFixed(1)'))
+        bar.anchor("right").add(pv.Label) \
+            .textStyle("white") \
+            .text(js('function(d) d.toFixed(1)'))
 
         # The variable label.
-        bar.anchor("left").add(pv.Label)\
-            .textMargin(5)\
-            .textAlign("right")\
-            .text(js('function() "ABCDEFGHIJK".charAt(this.index)'))
+        bar._parent.anchor("left").add(pv.Label) \
+            .textAlign("right") \
+            .textMargin(5) \
+            .text(js('function() "ABCDEFGHIJK".charAt(this.parent.index)'))
 
         # X-axis ticks.
-        self.add(pv.Rule)\
-            .data(js('x.ticks(5)'))\
-            .left(js('x'))\
+        self.add(pv.Rule) \
+            .data(js('x.ticks(5)')) \
+            .left(js('x')) \
             .strokeStyle(js('function(d) d ? "rgba(255,255,255,.3)" : "#000"'))\
-          .add(pv.Rule)\
-            .bottom(0)\
-            .height(5)\
-            .strokeStyle("#000")\
-          .anchor("bottom").add(pv.Label)\
+            .add(pv.Rule) \
+            .bottom(0) \
+            .height(5) \
+            .strokeStyle("#000") \
+          .anchor("bottom").add(pv.Label) \
             .text(js('x.tickFormat'))