Commits

Ralph Bean committed 93d9c54

Better js namespacing.

Comments (0)

Files changed (2)

tw2/d3/static/ext/bar.js

 if ( typeof tw2.d3 == "undefined" ) tw2.d3 = {};
 
 $.extend(tw2.d3, {
-    filter: function(data) {
-        // Generic utility.  Remove elements with 0 value from the list.
-        // Equivalent to the following python:
-        //     >>> data = [d for d in data if d.value > 0]
-        for (var i = 0; i < data.length; i++) {
-            if (data[i].value <= 0) {
-                data.splice(i, 1);
+    util: {
+        filter: function(data) {
+            // Generic utility.  Remove elements with 0 value from the list.
+            // Equivalent to the following python:
+            //     >>> data = [d for d in data if d.value > 0]
+            for (var i = 0; i < data.length; i++) {
+                if (data[i].value <= 0) {
+                    data.splice(i, 1);
+                }
             }
-        }
-        return data;
+            return data;
+        },
     },
-    bar: function (selector, data, width, height, padding, format) {
-        $(document).ready(function() {
+    bar: {
+        init: function (selector, data, width, height, padding) {
+            $(document).ready(function() {
+                tw2.d3.bar._init(selector, data, width, height, padding);
+            });
+        },
+        _init: function (selector, data, width, height, padding) {
             var w = width - padding[1] - padding[3];
             var h = height - padding[0] - padding[2];
 
-            var format = d3.format(format);
-
             var x = d3.scale.linear().range([0, w]);
             var y = d3.scale.ordinal().rangeRoundBands([0, h], .1);
 
             tw2.store[selector] = {
                 svg: svg,
                 data: data,
+                width: width,
+                height: height,
+                padding: padding,
             };
 
             svg.append("g").attr("class", "x axis")
             svg.append("g").attr("class", "y axis")
 
-            function redraw() {
-                tw2.store[selector].data[0].value -= 15;
-                tw2.store[selector].data = tw2.d3.filter(tw2.store[selector].data);
-                var data = tw2.store[selector].data;
+            tw2.d3.bar.redraw(selector)
+            setInterval(function() { tw2.d3.bar.redraw(selector)}, 1000);
+        },
+        redraw: function(selector) {
+            tw2.store[selector].data[0].value -= 15;
+            tw2.store[selector].data = tw2.d3.util.filter(
+                tw2.store[selector].data
+            );
+            var data = tw2.store[selector].data;
 
-                var x = d3.scale.linear().range([0, w]);
-                var y = d3.scale.ordinal().rangeRoundBands([0, h], .1);
+            var w =
+            tw2.store[selector].width - 
+            tw2.store[selector].padding[1] -
+            tw2.store[selector].padding[3];
 
-                var xAxis = d3.svg.axis().scale(x).orient("top").tickSize(-h);
-                var yAxis = d3.svg.axis().scale(y).orient("left").tickSize(0);
+            var h =
+            tw2.store[selector].height -
+            tw2.store[selector].padding[0] -
+            tw2.store[selector].padding[2];
 
-                // Set the scale domain.
-                x.domain([0, d3.max(data, function(d) { return d.value; })]);
-                y.domain($.map(data, function(d, i) { return d.key; }));
+            var x = d3.scale.linear().range([0, w]);
+            var y = d3.scale.ordinal().rangeRoundBands([0, h], .1);
 
-                var bar = svg.selectAll("g.bar").data(
-                    data,
-                    function(d) { return d.key }
-                );
+            var xAxis = d3.svg.axis().scale(x).orient("top").tickSize(-h);
+            var yAxis = d3.svg.axis().scale(y).orient("left").tickSize(0);
 
-                bar.enter().append("g")
-                .attr("class", "bar")
-                .append("rect");
+            // Set the scale domain.
+            x.domain([0, d3.max(data, function(d) { return d.value; })]);
+            y.domain($.map(data, function(d, i) { return d.key; }));
 
-                bar.exit().remove();
+            var bar = tw2.store[selector].svg.selectAll("g.bar").data(
+                data,
+                function(d) { return d.key }
+            );
 
-                bar.transition().duration(750).attr("transform", function(d) {
-                    return "translate(0," + y(d.key) + ")";
-                });
+            bar.enter().append("g")
+            .attr("class", "bar")
+            .append("rect");
 
-                bar.selectAll("rect")
-                .data(data, function(d) { return d.key })
-                .attr("height", y.rangeBand())
-                .transition()
-                .attr("width", function(d) { return x(d.value); });
+            bar.exit().remove();
 
-                svg.selectAll("g.x").transition().duration(750).call(xAxis);
-                svg.selectAll("g.y").transition().duration(750).call(yAxis);
+            bar.transition().duration(750).attr("transform", function(d) {
+                return "translate(0," + y(d.key) + ")";
+            });
 
-            }
-            redraw()
-            setInterval(redraw, 1000);
-        });
-    }
+            bar.selectAll("rect")
+            .data(data, function(d) { return d.key })
+            .attr("height", y.rangeBand())
+            .transition()
+            .attr("width", function(d) { return x(d.value); });
+
+            tw2.store[selector].svg.selectAll("g.x")
+            .transition().duration(750).call(xAxis);
+            tw2.store[selector].svg.selectAll("g.y")
+            .transition().duration(750).call(yAxis);
+        },
+    },
 });

tw2/d3/widgets.py

     height = twc.Param("Height of the chart in pixels.", default=930)
     padding = twc.Param("A list of ints [top, right, bottom, left]",
                         default=[30, 10, 10, 30])
-    fmtstr = twc.Param("A format string for numeric values.",
-                       default=",.0f")
 
     def prepare(self):
 
         # Check the types of everything
         int(self.width)
         int(self.height)
-        str(self.fmtstr)
         self.padding = [int(ele) for ele in self.padding]
 
         if self.data == None:
         # Munge our data so d3 can understand it
         json = [{'key': k, 'value': v} for k, v in self.data.iteritems()]
 
-        self.add_call(twc.js_function('tw2.d3.bar')(
+        self.add_call(twc.js_function('tw2.d3.bar.init')(
             self.attrs['id'],
             json,
             self.width,
             self.height,
             self.padding,
-            self.fmtstr,
         ))