Commits

Ralph Bean committed 6298dee

scatterplot yeaaah

Comments (0)

Files changed (3)

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

tw2/protovis/conventional/samples.py

 The demos implemented here are what is displayed in the tw2.devtools
 WidgetBrowser.
 """
-from widgets import AreaChart, BarChart, StreamGraph
+from widgets import (
+    AreaChart,
+    BarChart,
+    ScatterPlot,
+    StreamGraph,
+)
 from widgets import js
 from tw2.core import JSSymbol
 
 class DemoBarChart(BarChart):
     p_data = [random.random() for i in range(10)]
 
+class DemoScatterPlot(ScatterPlot):
+    p_data = [{'x': i, 'y' : random.random(), 'z' : 10**(2*random.random())}
+                for i in range(100)]
+
 # The following are some data generation functions used by the streamgraph demo
 def waves(n, m):
     def f(i, j):

tw2/protovis/conventional/widgets.py

             .strokeStyle("#000")\
           .anchor("bottom").add(pv.Label)\
             .text(js('x.tickFormat'))
+        
+class ScatterPlot(twp.PVWidget):
+    def prepare(self):
+        # Sizing and scales.
+        self.init_js = js(
+            """
+            var data = %s,
+                w = %i,
+                h = %i,
+                x = pv.Scale.linear(0, 99).range(0, w),
+                y = pv.Scale.linear(0, 1).range(0, h),
+                c = pv.Scale.log(1, 100).range("orange", "brown");
+            """ % (self.p_data, self.p_width, self.p_height))
+
+        # The root panel.
+        self.init().width(self.p_width).height(self.p_height) \
+                .bottom(self.p_bottom).top(self.p_top) \
+                .left(self.p_left).right(self.p_right)
+
+        # Y-axis and ticks.
+        self.add(pv.Rule) \
+            .data(js('y.ticks()')) \
+            .bottom(js('y')) \
+            .strokeStyle(js('function(d) d ? "#eee" : "#000"')) \
+          .anchor("left").add(pv.Label) \
+            .visible(js('function(d) d > 0 && d < 1')) \
+            .text(js('y.tickFormat'))
+
+        # X-axis and ticks.
+        self.add(pv.Rule) \
+            .data(js('x.ticks()')) \
+            .left(js('x')) \
+            .strokeStyle(js('function(d) d ? "#eee" : "#000"')) \
+          .anchor("bottom").add(pv.Label) \
+            .visible(js('function(d) d > 0 && d < 100')) \
+            .text(js('x.tickFormat'))
+
+        # The dot plot!
+        self.add(pv.Panel) \
+            .data(js('data')) \
+          .add(pv.Dot) \
+            .left(js('function(d) x(d.x)')) \
+            .bottom(js('function(d) y(d.y)')) \
+            .strokeStyle(js('function(d) c(d.z)'))  \
+            .fillStyle(js('function() this.strokeStyle().alpha(.2)'))  \
+            .size(js('function(d) d.z')) \
+            .title(js('function(d) d.z.toFixed(1)'))
 
 class StreamGraph(twp.PVWidget):
     def prepare(self):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.