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]);
48 function request_jit_tree(nodeId, level, depth, onComplete){
49 var params = { logarea: 1 };
50 jQuery.getJSON('jit_tree/'+nodeId+'/'+depth, params, onComplete);
57 var tm = new $jit.TM.Squarified({
58 //where to inject the visualization
59 injectInto: 'infovis',
60 //show only one tree level
61 levelsToShow: levelsToShow,
62 //parent box title heights
72 family: 'Tahoma, Verdana, Arial'
74 //enable specific canvas styles
75 //when rendering nodes
82 //Attach left and right click events
85 onClick: function(node) {
86 if(node) tm.enter(node);
88 onRightClick: function() {
91 //change node styles and canvas styles
92 //when hovering a node
93 onMouseEnter: function(node, eventInfo) {
95 //add node selected styles and replot node
96 node.setCanvasStyle('shadowBlur', 7);
97 node.setData('color', '#888');
98 tm.fx.plotNode(node, tm.canvas);
99 tm.labels.plotLabel(tm.canvas, node);
102 onMouseLeave: function(node) {
104 node.removeData('color');
105 node.removeCanvasStyle('shadowBlur');
110 //duration of the animations
116 //add positioning offsets
119 //implement the onShow method to
120 //add content to the tooltip when a node
122 onShow: function(tip, node, isLeaf, domElement) {
123 var data = node.data;
124 var html = "<div class=\"tip-title\">"
125 + (data.title ? data.title : "")
127 + "</div><div class=\"tip-text\">";
130 html += sprintf("Size: %d (%d + %d)<br />", data.self_size+data.kids_size, data.self_size, data.kids_size);
132 if (data.self_size) {
133 html += sprintf("Memory usage:<br />");
134 bySortedValue(data.leaves,
135 function(a, b) { return a[1] - b[1] },
136 function(k, v) { html += sprintf(" %10s: %5d<br />", k, v);
142 html += sprintf("Attributes:<br />");
143 bySortedValue(data.attr,
144 function(a, b) { return a[0] > b[0] ? 1 : a[0] < b[0] ? -1 : 0 },
145 function(k, v) { html += sprintf(" %10s: %5d<br />", k, v);
149 if (data.child_count) {
150 html += sprintf("Children: %d of %d<br />", data.child_count, data.kids_node_count);
152 html += sprintf("Id: %s%s<br />", node.id, data._ids_merged ? data._ids_merged : "");
153 html += sprintf("Depth: %d<br />", data.depth);
154 html += sprintf("Parent: %d<br />", data.parent_id);
156 html += JSON.stringify(data.attr, undefined, 4);
157 //html += JSON.stringify(data, undefined, 4);
159 tip.innerHTML = html;
162 //Implement this method for retrieving a requested
163 //subtree that has as root a node with id = nodeId,
164 //and level as depth. This method could also make a server-side
165 //call for the requested subtree. When completed, the onComplete
166 //callback method should be called.
167 request: function(nodeId, level, onComplete){
168 request_jit_tree(nodeId, level, levelsToShow, function(data) {
169 console.log("Fetched node "+nodeId);
171 onComplete.onComplete(nodeId, data);
174 //Add the name of the node in the corresponding label
175 //This method is called once, on label creation and only for DOM labels.
176 onCreateLabel: function(domElement, node){
177 domElement.innerHTML = node.name;
181 request_jit_tree(1, 0, levelsToShow, function(data) {
187 //add event to buttons
188 $jit.util.addEvent($jit.id('back'), 'click', function() { tm.out() });
189 $jit.util.addEvent($jit.id('logarea'), 'onchange', function() { tm.refresh() });