Commits

Ralph Bean committed 55b1c52

piechart done

Comments (0)

Files changed (2)

tw2/protovis/conventional/samples.py

     AreaChart,
     BarChart,
     ScatterPlot,
+    PieChart,
     StreamGraph,
 )
 from widgets import js
     p_data = [{'x': i, 'y' : random.random(), 'z' : 10**(2*random.random())}
                 for i in range(100)]
 
+class DemoPieChart(PieChart):
+    p_data = [random.random() for i in range(10)]
+
 # 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

             .size(js('function(d) d.z')) \
             .title(js('function(d) d.z.toFixed(1)'))
 
+class PieChart(twp.PVWidget):
+    def prepare(self):
+        # Sizing and scales.
+        self.init_js = js(
+            """
+            var data = %s,
+                w = %i,
+                h = %i,
+                r = h > w ? w / 2 : h / 2,
+                a = pv.Scale.linear(0, pv.sum(data)).range(0, 2 * Math.PI);
+            """ % (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)
+
+        # The wedge, with centered label.
+        self.add(pv.Wedge) \
+            .data(js('data.sort(pv.reverseOrder)')) \
+            .bottom(js('r')) \
+            .left(js('r')) \
+            .innerRadius(js('r - 40')) \
+            .outerRadius(js('r')) \
+            .angle(js('a')) \
+            .event("mouseover", js('function() this.innerRadius(0)')) \
+            .event("mouseout", js('function() this.innerRadius(r - 40)')) \
+          .anchor("center").add(pv.Label) \
+            .visible(js('function(d) d > .15')) \
+            .textAngle(0) \
+            .text(js('function(d) d.toFixed(2)'))
+
 class StreamGraph(twp.PVWidget):
     def prepare(self):
         self.init_js = js(
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.