1 var labelType, useGradients, nativeTextSupport, animate;
4 var ua = navigator.userAgent,
5 iStuff = ua.match(/iPhone/i) || ua.match(/iPad/i),
6 typeOfCanvas = typeof HTMLCanvasElement,
7 nativeCanvasSupport = (typeOfCanvas == 'object' || typeOfCanvas == 'function'),
8 textSupport = nativeCanvasSupport
9 && (typeof document.createElement('canvas').getContext('2d').fillText == 'function');
10 //I'm setting this based on the fact that ExCanvas provides text support for IE
11 //and that as of today iPhone/iPad current text support is lame
12 labelType = (!nativeCanvasSupport || (textSupport && !iStuff))? 'Native' : 'HTML';
13 nativeTextSupport = labelType == 'Native';
14 useGradients = nativeCanvasSupport;
15 animate = !(iStuff || !nativeCanvasSupport);
20 write: function(text){
22 this.elem = document.getElementById('log');
23 this.elem.innerHTML = text;
24 this.elem.style.left = (500 - this.elem.offsetWidth / 2) + 'px';
28 // http://stackoverflow.com/questions/5199901/how-to-sort-an-associative-array-by-its-values-in-javascript
29 function bySortedValue(obj, comparitor, callback, context) {
31 for (var key in obj) {
32 if (obj.hasOwnProperty(key)) {
33 tuples.push([key, obj[key]]);
37 tuples.sort(comparitor);
40 var length = tuples.length;
41 while (length--) callback.call(context, tuples[length][0], tuples[length][1]);
52 var tm = new $jit.TM.Squarified({
53 //where to inject the visualization
54 injectInto: 'infovis',
55 //show only one tree level
57 //parent box title heights
67 family: 'Tahoma, Verdana, Arial'
69 //enable specific canvas styles
70 //when rendering nodes
77 //Attach left and right click events
80 onClick: function(node) {
81 if(node) tm.enter(node);
83 onRightClick: function() {
86 //change node styles and canvas styles
87 //when hovering a node
88 onMouseEnter: function(node, eventInfo) {
90 //add node selected styles and replot node
91 node.setCanvasStyle('shadowBlur', 7);
92 node.setData('color', '#888');
93 tm.fx.plotNode(node, tm.canvas);
94 tm.labels.plotLabel(tm.canvas, node);
97 onMouseLeave: function(node) {
99 node.removeData('color');
100 node.removeCanvasStyle('shadowBlur');
105 //duration of the animations
111 //add positioning offsets
114 //implement the onShow method to
115 //add content to the tooltip when a node
117 onShow: function(tip, node, isLeaf, domElement) {
118 var html = "<div class=\"tip-title\">" + node.name
119 + "</div><div class=\"tip-text\">";
120 var data = node.data;
123 html += sprintf("Size: %d (%d + %d)<br />", data.self_size+data.kids_size, data.self_size, data.kids_size);
125 if (data.self_size) {
126 html += sprintf("Memory usage:<br />");
127 bySortedValue(data.leaves,
128 function(a, b) { return a[1] - b[1] },
129 function(k, v) { html += sprintf(" %10s: %5d<br />", k, v);
134 html += sprintf("Attributes:<br />");
135 bySortedValue(data.attr,
136 function(a, b) { return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0 },
137 function(k, v) { html += sprintf(" %10s: %5d<br />", k, v);
141 if (data.child_count) {
142 html += sprintf("Children: %d of %d<br />", data.child_count, data.kids_node_count);
144 html += sprintf("Id: %s%s<br />", node.id, data._ids_merged ? data._ids_merged : "");
145 html += sprintf("Depth: %d<br />", data.depth);
146 html += sprintf("Parent: %d<br />", data.parent_id);
148 tip.innerHTML = html;
151 //Implement this method for retrieving a requested
152 //subtree that has as root a node with id = nodeId,
153 //and level as depth. This method could also make a server-side
154 //call for the requested subtree. When completed, the onComplete
155 //callback method should be called.
156 request: function(nodeId, level, onComplete){
158 jQuery.getJSON('jit_tree/'+nodeId+'/1', function(data) {
159 console.log("Node "+nodeId);
161 onComplete.onComplete(nodeId, data);
165 var tree = memnodes[0];
166 var subtree = $jit.json.getSubtree(tree, nodeId);
167 $jit.json.prune(subtree, 2);
168 onComplete.onComplete(nodeId, subtree);
171 //Add the name of the node in the corresponding label
172 //This method is called once, on label creation and only for DOM labels.
173 onCreateLabel: function(domElement, node){
174 domElement.innerHTML = node.name;
179 jQuery.getJSON('jit_tree/1/1', function(data) {
186 //var pjson = eval('(' + json + ')');
187 var pjson = memnodes[0];
188 $jit.json.prune(pjson, 2);
194 //add event to the back button
195 var back = $jit.id('back');
196 $jit.util.addEvent(back, 'click', function() {