Commits

Ralph Bean committed f151286

Moved the samples around so they show up in the "view source" tw2.devtools stuff.

  • Participants
  • Parent commits def0745

Comments (0)

Files changed (1)

tw2/protovis/custom/samples.py

     StreamGraph,
     BubbleChart,
 )
-from widgets import js
-from tw2.core import JSSymbol
 
 import math
 import random
     p_height = 10
     p_data = [0.2, 0.3, 0.6, 0.1, 0.9, 0.8, 0.23, 0.77, 0.63, 0.43, 0.59, 0.11]
 
-# The following are some data generation functions used by the streamgraph demo
-def waves(n, m):
-    def f(i, j):
-        x = 20 * j / m - i / 3
-        if x > 0:
-            return 2 * x * math.exp(x * -0.5)
-        return 0
-    return map(lambda i : map(lambda j : f(i, j), range(m)), range(n))
-
-def layers(n, m):
-    def bump(a):
-        x = 1.0 / (.1 + random.random())
-        y = 2.0 * random.random() - 0.5
-        z = 10.0 / (0.1 + random.random())
-        for i in range(m):
-            w = (float(i) / m - y) * z
-            a[i] += x * math.exp(-w * w)
-        return a
-    def f(*args):
-        a = [0] * m
-        for i in range(5):
-            a = bump(a)
-        return a
-    return map(f, range(n))
 
 class DemoStreamGraph(StreamGraph):
+    def layers(n, m):
+        """ Just a toy data generator for the demo """
+        def bump(a):
+            x = 1.0 / (.1 + random.random())
+            y = 2.0 * random.random() - 0.5
+            z = 10.0 / (0.1 + random.random())
+            for i in range(m):
+                w = (float(i) / m - y) * z
+                a[i] += x * math.exp(-w * w)
+            return a
+        def f(*args):
+            a = [0] * m
+            for i in range(5):
+                a = bump(a)
+            return a
+        return map(f, range(n))
+
+    def waves(n, m):
+        """ Another toy data generator """
+        def f(i, j):
+            x = 20 * j / m - i / 3
+            if x > 0:
+                return 2 * x * math.exp(x * -0.5)
+            return 0
+        return map(lambda i : map(lambda j : f(i, j), range(m)), range(n))
+
+    # Actually use the 'layers' data
     p_data = layers(20, 400)
 
-def make_bubblechart_data():
-    p_data = []
-    for tup in os.walk('.'):
-        dir, dirs, files = tup
-        for file in files:
-            if (file.endswith('pyc') 
-                or file.endswith('pyo')
-                or file.endswith('.swp')):
-                continue
-            nodename = "%s/%s" % (dir, file)
-            value = int(os.path.getsize(nodename)) + 1
-            clip = int(math.sqrt(value)/50.0) # trim text based on value
-            p_data.append({
-                'name' : nodename,
-                'value' : value,
-                'text' : nodename.split('/')[-1][:clip],
-                'group' : "/".join(nodename.split('/')[:-1]),
-            })
-    return p_data
 
 class DemoBubbleChart(BubbleChart):
+    def make_bubblechart_data():
+        """ A little toy data generator.
+
+        Walk the file system and report sizes
+        """
+
+        p_data = []
+        for tup in os.walk('.'):
+            dir, dirs, files = tup
+            for file in files:
+                if (file.endswith('pyc')
+                    or file.endswith('pyo')
+                    or file.endswith('.swp')):
+                    continue
+                nodename = "%s/%s" % (dir, file)
+                value = int(os.path.getsize(nodename)) + 1
+                clip = int(math.sqrt(value)/50.0) # trim text based on value
+                p_data.append({
+                    'name' : nodename,
+                    'value' : value,
+                    'text' : nodename.split('/')[-1][:clip],
+                    'group' : "/".join(nodename.split('/')[:-1]),
+                })
+        return p_data
+
     p_height = 750
     p_width = 750
     p_data = make_bubblechart_data()