-
Notifications
You must be signed in to change notification settings - Fork 9
/
TreeMap.js
664 lines (592 loc) · 18.6 KB
/
TreeMap.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
/** @module dtreemap/TreeMap */
define(["dcl/dcl", "delite/register", "dcolor/Color",
"dojo/dom-geometry", "dojo/dom-class",
"./_utils", "dpointer/events", "delite/Widget", "delite/Selection",
"delite/StoreMap", "requirejs-dplugins/css!./themes/TreeMap.css"],
function (dcl, register, Color, domGeom, domClass,
utils, pointer, Widget, Selection, StoreMap) {
/**
* A treemap widget.
* @class module:dtreemap/TreeMap
* @augments module:delite/Widget
* @augments module:delite/Selection
* @augments module:delite/StoreMap
*/
return register("d-treemap", [HTMLElement, Widget, Selection, StoreMap], /** @lends module:dtreemap/TreeMap# */ {
baseClass: "d-treemap",
/**
* The associated array item to renderer list.
* @member {Object}
* @protected
*/
itemToRenderer: null,
/**
* The root item of the treemap, that is the first visible item.
* If null the entire treemap hierarchy is shown.
* @member {Object}
* @default null
*/
rootItem: null,
/**
* The attribute of the store item that contains the tooltip text of a treemap cell.
* @member {string}
* @default ""
*/
tooltipAttr: "",
/**
* A function that returns the tooltip text of a treemap cell from a store item. If specified takes
* precedence over tooltipAttr.
* @member {Function}
* @default null
*/
tooltipFunc: null,
/**
* The attribute of the store item that contains the data used to compute the area of a treemap cell.
* @member {string}
* @default ""
*/
areaAttr: "",
/**
* A function that returns the value use to compute the area of cell from a store item. If specified
* takes precedence over areaAttr.
* @member {Function}
* @default null
*/
areaFunc: null,
/**
* The attribute of the store item that contains the label of a treemap cell.
* @member {string}
* @default "label"
*/
labelAttr: "label",
/**
* A function that returns the label of a treemap cell from a store item. If specified takes
* precedence over labelAttr.
* @member {Function}
* @default null
*/
labelFunc: null,
/**
* The starting depth level at which the labels are not displayed anymore on cells.
* If NaN no threshold is applied. The depth is the visual depth of the items on the screen not
* in the data (i.e. after drill down the depth of an item might change).
* @member {number}
* @default NaN
*/
labelThreshold: NaN,
/**
* The attribute of the store item that contains the data used to compute the color of a treemap cell.
* @member {string}
* @default ""
*/
colorAttr: "",
/**
* A function that returns from a store item the data used to compute the color of a treemap cell.
* If specified takes precedence over colorAttr.
* @member {Function}
* @default null
*/
colorFunc: null,
/**
* The optional color model that converts data to color.
* @member {dcolor/api/ColorModel}
* @default null
*/
colorModel: null,
/**
* An array of data attributes used to group data in the treemap.
* @member {string[]}
* @default []
*/
groupAttrs: [],
/**
* An array of grouping functions used to group data in the treemap.
* When null, groupAttrs is used to compute grouping functions.
* @member {Function[]}
* @default null
*/
groupFuncs: null,
_groupFuncs: null,
mapAtInit: false,
copyAllItemProps: true,
preRender: function () {
this.allowRemap = true;
this.itemToRenderer = {};
},
getIdentity: function (item) {
return item.__treeID ? item.__treeID : this.store.getIdentity(item);
},
/**
* To be called when the TreeMap should relayout itself following a change in CSS layout. The application
* typically calls this method when a `resize` event occurs.
*/
update: function () {
this.notifyCurrentValue("areaAttr");
},
postRender: function () {
this.on("pointerover", this._pointerOverHandler.bind(this));
this.on("pointerout", this._pointerOutHandler.bind(this));
this.on("pointerup", this._pointerUpHandler.bind(this));
this.setAttribute("role", "presentation");
this.setAttribute("aria-label", "treemap");
},
// we need to call Store.computeProperties
computeProperties: function (props) {
if (this.renderItems && this._mappedKeys.some(function (item) {
return props.hasOwnProperty(item + "Attr") || props.hasOwnProperty(item + "Func");
})) {
this.remap();
}
if ("renderItems" in props || "groupAttrs" in props || "groupFuncs" in props) {
this._set("rootItem", null);
this.notifyCurrentValue("rootItem");
}
if ("renderItems" in props) {
this.notifyCurrentValue("groupAttrs");
this.notifyCurrentValue("colorAttr");
}
},
/* jshint maxcomplexity: 13 */
refreshRendering: function (props) {
var refresh = false;
if ("groupAttrs" in props || "groupFuncs" in props) {
this._updateTreeMapHierarchy();
refresh = true;
}
if ("colorAttr" in props || "colorFunc" in props || "colorModel" in props) {
refresh = true;
if (this.colorModel != null && this.renderItems != null && this.colorModel.initialize) {
this.colorModel.initialize(this.renderItems, this._colorFunc.bind(this));
}
}
if ("areaAttr" in props || "areaFunc" in props) {
this._removeAreaForGroup();
refresh = true;
}
if (("rootItem" in props || refresh) && this._groupeditems) {
if (this.containerNode == null) {
this.containerNode = this.ownerDocument.createElement("div");
dcl.mix(this.containerNode.style, {
"position": "relative",
"width": "100%",
"height": "100%"
});
this.appendChild(this.containerNode);
}
if ("rootItem" in props) {
this.containerNode.innerHTML = "";
this._render(true);
} else {
this._render(false);
}
}
},
/* jshint maxcomplexity: 10 */
_render: function (forceCreate) {
var rootItem = this.rootItem, rootParentItem;
if (rootItem != null) {
var rootItemRenderer = this._getRenderer(rootItem);
if (rootItemRenderer) {
if (this._isLeaf(rootItem)) {
rootItem = rootItemRenderer.parentItem;
}
rootParentItem = rootItemRenderer.parentItem;
}
}
var box = domGeom.getMarginBox(this);
if (rootItem != null && !this._isRoot(rootItem)) {
this._buildRenderer(this.containerNode, rootParentItem, rootItem, {
x: 0,
y: 0,
w: box.w,
h: box.h
}, 0, forceCreate);
} else {
this._buildChildrenRenderers(this.containerNode,
rootItem ? rootItem : { __treeRoot: true, children: this._groupeditems },
0, forceCreate, null);
}
},
_setGroupAttrsAttr: function (value) {
if (this.groupFuncs == null) {
if (value != null) {
this._groupFuncs = value.map(function (attr) {
return function (item) {
return item[attr];
};
});
} else {
this._groupFuncs = null;
}
}
this._set("groupAttrs", value);
},
_setGroupFuncsAttr: function (value) {
this._set("groupFuncs", this._groupFuncs = value);
if (value == null && this.groupAttrs != null) {
this._groupFuncs = this.groupAttrs.map(function (attr) {
return function (item) {
return item[attr];
};
});
}
},
_colorFunc: function (/*Object*/ item) {
if (this.colorFunc) {
return this.colorFunc(item);
} else {
var color = item.color;
if (!color) {
color = 0;
}
return parseFloat(color);
}
},
/**
* Creates an item renderer of the specified kind. This is called only when the treemap
* is created. Default implementation always create div nodes. It also sets overflow
* to hidden and position to absolute on non-header renderers.
* @param item The render item.
* @param level The item depth level.
* @param kind The specified kind. This can either be "leaf", "group", "header" or "content".
* @returns {HTMLElement} The renderer use for the specified kind.
* @protected
*/
createRenderer: function (item, level, kind) {
var div = this.ownerDocument.createElement("div");
if (kind !== "header") {
dcl.mix(div.style, {
"overflow": "hidden",
"position": "absolute"
});
}
return div;
},
/**
* Style the item renderer. This is called each time the treemap is refreshed.
* For leaf items it colors them with the color computed from the color model.
* For other items it does nothing.
* @param renderer The item renderer.
* @param item The render item.
* @param level The item depth level.
* @param kind The specified kind. This can either be "leaf", "group", "header" or "content".
* @protected
*/
styleRenderer: function (renderer, item, level, kind) {
switch (kind) {
case "leaf":
renderer.style.background = this.getColorForItem(item).toHex();
/* falls through */
case "header":
var label = this.getLabelForItem(item);
if (label && (isNaN(this.labelThreshold) || level < this.labelThreshold)) {
renderer.innerHTML = label;
} else {
renderer.innerHTML = "";
}
break;
default:
}
},
_updateTreeMapHierarchy: function () {
var items = this.renderItems;
if (items == null) {
return;
}
if (this._groupFuncs != null && this._groupFuncs.length > 0) {
this._groupeditems = utils.group(items, this._groupFuncs,
this._getAreaForItem.bind(this)).children;
} else {
this._groupeditems = items;
}
},
_removeAreaForGroup: function (item) {
var children;
if (item != null) {
if (item.__treeValue) {
delete item.__treeValue;
children = item.children;
} else {
// not a grouping item
return;
}
} else {
children = this._groupeditems;
}
if (children) {
for (var i = 0; i < children.length; ++i) {
this._removeAreaForGroup(children[i]);
}
}
},
_getAreaForItem: function (item) {
var area = parseFloat(item.area);
return isNaN(area) ? 0 : area;
},
_computeAreaForItem: function (item) {
var value;
if (item.__treeID) { // group
value = item.__treeValue;
if (!value) {
value = 0;
var children = item.children;
for (var i = 0; i < children.length; ++i) {
value += this._computeAreaForItem(children[i]);
}
item.__treeValue = value;
}
} else {
value = this._getAreaForItem(item);
}
return value;
},
/**
* Returns the color for a given item. This either use the colorModel if not null
* or just the result of the colorFunc.
* @param item The render item.
* @returns {dcolor/Color} The item color
*/
getColorForItem: function (item) {
var value = this._colorFunc(item);
if (this.colorModel != null) {
return this.colorModel.getColor(value);
} else {
return new Color(value);
}
},
/**
* Returns the label for a given item.
* @param item The render item.
* @returns {string}
*/
getLabelForItem: function (item) {
return item.__treeName ? item.__treeName : item.label.toString();
},
_buildChildrenRenderers: function (domNode, item, level, forceCreate, delta, anim) {
var children = item.children;
var box = domGeom.getMarginBox(domNode);
var solution = utils.solve(children, box.w, box.h, this._computeAreaForItem.bind(this),
this.effectiveDir === "rtl");
var rectangles = solution.rectangles;
if (delta) {
rectangles = rectangles.map(function (item) {
item.x += delta.l;
item.y += delta.t;
return item;
});
}
var rectangle;
for (var j = 0; j < children.length; ++j) {
rectangle = rectangles[j];
this._buildRenderer(domNode, item, children[j], rectangle, level, forceCreate, anim);
}
},
_isLeaf: function (item) {
return !item.children;
},
_isRoot: function (item) {
return item.__treeRoot;
},
_getRenderer: function (item, anim, parent) {
if (anim) {
// while animating we do that on a copy of the subtree
// so we can use our hash object to get to the renderer
for (var i = 0; i < parent.children.length; ++i) {
if (parent.children[i].item === item) {
return parent.children[i];
}
}
}
return this.itemToRenderer[this.getIdentity(item)];
},
_buildRenderer: function (container, parent, child, rect, level, forceCreate, anim) {
var isLeaf = this._isLeaf(child);
var renderer = !forceCreate ? this._getRenderer(child, anim, container) : null;
renderer = isLeaf ? this._updateLeafRenderer(renderer, child, level) : this._updateGroupRenderer(renderer,
child, level);
if (forceCreate) {
renderer.level = level;
renderer.item = child;
renderer.parentItem = parent;
this.itemToRenderer[this.getIdentity(child)] = renderer;
// update its selection status
this.updateRenderers(child);
}
// in some cases the computation might be slightly incorrect (0.0000...1)
// and due to the floor this will cause 1px gaps
var x = Math.floor(rect.x);
var y = Math.floor(rect.y);
var w = Math.floor(rect.x + rect.w + 0.00000000001) - x;
var h = Math.floor(rect.y + rect.h + 0.00000000001) - y;
// before sizing put the item inside its parent so that styling
// is applied and taken into account
if (forceCreate) {
container.appendChild(renderer);
}
domGeom.setMarginBox(renderer, { l: x, t: y, w: w, h: h });
if (!isLeaf) {
var box = domGeom.getContentBox(renderer);
this._layoutGroupContent(renderer, box.w, box.h, level + 1, forceCreate, anim);
}
this.emit("treemap-renderer-updated",
{ renderer: renderer, item: child, kind: isLeaf ? "leaf" : "group", level: level });
},
_layoutGroupContent: function (renderer, width, height, level, forceCreate, anim) {
var header = renderer.querySelector(".d-treemap-header");
var content = renderer.querySelector(".d-treemap-groupcontent");
if (header == null || content == null) {
return;
}
var box = domGeom.getMarginBox(header);
// If the header is too high, reduce its area
// and don't show the children..
if (box.h > height) {
// TODO: this might cause pb when coming back to visibility later
// as the getMarginBox of the header will keep that value?
box.h = height;
content.style.display = "none";
} else {
content.style.dispaly = "block";
domGeom.setMarginBox(content, { l: 0, t: box.h, w: width, h: (height - box.h) });
this._buildChildrenRenderers(content, renderer.item, level, forceCreate, null, anim);
}
domGeom.setMarginBox(header, { l: 0, t: 0, w: width, h: box.h });
},
/**
* Update a group renderer. This creates the renderer if not already created,
* call styleRender for it and recurse into children.
* @param renderer The item renderer.
* @param item The render item.
* @param level The item depth level.
* @returns {HTMLElement}
* @private
*/
_updateGroupRenderer: function (renderer, item, level) {
var forceCreate = renderer == null;
if (renderer == null) {
renderer = this.createRenderer("div", level, "group");
domClass.add(renderer, "d-treemap-group");
}
this.styleRenderer(renderer, item, level, "group");
var header = renderer.querySelector(".d-treemap-header");
header = this._updateHeaderRenderer(header, item, level);
if (forceCreate) {
renderer.appendChild(header);
}
var content = renderer.querySelector(".d-treemap-groupcontent");
content = this._updateGroupContentRenderer(content, item, level);
if (forceCreate) {
renderer.appendChild(content);
}
return renderer;
},
/**
* Update a leaf renderer. This creates the renderer if not already created,
* call styleRender for it and set the label as its innerHTML.
* @param renderer The item renderer.
* @param item The render item.
* @param level The item depth level.
* @returns {HTMLElement}
* @private
*/
_updateHeaderRenderer: function (renderer, item, level) {
if (renderer == null) {
renderer = this.createRenderer(item, level, "header");
domClass.add(renderer, "d-treemap-header");
domClass.add(renderer, "d-treemap-header_" + level);
}
this.styleRenderer(renderer, item, level, "header");
return renderer;
},
/**
* Update a leaf renderer. This creates the renderer if not already created,
* call styleRender for it and set the label as its innerHTML.
* @param renderer The item renderer.
* @param item The render item.
* @param level The item depth level.
* @returns {HTMLElement}
* @private
*/
_updateLeafRenderer: function (renderer, item, level) {
if (renderer == null) {
renderer = this.createRenderer(item, level, "leaf");
domClass.add(renderer, "d-treemap-leaf");
domClass.add(renderer, "d-treemap-leaf_" + level);
}
this.styleRenderer(renderer, item, level, "leaf");
if (item.tooltip) {
renderer.title = item.tooltip;
}
return renderer;
},
/**
* Update a group content renderer. This creates the renderer if not already created,
* and call styleRender for it.
* @param renderer The item renderer.
* @param item The render item.
* @param level The item depth level
* @returns {HTMLElement}
* @private
*/
_updateGroupContentRenderer: function (renderer, item, level) {
if (renderer == null) {
renderer = this.createRenderer(item, level, "content");
domClass.add(renderer, "d-treemap-groupcontent");
domClass.add(renderer, "d-treemap-groupcontent_" + level);
}
this.styleRenderer(renderer, item, level, "content");
return renderer;
},
_getRendererFromTarget: function (target) {
var renderer = target;
while (renderer !== this && !renderer.item) {
renderer = renderer.parentNode;
}
return renderer;
},
_pointerOverHandler: function (e) {
var renderer = this._getRendererFromTarget(e.target);
if (renderer.item) {
var item = renderer.item;
this.updateRenderers(item);
this.emit("treemap-item-over", {renderer: renderer, item: item, triggerEvent: e});
}
},
_pointerOutHandler: function (e) {
var renderer = this._getRendererFromTarget(e.target);
if (renderer.item) {
var item = renderer.item;
this.updateRenderers(item);
this.emit("treemap-item-out", {renderer: renderer, item: item, triggerEvent: e});
}
},
_pointerUpHandler: function (e) {
var renderer = this._getRendererFromTarget(e.target);
if (renderer.item) {
this.selectFromEvent(e, renderer.item, renderer, true);
}
},
updateRenderers: function (items) {
if (!items) {
return;
}
if (!Array.isArray(items)) {
items = [items];
}
for (var i = 0; i < items.length; i++) {
var item = items[i];
var renderer = this._getRenderer(item);
// at init time the renderer might not be ready
if (!renderer) {
continue;
}
var selected = this.isSelected(item);
if (selected) {
domClass.add(renderer, "d-selected");
} else {
domClass.remove(renderer, "d-selected");
}
}
}
});
});