-
Notifications
You must be signed in to change notification settings - Fork 7
/
DOMTreeMap.js
2370 lines (1874 loc) · 65.9 KB
/
DOMTreeMap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//TreeMap object.
var TM = {};
(function() {
/*
Script: Core.js
Description:
Provides common utility functions and the Class object used internally by the library.
Also provides the <TM.Util> object for manipulating JSON tree structures
Some of the Basic utility functions and the Class system are based in the MooTools Framework <http://mootools.net>. Copyright (c) 2006-2010 Valerio Proietti, <http://mad4milk.net/>. MIT license <http://mootools.net/license.txt>.
Author:
Nicolas Garcia Belmonte
Copyright:
Copyright 2008-2010 by Nicolas Garcia Belmonte.
Homepage:
<http://thejit.org>
Version:
1.0
License:
BSD License
> Redistribution and use in source and binary forms, with or without
> modification, are permitted provided that the following conditions are met:
> * Redistributions of source code must retain the above copyright
> notice, this list of conditions and the following disclaimer.
> * Redistributions in binary form must reproduce the above copyright
> notice, this list of conditions and the following disclaimer in the
> documentation and/or other materials provided with the distribution.
> * Neither the name of the organization nor the
> names of its contributors may be used to endorse or promote products
> derived from this software without specific prior written permission.
>
> THIS SOFTWARE IS PROVIDED BY Nicolas Garcia Belmonte ``AS IS'' AND ANY
> EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
> WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
> DISCLAIMED. IN NO EVENT SHALL Nicolas Garcia Belmonte BE LIABLE FOR ANY
> DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
> (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
> LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
> ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
> (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
> SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var $ = function(d) { return document.getElementById(d); };
$.empty = function() {};
$.lambda = function(value) {
return (typeof value == 'function') ? value : function(){
return value;
};
};
$.extend = function(original, extended){
for (var key in (extended || {})) original[key] = extended[key];
return original;
};
$.splat = function(obj){
var type = $.type(obj);
return (type) ? ((type != 'array') ? [obj] : obj) : [];
};
$.type = function(elem) {
return $.type.s.call(elem).match(/^\[object\s(.*)\]$/)[1].toLowerCase();
};
$.type.s = Object.prototype.toString;
$.each = function(iterable, fn){
var type = $.type(iterable);
if(type == 'object') {
for (var key in iterable) fn(iterable[key], key);
} else {
for(var i=0; i < iterable.length; i++) fn(iterable[i], i);
}
};
$.merge = function(){
var mix = {};
for (var i = 0, l = arguments.length; i < l; i++){
var object = arguments[i];
if ($.type(object) != 'object') continue;
for (var key in object){
var op = object[key], mp = mix[key];
mix[key] = (mp && $.type(op) == 'object' && $.type(mp) == 'object') ? $.merge(mp, op) : $.unlink(op);
}
}
return mix;
};
$.unlink = function(object){
var unlinked;
switch ($.type(object)){
case 'object':
unlinked = {};
for (var p in object) unlinked[p] = $.unlink(object[p]);
break;
case 'array':
unlinked = [];
for (var i = 0, l = object.length; i < l; i++) unlinked[i] = $.unlink(object[i]);
break;
default: return object;
}
return unlinked;
};
$.rgbToHex = function(srcArray, array){
if (srcArray.length < 3) return null;
if (srcArray.length == 4 && srcArray[3] == 0 && !array) return 'transparent';
var hex = [];
for (var i = 0; i < 3; i++){
var bit = (srcArray[i] - 0).toString(16);
hex.push((bit.length == 1) ? '0' + bit : bit);
}
return (array) ? hex : '#' + hex.join('');
};
$.hexToRgb = function(hex) {
if(hex.length != 7) {
hex = hex.match(/^#?(\w{1,2})(\w{1,2})(\w{1,2})$/);
hex.shift();
if (hex.length != 3) return null;
var rgb = [];
for(var i=0; i<3; i++) {
var value = hex[i];
if (value.length == 1) value += value;
rgb.push(parseInt(value, 16));
}
return rgb;
} else {
hex = parseInt(hex.slice(1), 16);
return [
hex >> 16,
hex >> 8 & 0xff,
hex & 0xff
];
}
}
$.destroy = function(elem) {
$.clean(elem);
if(elem.parentNode) elem.parentNode.removeChild(elem);
if(elem.clearAttributes) elem.clearAttributes();
};
$.clean = function(elem) {
for(var ch = elem.childNodes, i=0; i < ch.length; i++) {
$.destroy(ch[i]);
}
};
$.addEvent = function(obj, type, fn) {
if (obj.addEventListener)
obj.addEventListener(type, fn, false);
else
obj.attachEvent('on' + type, fn);
};
$.hasClass = function(obj, klass) {
return (' ' + obj.className + ' ').indexOf(' ' + klass + ' ') > -1;
};
$.addClass = function(obj, klass) {
if(!$.hasClass(obj, klass)) obj.className = (obj.className + " " + klass);
};
$.removeClass = function(obj, klass) {
obj.className = obj.className.replace(new RegExp('(^|\\s)' + klass + '(?:\\s|$)'), '$1');
};
$.getPos = function(elem) {
if(elem.getBoundingClientRect) {
var bound = elem.getBoundingClientRect(), html = elem.ownerDocument.documentElement;
return {
x: bound.left + html.scrollLeft - html.clientLeft,
y: bound.top + html.scrollTop - html.clientTop
};
}
var offset = getOffsets(elem);
var scroll = getScrolls(elem);
return {x: offset.x - scroll.x, y: offset.y - scroll.y};
function getOffsets(elem) {
var position = { x: 0, y: 0 };
while (elem && !isBody(elem)){
position.x += elem.offsetLeft;
position.y += elem.offsetTop;
elem = elem.offsetParent;
}
return position;
}
function getScrolls(elem){
var position = {x: 0, y: 0};
while (elem && !isBody(elem)){
position.x += elem.scrollLeft;
position.y += elem.scrollTop;
elem = elem.parentNode;
}
return position;
}
function isBody(element){
return (/^(?:body|html)$/i).test(element.tagName);
}
};
var Class = function(properties){
properties = properties || {};
var klass = function(){
for (var key in this){
if (typeof this[key] != 'function') this[key] = $.unlink(this[key]);
}
this.constructor = klass;
if (Class.prototyping) return this;
var instance = (this.initialize) ? this.initialize.apply(this, arguments) : this;
return instance;
};
for (var mutator in Class.Mutators){
if (!properties[mutator]) continue;
properties = Class.Mutators[mutator](properties, properties[mutator]);
delete properties[mutator];
}
$.extend(klass, this);
klass.constructor = Class;
klass.prototype = properties;
return klass;
};
Class.Mutators = {
Implements: function(self, klasses){
$.each($.splat(klasses), function(klass){
Class.prototying = klass;
var instance = (typeof klass == 'function')? new klass : klass;
for(var prop in instance) {
if(!(prop in self)) {
self[prop] = instance[prop];
}
}
delete Class.prototyping;
});
return self;
}
};
$.extend(Class, {
inherit: function(object, properties){
var caller = arguments.callee.caller;
for (var key in properties){
var override = properties[key];
var previous = object[key];
var type = $.type(override);
if (previous && type == 'function'){
if (override != previous){
if (caller){
override.__parent = previous;
object[key] = override;
} else {
Class.override(object, key, override);
}
}
} else if(type == 'object'){
object[key] = $.merge(previous, override);
} else {
object[key] = override;
}
}
if (caller) object.parent = function(){
return arguments.callee.caller.__parent.apply(this, arguments);
};
return object;
},
override: function(object, name, method){
var parent = Class.prototyping;
if (parent && object[name] != parent[name]) parent = null;
var override = function(){
var previous = this.parent;
this.parent = parent ? parent[name] : object[name];
var value = method.apply(this, arguments);
this.parent = previous;
return value;
};
object[name] = override;
}
});
Class.prototype.implement = function(){
var proto = this.prototype;
$.each(Array.prototype.slice.call(arguments || []), function(properties){
Class.inherit(proto, properties);
});
return this;
};
var Event = {
getPos: function(e, win) {
// get mouse position
win = win || window;
e = e || win.event;
var doc = win.document;
doc = doc.html || doc.body;
var page = {
x: e.pageX || e.clientX + doc.scrollLeft,
y: e.pageY || e.clientY + doc.scrollTop
};
return page;
}
};
/*
Object: TM.Util
Some common JSON tree manipulation methods.
*/
TM.Util = {
/*
Method: prune
Clears all tree nodes having depth greater than maxLevel.
Parameters:
tree - A JSON tree object. For more information please see <Loader.loadJSON>.
maxLevel - An integer specifying the maximum level allowed for this tree. All nodes having depth greater than max level will be deleted.
*/
prune: function(tree, maxLevel) {
this.each(tree, function(elem, i) {
if(i == maxLevel && elem.children) {
delete elem.children;
elem.children = [];
}
});
},
/*
Method: getParent
Returns the parent node of the node having _id_ as id.
Parameters:
tree - A JSON tree object. See also <Loader.loadJSON>.
id - The _id_ of the child node whose parent will be returned.
Returns:
A tree JSON node if any, or false otherwise.
*/
getParent: function(tree, id) {
if(tree.id == id) return false;
var ch = tree.children;
if(ch && ch.length > 0) {
for(var i=0; i<ch.length; i++) {
if(ch[i].id == id)
return tree;
else {
var ans = this.getParent(ch[i], id);
if(ans) return ans;
}
}
}
return false;
},
/*
Method: getSubtree
Returns the subtree that matches the given id.
Parameters:
tree - A JSON tree object. See also <Loader.loadJSON>.
id - A node *unique* identifier.
Returns:
A subtree having a root node matching the given id. Returns null if no subtree matching the id is found.
*/
getSubtree: function(tree, id) {
if(tree.id == id) return tree;
for(var i=0, ch=tree.children; i<ch.length; i++) {
var t = this.getSubtree(ch[i], id);
if(t != null) return t;
}
return null;
},
/*
Method: getLeaves
Returns the leaves of the tree.
Parameters:
node - A JSON tree node. See also <Loader.loadJSON>.
maxLevel - _optional_ A subtree's max level.
Returns:
An array having objects with two properties.
- The _node_ property contains the leaf node.
- The _level_ property specifies the depth of the node.
*/
getLeaves: function (node, maxLevel) {
var leaves = [], levelsToShow = maxLevel || Number.MAX_VALUE;
this.each(node, function(elem, i) {
if(i < levelsToShow &&
(!elem.children || elem.children.length == 0 )) {
leaves.push({
'node':elem,
'level':levelsToShow - i
});
}
});
return leaves;
},
/*
Method: eachLevel
Iterates on tree nodes with relative depth less or equal than a specified level.
Parameters:
tree - A JSON tree or subtree. See also <Loader.loadJSON>.
initLevel - An integer specifying the initial relative level. Usually zero.
toLevel - An integer specifying a top level. This method will iterate only through nodes with depth less than or equal this number.
action - A function that receives a node and an integer specifying the actual level of the node.
Example:
(start code js)
TM.Util.eachLevel(tree, 0, 3, function(node, depth) {
alert(node.name + ' ' + depth);
});
(end code)
*/
eachLevel: function(tree, initLevel, toLevel, action) {
if(initLevel <= toLevel) {
action(tree, initLevel);
for(var i=0, ch = tree.children; i<ch.length; i++) {
this.eachLevel(ch[i], initLevel +1, toLevel, action);
}
}
},
/*
Method: each
A tree iterator.
Parameters:
tree - A JSON tree or subtree. See also <Loader.loadJSON>.
action - A function that receives a node.
Example:
(start code js)
TM.Util.each(tree, function(node) {
alert(node.name);
});
(end code)
*/
each: function(tree, action) {
this.eachLevel(tree, 0, Number.MAX_VALUE, action);
},
/*
Method: loadSubtrees
Appends subtrees to leaves by requesting new subtrees
with the _request_ method.
Parameters:
tree - A JSON tree node. <Loader.loadJSON>.
controller - An object that implements a request method.
Example:
(start code js)
TM.Util.loadSubtrees(leafNode, {
request: function(nodeId, level, onComplete) {
//Pseudo-code to make an ajax request for a new subtree
// that has as root id _nodeId_ and depth _level_ ...
Ajax.request({
'url': 'http://subtreerequesturl/',
onSuccess: function(json) {
onComplete.onComplete(nodeId, json);
}
});
}
});
(end code)
*/
loadSubtrees: function(tree, controller) {
var maxLevel = controller.request && controller.levelsToShow;
var leaves = this.getLeaves(tree, maxLevel),
len = leaves.length,
selectedNode = {};
if(len == 0) controller.onComplete();
for(var i=0, counter=0; i<len; i++) {
var leaf = leaves[i], id = leaf.node.id;
selectedNode[id] = leaf.node;
controller.request(id, leaf.level, {
onComplete: function(nodeId, tree) {
var ch = tree.children;
selectedNode[nodeId].children = ch;
if(++counter == len) {
controller.onComplete();
}
}
});
}
}
};
/*
* File: Options.js
*
* Visualization common options.
*
*/
/*
* Object: Options
*
* Parent object for common Options.
*
*/
var Options = function() {
var args = Array.prototype.slice.call(arguments);
for(var i=0, l=args.length, ans={}; i<l; i++) {
var opt = Options[args[i]];
if(opt.$extend) {
$.extend(ans, opt);
} else {
ans[args[i]] = opt;
}
}
return ans;
};
/*
Object: Options.Controller
Provides controller methods.
Description:
You can implement controller functions inside the configuration object of all visualizations.
*Common to all visualizations*
- _onBeforeCompute(node)_ This method is called right before performing all computation and animations to the JIT visualization.
- _onAfterCompute()_ This method is triggered right after all animations or computations for the JIT visualizations ended.
*Used in <Canvas> based visualizations <ST>, <Hypertree>, <RGraph>*
- _onCreateLabel(domElement, node)_ This method receives the label dom element as first parameter, and the corresponding <Graph.Node> as second parameter. This method will only be called on label creation. Note that a <Graph.Node> is a node from the input tree/graph you provided to the visualization. If you want to know more about what kind of JSON tree/graph format is used to feed the visualizations, you can take a look at <Loader.loadJSON>. This method proves useful when adding events to the labels used by the JIT.
- _onPlaceLabel(domElement, node)_ This method receives the label dom element as first parameter and the corresponding <Graph.Node> as second parameter. This method is called each time a label has been placed on the visualization, and thus it allows you to update the labels properties, such as size or position. Note that onPlaceLabel will be triggered after updating the labels positions. That means that, for example, the left and top css properties are already updated to match the nodes positions.
- _onBeforePlotNode(node)_ This method is triggered right before plotting a given node. The _node_ parameter is the <Graph.Node> to be plotted.
This method is useful for changing a node style right before plotting it.
- _onAfterPlotNode(node)_ This method is triggered right after plotting a given node. The _node_ parameter is the <Graph.Node> plotted.
- _onBeforePlotLine(adj)_ This method is triggered right before plotting an edge. The _adj_ parameter is a <Graph.Adjacence> object.
This method is useful for adding some styles to a particular edge before being plotted.
- _onAfterPlotLine(adj)_ This method is triggered right after plotting an edge. The _adj_ parameter is the <Graph.Adjacence> plotted.
*Used in <TM> (Treemap) and DOM based visualizations*
- _onCreateElement(content, node, isLeaf, elem1, elem2)_ This method is called on each newly created node.
Parameters:
content - The div wrapper element with _content_ className.
node - The corresponding JSON tree node (See also <Loader.loadJSON>).
isLeaf - Whether is a leaf or inner node. If the node's an inner tree node, elem1 and elem2 will become the _head_ and _body_ div elements respectively.
If the node's a _leaf_, then elem1 will become the div leaf element.
- _onDestroyElement(content, node, isLeaf, elem1, elem2)_ This method is called before collecting each node. Takes the same parameters as onCreateElement.
*Used in <ST> and <TM>*
- _request(nodeId, level, onComplete)_ This method is used for buffering information into the visualization. When clicking on an empty node,
the visualization will make a request for this node's subtrees, specifying a given level for this subtree (defined by _levelsToShow_). Once the request is completed, the _onComplete_
object should be called with the given result.
*/
Options.Controller = {
$extend: true,
onBeforeCompute: $.empty,
onAfterCompute: $.empty,
onCreateLabel: $.empty,
onPlaceLabel: $.empty,
onComplete: $.empty,
onBeforePlotLine:$.empty,
onAfterPlotLine: $.empty,
onBeforePlotNode:$.empty,
onAfterPlotNode: $.empty,
onCreateElement: $.empty,
onDestroyElement:$.empty,
request: false
};
/*
Object: Options.Tips
Options for Tips
Description:
Options for Tool Tips.
Implemented by:
<TM>
These configuration parameters are currently used by <TM>.
- _enable_ If *true*, a tooltip will be shown when a node is hovered. The tooltip is a div DOM element having "tip" as CSS class. Default's *false*.
- _offsetX_ An offset added to the current tooltip x-position (which is the same as the current mouse position). Default's 20.
- _offsetY_ An offset added to the current tooltip y-position (which is the same as the current mouse position). Default's 20.
- _onShow(tooltip, node, isLeaf, domElement)_ Implement this method to change the HTML content of the tooltip when hovering a node.
Parameters:
tooltip - The tooltip div element.
node - The corresponding JSON tree node (See also <Loader.loadJSON>).
isLeaf - Whether is a leaf or inner node.
domElement - The current hovered DOM element.
*/
Options.Tips = {
$extend: false,
enable: false, // TODO(nico) change allow for enable
attachToDOM: true,
attachToCanvas: false,
offsetX: 20,
offsetY: 20,
onShow: $.empty
};
/*
* File: Extras.js
*
* Provides Extras such as Tips and Style Effects.
*
* Description:
*
* Provides the <Tips> and <NodeStyles> classes and functions.
*
*/
/*
* Provides the initialization function for <NodeStyles> and <Tips> implemented
* by all main visualizations.
*
*/
var Extras = {
initializeExtras: function() {
var tips = this.config.Tips;
if(tips) {
this.tips = new Tips(this);
}
}
};
/*
Class: Tips
A class containing tip related functions. This class is used internally.
Used by:
<ST>, <Sunburst>, <Hypertree>, <RGraph>, <TM>, <ForceDirected>, <Icicle>
See also:
<Options.Tips>
*/
var Tips = new Class({
initialize: function(viz) {
this.viz = viz;
this.controller = this.config = viz.config;
// add tooltip
if(this.config.Tips.enable && document.body) {
var tip = document.getElementById('_tooltip') || document.createElement('div');
tip.id = '_tooltip';
tip.className = 'tip';
var style = tip.style;
style.position = 'absolute';
style.display = 'none';
style.zIndex = 13000;
document.body.appendChild(tip);
this.tip = tip;
this.node = false;
}
},
attach: function(node, elem) {
if(this.config.Tips.enable) {
var that = this, cont = this.controller;
$.addEvent(elem, 'mouseover', function(e){
cont.Tips.onShow(that.tip, node, elem);
});
$.addEvent(elem, 'mouseout', function(e){
that.tip.style.display = 'none';
});
// Add mousemove event handler
$.addEvent(elem, 'mousemove', function(e, win){
var pos = Event.getPos(e, win);
that.setTooltipPosition(pos);
});
}
},
onClick: $.empty,
onRightClick: $.empty,
onMousemove: function(node, opt) {
if(!node) {
this.tip.style.display = 'none';
this.node = false;
return;
}
if(!this.node || this.node.id != node.id) {
this.node = node;
this.config.Tips.onShow(this.tip, node);
}
this.setTooltipPosition(opt.position);
},
setTooltipPosition: function(pos) {
var tip = this.tip, style = tip.style, cont = this.config;
style.display = '';
// get window dimensions
win = {
'height': document.body.clientHeight,
'width': document.body.clientWidth
};
// get tooltip dimensions
var obj = {
'width': tip.offsetWidth,
'height': tip.offsetHeight
};
// set tooltip position
var x = cont.Tips.offsetX, y = cont.Tips.offsetY;
style.top = ((pos.y + y + obj.height > win.height)?
(pos.y - obj.height - y) : pos.y + y) + 'px';
style.left = ((pos.x + obj.width + x > win.width)?
(pos.x - obj.width - x) : pos.x + x) + 'px';
}
});
/*
* File: Treemap.js
*
* Implements the <TM> class and other derived classes.
*
* Description:
*
* A Treemap is an information visualization technique, proven very useful when displaying large hierarchical structures on a constrained space. The idea behind a Treemap is to describe hierarchical relations as 'containment'. That means that if node B is child of node A, then B 'is contained' in A.
*
* Inspired by:
*
* Squarified Treemaps (Mark Bruls, Kees Huizing, and Jarke J. van Wijk)
*
* <http://www.win.tue.nl/~vanwijk/stm.pdf>
*
* Tree visualization with tree-maps: 2-d space-filling approach (Ben Shneiderman)
*
* <http://hcil.cs.umd.edu/trs/91-03/91-03.html>
*
* Disclaimer:
*
* This visualization was built from scratch, taking only these papers as inspiration, and only shares some features with the Treemap papers mentioned above.
*
*/
/*
Class: TM
Abstract Treemap class.
Implements:
<Tips>
Implemented By:
<TM.Squarified>, <TM.Strip> and <TM.SliceAndDice>.
Description:
Implements layout and configuration options inherited by <TM.Squarified>, <TM.Strip> and <TM.SliceAndDice>.
All Treemap constructors take the same configuration object as parameter.
Two special _data_ keys are read from the JSON tree structure loaded by <Loader.loadJSON> to calculate
node's color and dimensions. These properties are $.area (for nodes dimensions) and $.color. Both of these properties are floats.
This means that the tree structure defined in <Loader.loadJSON> should now look more like this
(start code js)
var json = {
"id": "aUniqueIdentifier",
"name": "usually a nodes name",
"data": {
"$.area": 33, //some float value
"$.color": 36, //-optional- some float value
"some key": "some value",
"some other key": "some other value"
},
"children": [ 'other nodes or empty' ]
};
(end code)
If you want to know more about JSON tree structures and the _data_ property please read <Loader.loadJSON>.
Configuration:
*General*
- _rootId_ The id of the div container where the Treemap will be injected. Default's 'infovis'.
- _orientation_ For <TM.Strip> and <TM.SliceAndDice> only. The layout algorithm orientation. Possible values are 'h' or 'v'.
- _levelsToShow_ Max depth of the plotted tree. Useful when using the request method.
- _addLeftClickHandler_ Add a left click event handler to zoom in the Treemap view when clicking a node. Default's *false*.
- _addRightClickHandler_ Add a right click event handler to zoom out the Treemap view. Default's *false*.
- _selectPathOnHover_ If setted to *true* all nodes contained in the path between the hovered node and the root node will have an *in-path* CSS class. Default's *false*.
*Nodes*
There are two kinds of Treemap nodes.
(see treemapnode.png)
Inner nodes are nodes having children, like _Pearl Jam_.
These nodes are represented by three div elements. A _content_ element, a _head_ element (where the title goes) and a _body_ element, where the children are laid out.
(start code xml)
<div class="content">
<div class="head">Pearl Jam</div>
<div class="body">...other nodes here...</div>
</div>
(end code)
Leaves are optionally colored nodes laying at the "bottom" of the tree. For example, _Yield_, _Vs._ and _Riot Act_ are leaves.
These nodes are represented by two div elements. A _content_ element and a wrapped _leaf_ element
(start code xml)
<div class="content">
<div class="leaf">Yield</div>
</div>
(end code)
There are some configuration properties regarding Treemap nodes
- _titleHeight_ The height of the title (_head_) div container. Default's 13.
- _offset_ The separation offset between the _content_ div element and its contained div(s). Default's 4.
*Color*
_Color_ is an object containing as properties
- _enable_ If *true*, the algorithm will check for the JSON node data _$.color_ property to add some color to the Treemap leaves.
This color is calculated by interpolating a node's $.color value range with a real RGB color range.
By specifying min|maxValues for the $.color property and min|maxColorValues for the RGB counterparts, the visualization is able to
interpolate color values and assign a proper color to the leaf node. Default's *false*.
- _minValue_ The minimum value expected for the $.color value property. Used for interpolating. Default's -100.
- _maxValue_ The maximum value expected for the $.color value property. Used for interpolating. Default's 100.
- _minColorValue_ A three-element RGB array defining the color to be assigned to the _$.color_ having _minValue_ as value. Default's [255, 0, 50].
- _maxColorValue_ A three-element RGB array defining the color to be assigned to the _$.color_ having _maxValue_ as value. Default's [0, 255, 50].
*Tips*
See <Options.Tips>.
*Controller options*
See <Options.Controller>.
See also <TM.Squarified>, <TM.SliceAndDice> and <TM.Strip>.
*/
TM.Base = $.extend({
layout: {
orientation: "h",
vertical: function() {
return this.orientation == "v";
},
horizontal: function() {
return this.orientation == "h";
},
change: function() {
this.orientation = this.vertical()? "h" : "v";
}
},
config: {
orientation: "h",
titleHeight: 13,
rootId: 'infovis',
offset:4,
levelsToShow: 3,
addLeftClickHandler: false,
addRightClickHandler: false,
selectPathOnHover: false,
Tips: Options.Tips,
Color: {
enable: false,
minValue: -100,
maxValue: 100,
minColorValue: [255, 0, 50],
maxColorValue: [0, 255, 50]
}
},
initialize: function(controller) {
this.tree = null;
this.shownTree = null;
this.controller = this.config = $.merge(Options.Controller,
this.config,
controller);
this.rootId = this.config.rootId;
this.layout.orientation = this.config.orientation;
// add tips
this.initializeExtras();
// purge
var that = this;
var fn = function() {
that.empty();
if(window.CollectGarbage) window.CollectGarbage();
delete fn;
};
if(window.addEventListener) {
window.addEventListener('unload', fn, false);
} else {
window.attachEvent('onunload', fn);
}
},
/*
Method: each
Traverses head and leaf nodes applying a given function
Parameters:
f - A function that takes as parameters the same as the onCreateElement and onDestroyElement methods described in <TM>.
*/
each: function(f) {
(function rec(elem) {
if(!elem) return;
var ch = elem.childNodes, len = ch.length;
if(len > 0) {