Commits

Ralph Bean committed 446960b

Basic monochrome treemap.

Comments (0)

Files changed (2)

tw2/rrd/samples.py

 arch = ['32', '64'][sys.maxsize > 2**32]
 data_directory = '/'.join(__file__.split('/')[:-1] + ['data', arch]) + '/'
 
+class DemoNestedRRDJitTreeMap(tw2.rrd.NestedRRDJitTreeMap):
+    start = datetime.datetime.fromtimestamp(1306557540)
+    end = datetime.datetime.fromtimestamp(1306558380)
+
+    rrd_directories = [
+        data_directory + 'nested/filename/' + country
+        for country in [
+            'Brazil/',
+            'Canada/',
+            'China/',
+            'France/',
+            'Germany/',
+            'Greece/',
+            'India/',
+            'Italy/',
+            'Malaysia/',
+            'Mexico/',
+            'Peru/',
+            'Puerto_Rico/',
+            'Russian_Federation/',
+            'Singapore/',
+            'Spain/',
+            'Sweden/',
+            'Turkey/',
+            'Ukraine/',
+            'United_Kingdom/',
+            'United_States/',
+        ]
+    ]
+
 class DemoNestedRRDProtoCirclePackingWidget(
     tw2.rrd.NestedRRDProtoCirclePackingWidget):
 

tw2/rrd/widgets/nested.py

 import tw2.core as twc
 import tw2.core.util as util
 
+import tw2.jit
 import tw2.protovis.hierarchies
 
 import os
                 del self.p_data[key]
 
         super(NestedRRDProtoCirclePackingWidget, self).prepare()
+
+class NestedRRDJitTreeMap(tw2.jit.TreeMap, RRDNestedMixin):
+    data = twc.Variable("Internally produced.")
+    method = twc.Param(
+        "Method for consolidating values.  Either 'sum' or 'average'",
+        default='average')
+
+    postInitJSCallback = twc.JSSymbol(
+        src="(function (jitwidget) { jitwidget.refresh(); })")
+
+
+    def make_from_nested(self, data):
+        res = []
+        for i in range(len(data)):
+            key1 = data[i]['label']
+            children = []
+            for j in range(len(data[i]['data'])):
+                key2 = data[i]['data'][j]['label']
+                value = sum([
+                    item[1] for item in data[i]['data'][j]['data']
+                ])
+
+                if self.method == 'average':
+                    value = float(value) / len(data[i]['data'][j]['data'])
+
+                if value == 0:
+                    continue
+
+                children.append({
+                    'id' : "%i-%i-%s-%s" % (i, j, key1, key2),
+                    'name' : key2,
+                    'children' : [],
+                    'data' : {
+                        '$area' : value,
+                    },
+                })
+
+            if not children:
+                continue
+
+            res.append({
+                'id' : "%s-%i" % (key1, i),
+                'name' : key1,
+                'children' : children,
+                'data' : {
+                    '$area' : sum([
+                        c['data']['$area'] for c in children
+                    ]),
+                },
+            })
+
+        return res
+
+    def prepare(self):
+        raw_data = self.nested_fetch()
+        self.data = {
+            'id' : 'root',
+            'name' : 'whutevah',
+            'children' : self.make_from_nested(raw_data),
+        }
+        super(NestedRRDJitTreeMap, self).prepare()