-
Notifications
You must be signed in to change notification settings - Fork 2
/
dag.js
878 lines (837 loc) · 26.2 KB
/
dag.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
let treeData;
let svgSelection;
let defs;
let layout;
let dag;
let nodes;
let graph;
let width = 600, height = 400;
let viewboxWidth = 11000, viewboxHeight = 10000;
let maxTextLength = 200;
let nodeWidth = maxTextLength + 20;
let nodeHeight = 140;
let shownNodesMap = {};
let nodeChildrenStateMap = {};
let nodeParentsStateMap = {};
let leavesNodesIds = [];
let rootsNodesIds = [];
let rootsNodesCoord = {};
let currentTree = [];
let zoomTransform;
let currentHighlightedNodeId;
// Define the zoom function for the zoomable tree
var zoom = d3.zoom()
.scaleExtent([1, 20])
.on('zoom', function(event) {
graph
.attr('transform', event.transform);
zoomTransform = event.transform;
});
// How to draw edges
const line = d3
.line()
.curve(d3.curveCatmullRom)
.x((d) => d.x + nodeWidth/2)
.y((d) => d.y + nodeHeight/2);
/**
* Initialize tree with the two top layers nodes
*/
function initGraph() {
// fetch data and render
treeData = JSON.parse(getDataFromSessionStorage(repoName + "Tree"));
let data = structuredClone(treeData); // A clone is made to avoid any modifications in the original data
for(let i=0;i<data.length;i++)
{
// Expand the nodes with no parents and initialize shownNodesMap with the shown nodes in the tree
// and nodeChildrenStateMap with the state of the node children(expanded or collapsed)
if(data[i]["parentIds"].length === 0)
{
shownNodesMap[data[i]["id"]] = 1;
currentTree.push(data[i]);
nodeChildrenExpand(data[i]["id"],data)
rootsNodesIds.push(data[i]["id"]);
rootsNodesCoord[data[i]["id"]] = [];
}
else{
if(shownNodesMap[data[i]["id"]] !== 1)
{
shownNodesMap[data[i]["id"]] = 0;
}
}
// Get nodes with no children to draw them without children expand/collapse button
if(getNodeChildren(data[i]["id"],data).length === 0)
{
leavesNodesIds.push(data[i]["id"])
}
}
// Remove hidden parents of the nodes in the currentTree
for (let i = 0; i < currentTree.length; i++)
{
if(shownNodesMap[currentTree[i]["id"]] === 1)
{
removeHiddenParents(currentTree[i]["parentIds"]);
}
}
updateShownNodeMap(treeData);
drawTree(currentTree,"init");
if(zoomTransform === undefined)
{
zoomTransform = d3.zoomIdentity
}
zoomTransform.k = 2.2;
zoomTransform.x = -2000;
zoomTransform.y = 2000;
graph.attr('transform', zoomTransform);
}
/**
* Interface to parse all data starting at
* @param {String} host of json file
* @param {String} dataDict dictionary at domain where the data is located
* @param {String} jsonRootFile file name of json file
*/
function parseData(host, dataDict, jsonDataFile) {
let jsonRootFullPath = (window.location.href.includes("localhost") || window.location.href.includes("127.0.")) ?
`./${jsonDataFile}` : `${host}${dataDict}${jsonDataFile}`;
let rawFile = new XMLHttpRequest();
rawFile.open("GET", jsonRootFullPath, false);
let allText;
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
allText = rawFile.responseText;
}
}
}
rawFile.send(null);
return JSON.parse(allText);
}
/**
* Performs action after the info label is clicked
* @param {Object} d clicked info
*/
function onNodeInfoClicked(d) {
let currentNodeId = d.currentTarget.__data__.data.id;
let node = getNodeByTitle(d.currentTarget.__data__.data.title);
$("#info_box").empty();
addNodeInfos(node, "preview");
updateGraphPlot(currentNodeId);
}
/**
* Performs action after a node is clicked
* @param {Object} d clicked info
*/
function onNodeClicked(d) {
let currentNodeId = d.currentTarget.__data__.data.id;
let node = getNodeByTitle(d.currentTarget.__data__.data.title);
$("#info_box").empty();
addNodeInfos(node, "preview");
updateGraphPlot(currentNodeId);
}
/**
* Method wraps long labels of nodes into multiple line label
* @param {String} text labels
* @param {Number} width max width of one line
*/
function wrapNodeText(text, width) {
text.each(function (d) {
let textd3 = d3.select(this);
if (textd3.node().getComputedTextLength() < width) return;
let words = textd3.text().split(" ").reverse();
// split into lines
let word;
let line = [];
let lineNumber = 0;
let lineHeight = 1; // ems
let x = textd3.attr('x');
let y = textd3.attr('y');
let dy = 0;
let tspan = textd3.text(null)
.append('tspan')
.attr('x', x)
.attr('y', y)
.attr('dy', dy );
while (word = words.pop()) {
line.push(word);
tspan.text(line.join(' '));
if (tspan.node().getComputedTextLength() > width) {
line.pop();
tspan.text(line.join(' '));
line = [word];
tspan = textd3.append('tspan')
.attr('x', x)
.attr('y', y)
.attr('dy', ++lineNumber * lineHeight + dy + 'em')
.text(word);
}
}
});
}
/**
* Performs graph update. Updates nodes and links.
* @param {String} currentNodeId
*/
function updateGraphPlot(currentNodeId) {
currentHighlightedNodeId = currentNodeId;
graphs = graph.selectAll("path");
paths = graphs._groups[0];
paths.forEach(function (d) {
source = d.__data__.source.data;
target = d.__data__.target.data;
if (currentNodeId == source.id) {
d.setAttribute("stroke-width", "10");
d.setAttribute("style", "stroke: rgb(255, 0, 0); transition: 0.5s");
}
else if (currentNodeId == target.id) {
d.setAttribute("stroke-width", "10");
d.setAttribute("style", "stroke: rgb(0, 0, 255); transition: 0.5s");
}
else {
d.setAttribute("stroke-width", "1.5");
d.setAttribute("style", "stroke: rgb(34, 34, 34); transition: 0.5s");
}
});
}
/**
* Toggle between expanding and collapsing node children
* @param {Object} d clicked node
* @param {String} command identify which action should be executed
*/
function onNodeChildrenToggle(d,command){
let currentNodeId = d.data.id;
let state;
if(command === "collapse")
{
state = "children collapse";
nodeChildrenStateMap[currentNodeId] = 0;
}
else
{
state = "children expand";
nodeChildrenStateMap[currentNodeId] = 1;
}
updateTree(currentNodeId,state);
}
function onNodeParentsToggle(d)
{
let currentNodeId = d.currentTarget.__data__.data.id;
let state = "parents expand";
updateTree(currentNodeId,state);
}
/**
* update currentTree after expand/collapse of a node
* @param {String} currentNodeId node ID
* @param {String} state node to be expanded or collapsed
*/
function updateTree(currentNodeId,state){
let data = structuredClone(treeData); // A clone is made to avoid any modifications in the original data
if(state === "children expand")
{
nodeChildrenExpand(currentNodeId,data);
}
else if(state === "children collapse")
{
nodeChildrenCollapse(currentNodeId);
}
else if (state === "parents expand")
{
nodeParentsExpand(currentNodeId,data);
}
for (let i = 0; i < currentTree.length; i++)
{
if(shownNodesMap[currentTree[i]["id"]] === 1)
{
removeHiddenParents(currentTree[i]["parentIds"]);
}
}
updateShownNodeMap(treeData)
highlightSelectedNode(currentNodeId);
updateTreeGraph(currentTree,currentNodeId, state);
}
/**
* draw the tree elements
* @param {Array} drawData current tree data
* @param {String} state initialize or update the tree
*/
function drawTree(drawData,state)
{
generateTreeLayout(drawData);
const { width, height } = layout(dag);
let sizeFactor = width/window.innerWidth
currentHighlightedNodeId = "-1";
// --------------------------------
// This code only handles rendering
// --------------------------------
svgSelection = d3.select("svg");
svgSelection.selectAll('*').remove();
if(state === "expand tree")
{
svgSelection.attr("viewBox", [0, 0, width, (window.innerHeight)*sizeFactor].join(" "));
}
else{
svgSelection.attr("viewBox", [0, 0, viewboxWidth, viewboxHeight].join(" "));
}
svgSelection.call(zoom);
graph = svgSelection.append("g");
defs = graph.append("defs"); // For gradients
// Plot edges
graph
.append("g")
.attr("class", "paths-list")
.selectAll("path")
.data(dag.links())
.enter()
.append("path")
.attr("d", ({ points }) => line(points))
.attr("fill", "none")
.attr("stroke-width", 3)
.style("stroke", "#222222");
// Select nodes
nodes = graph
.append("g")
.attr("class", "nodes-list")
.selectAll("g")
.data(dag.descendants())
.enter()
.append("g")
.attr("class", "node")
.attr("transform", ({x,y}) => `translate(${x}, ${y})`);
// Plot nodes
nodes
.append("rect")
.attr("width", nodeWidth)
.attr("height", nodeHeight)
.attr("rx", function (d) {
switch (d.data.nodeType) {
case "designParameter":
return 40;
case "systemIndependent":
return 40;
default:
return 2;
}
})
.attr("stroke-width", 1.5)
.style("fill", function (d) {
switch (d.data.nodeType) {
case "designParameter":
return "#b4acd2";
case "systemIndependent":
return "#ace3b5";
default:
return "#f4f4f9";
}
})
.on("click", onNodeClicked);
// Add text to nodes
nodes
.append("text")
.attr("y", nodeHeight / 2)
.attr("x", 13)
.attr("dy", ".35em")
.text((d) => d.data.title)
.call(wrapNodeText, maxTextLength)
.on("click", onNodeClicked);
// Add information icon
nodes.append("circle")
.attr("class", "iButton")
.attr("cx", nodeWidth-20)
.attr("cy", 20)
.attr("r", 15)
.on("mouseover", function () { d3.select(this).attr("r", 20); })
.on("mouseout", function () { d3.select(this).attr("r", 15); })
.on("click", onNodeInfoClicked);
nodes.append("text")
.attr("class", "iText")
.attr("y", 26.5)
.attr("x", nodeWidth - 20 - (5 / 2))
.html("i");
// Filter nodes with no parent to add parents expand/collapse button
let rootsNodes = nodes.filter(function(node){
return !nodeParentsStateMap[node.data.id];
})
rootsNodes.append("circle")
.attr("cx", nodeWidth/2)
.attr("cy", 0)
.attr("r", 12)
.on("mouseover", function () { d3.select(this).attr("r", 15); })
.on("mouseout", function () { d3.select(this).attr("r", 12); })
.on("click", onNodeParentsToggle);
rootsNodes.append("text")
.attr("class", "iText")
.attr("x", nodeWidth/2 - 7)
.attr("y",8.5)
.html("+")
// Filter nodes with no children to add expand/collapse button
let leavesNodes = nodes.filter(function(node){
return !leavesNodesIds.includes(node.data.id);
})
leavesNodes.append("rect")
.attr("width", 50)
.attr("height", 20)
.attr("x", nodeWidth/2- 25)
.attr("y", nodeHeight - 10)
.attr("fill",function (d) {
switch (nodeChildrenStateMap[d.data.id]) {
case 1:
return "darkred";
default:
return "darkblue";
}
})
.attr("stroke-width","0")
leavesNodes
.append("rect")
.attr("width", 25)
.attr("height", 20)
.attr("x", nodeWidth/2- 25)
.attr("y", nodeHeight - 10)
.attr("class", "node-expand-button") // Add a class for styling if needed
.attr("fill",function (d) {
switch (nodeChildrenStateMap[d.data.id]) {
case 0:
case 2:
return "darkgreen";
default:
return "grey";
}
})
.on("click", function(event,d){
if((nodeChildrenStateMap[d.data.id]) === 0 || nodeChildrenStateMap[d.data.id] === 2)
{
onNodeChildrenToggle(d,"expand");
}
});
leavesNodes
.append("rect")
.attr("width", 25)
.attr("height", 20)
.attr("x", nodeWidth/2)
.attr("y", nodeHeight - 10)
.attr("class", "node-collapse-button") // Add a class for styling if needed
.attr("fill",function (d) {
switch (nodeChildrenStateMap[d.data.id]) {
case 1:
case 2:
return "darkred";
default:
return "grey";
}
})
.on("click", function(event,d){
if( (nodeChildrenStateMap[d.data.id]))
{
onNodeChildrenToggle(d,"collapse");
}
});
leavesNodes.append("text")
.attr("class", "iText")
.attr("x",function (d) {
return nodeWidth/2 - 20;
})
.attr("y",function (d) {
return nodeHeight + 8.5;
})
.html(function (d) {
return "+";
})
leavesNodes.append("text")
.attr("class", "iText")
.attr("x",function (d) {
return nodeWidth/2 + 8;
})
.attr("y",function (d) {
return nodeHeight + 6.5;
})
.html(function (d) {
return "-";
})
}
/**
* Search for nodes children and add them to the currentTree
* @param {String} currentNodeId clicked node ID
* @param {Array} data tree data
*/
function nodeChildrenExpand(currentNodeId,data)
{
let nodeChildren = [];
for(let i = 0;i<data.length;i++)
{
if(data[i]["parentIds"].includes(currentNodeId))
{
// If the child is already shown, add the parent to it in currentTree
if(shownNodesMap[data[i]["id"]] === 1)
{
for(let j = 0;j<currentTree.length;j++)
{
if(currentTree[j]["id"] === data[i]["id"])
{
if(!currentTree[j]["parentIds"].includes(currentNodeId))
{
currentTree[j]["parentIds"].push(currentNodeId);
}
break;
}
}
}
else{
shownNodesMap[data[i]["id"]] = 1;
currentTree.push(data[i]);
}
nodeChildren.push(data[i]["id"]);
}
}
// Link new nodes (clicked node children) to their existing children
linkNewNodes(nodeChildren,data)
}
/**
* Search for nodes children and remove them from currentTree
* If there is a child has children, check if it should be removed, or it has other parents in the currentTree
* @param {String} currentNodeId clicked node ID
*/
function nodeChildrenCollapse(currentNodeId)
{
let childrenQueue = [];
childrenQueue.push(currentNodeId);
while(childrenQueue.length !== 0)
{
for(let i = 0;i<currentTree.length;i++)
{
for(let j = 0;j<currentTree[i]["parentIds"].length;j++)
{
if(currentTree[i]["parentIds"][j] === currentNodeId)
{
currentTree[i]["parentIds"].splice(j, 1);
if(currentTree[i]["parentIds"].length === 0)
{
childrenQueue.push(currentTree[i]["id"]);
shownNodesMap[currentTree[i]["id"]] = 0;
}
break;
}
}
}
childrenQueue.shift();
if(childrenQueue.length !== 0)
{
currentNodeId = childrenQueue[0];
}
}
let itr = 0;
while(itr<currentTree.length)
{
if(shownNodesMap[currentTree[itr]["id"]] === 0)
{
nodeChildrenStateMap[currentTree[itr]["id"]] = 0;
currentTree.splice(itr, 1);
}
else{
itr++;
}
}
}
/**
* Remove node parents not included in the currentTree
* @param {Array} parents node parents
*/
function removeHiddenParents(parents)
{
let j = 0;
while(j<parents.length)
{
if(shownNodesMap[parents[j]] === 0)
{
parents.splice(j, 1);
}
else
{
j++;
}
}
}
/**
* Update expand/collapse state of node
* @param {Array} data tree data
*/
function updateShownNodeMap(data)
{
for (let i = 0; i < currentTree.length; i++) {
if (getNodeChildren(currentTree[i]["id"], data).length === getNodeChildren(currentTree[i]["id"], currentTree).length) {
nodeChildrenStateMap[currentTree[i]["id"]] = 1;
} else if (getNodeChildren(currentTree[i]["id"], currentTree).length > 0 ){
nodeChildrenStateMap[currentTree[i]["id"]] = 2;
}
else{
nodeChildrenStateMap[currentTree[i]["id"]] = 0;
}
if (getNodeParents(currentTree[i]["id"], data).length === getNodeParents(currentTree[i]["id"], currentTree).length) {
nodeParentsStateMap[currentTree[i]["id"]] = 1;
} else {
nodeParentsStateMap[currentTree[i]["id"]] = 0;
}
}
}
/**
* Expand node found by search bar
* It links the node to its existing parents and expand its children
* @param {Object} node node
*/
function expandSearchedNodeTree(node)
{
let data = structuredClone(treeData);// A clone is made to avoid any modifications in the original data
if(shownNodesMap[node.id] !==1)
{
shownNodesMap[node.id] = 1;
currentTree.push(node);
}
let nodeParentsQueue = node.parentIds.slice(0);
// show node parents till the top layer
while(nodeParentsQueue.length > 0)
{
let parentId = nodeParentsQueue.shift();
let parent = getNodeById(parentId);
if(shownNodesMap[parentId] !==1)
{
shownNodesMap[parentId] = 1;
currentTree.push(parent);
}
parent.parentIds.forEach(function(elem){
nodeParentsQueue.push(elem);
})
}
// show node children
let children = getNodeChildren(node.id,data);
children.forEach(function(child){
if(shownNodesMap[child.id] !==1)
{
shownNodesMap[child.id] = 1;
currentTree.push(getNodeById(child.id));
}
})
linkNewNodes(children,data);
updateTree(node.id,"expand searched node tree")
}
/**
* Search for nodes parents and add them to the currentTree
* @param {String} currentNodeId clicked node ID
* @param {Array} data tree data
*/
function nodeParentsExpand(currentNodeId,data)
{
let node = getNodeById(currentNodeId);
let nodeParents = node.parentIds;
nodeParents.forEach(function(parentId){
if(shownNodesMap[parentId] === 0)
{
currentTree.push(getNodeById(parentId));
shownNodesMap[parentId] = 1;
linkNewNodes([parentId],data);
}
else // if the parent is already shown, add parent id to the current node only
{
for(let k=0;k<currentTree.length;k++)
{
if(currentNodeId === currentTree[k]["id"] && !currentTree[k]["parentIds"].includes(parentId))
{
currentTree[k]["parentIds"].push(parentId);
break;
}
}
}
})
}
/**
* Link new nodes to the current shown nodes (new parents to their existing children or new children to their existing parents)
* @param {Array} nodes new nodes
* @param {Array} data tree data
*/
function linkNewNodes(nodes,data)
{
for(let i = 0;i<nodes.length;i++)
{
let nodeGrandChildren = getNodeChildren(nodes[i],data);
for(let j = 0;j<nodeGrandChildren.length;j++)
{
if(shownNodesMap[nodeGrandChildren[j]["id"]] === 1)
{
for(let k=0;k<currentTree.length;k++)
{
if(nodeGrandChildren[j]["id"] === currentTree[k]["id"] && !currentTree[k]["parentIds"].includes(nodes[i]))
{
currentTree[k]["parentIds"].push(nodes[i]);
}
}
}
}
}
}
/**
* update tree graph with nodes transition effect
* @param {Array} drawData tree data
* @param {String} currentNodeId tree data
* @param {String} state node state
*/
function updateTreeGraph(drawData,currentNodeId,state)
{
let currentNode = nodes.filter(function(node){
return node.data.id === currentNodeId;
})
let currentX = 0,currentY = 0;
let currentZoomX = zoomTransform.x;
let currentZoomY = zoomTransform.y;
if(currentNode._groups[0].length !== 0 ){
currentX = currentNode._groups[0][0].__data__.x
currentY = currentNode._groups[0][0].__data__.y
}
graph.select(".paths-list").selectAll("*").remove();
generateTreeLayout(drawData);
layout(dag);
// Remove collapsed nodes before starting the animation
let collapsedNodes = nodes.filter(function(node){
return shownNodesMap[node.data.id] === 0;
})
collapsedNodes.remove();
nodes = graph
.select(".nodes-list")
.selectAll(".node")
.data(dag.descendants(), d => d.data.id)
let searchedNodeX = 0,searchedNodeY = 0;
if (state === "expand searched node tree")
{
let new_nodes = dag.descendants();
for(let i = 0; i < new_nodes.length; ++i)
{
if(new_nodes[i].data.id === currentNodeId)
{
searchedNodeX = new_nodes[i].x;
searchedNodeY = new_nodes[i].y;
}
}
}
let completeTransitions = 0;
nodes
.transition()
.duration(1000)
.ease(d3.easeLinear)
.attrTween("transform", function (node) {
// Your translation function
let translateFunction = d3.interpolateString(this.getAttribute("transform"), `translate(${node.x}, ${node.y})`);
// Return the tween function
return function (t) {
// Dynamically adjust the viewBox during the transition
if(state === "expand searched node tree")
{
zoomTransform.x = currentZoomX + (viewboxWidth/2 - currentZoomX - searchedNodeX*zoomTransform.k)*t;
zoomTransform.y = currentZoomY + (viewboxHeight/2 - currentZoomY - searchedNodeY*zoomTransform.k)*t;
}
else if(node.data.id === currentNodeId)
{
zoomTransform.x = currentZoomX - (node.x - currentX)*t*zoomTransform.k;
zoomTransform.y = currentZoomY - (node.y - currentY)*t*zoomTransform.k;
}
graph.attr('transform', zoomTransform);
// Return the interpolated transform value
return translateFunction(t);
};
})
.on("end", function(){
completeTransitions++;
if(completeTransitions===nodes.size())
{
drawTree(drawData,"update");
graph.attr('transform', zoomTransform);
updateGraphPlot(currentNodeId);
}
});
}
/**
* highlight selected node
* @param {String} currentNodeId node ID
*/
function highlightSelectedNode(currentNodeId)
{
let node = nodes.filter(function(node){
return node.data.id === currentNodeId;
})
node.select("rect").style("fill","rgba(255,255,0,0.5)");
}
/**
* generate tree layout
* @param {Array} treeData tree data
*/
function generateTreeLayout(treeData)
{
dag = d3.dagStratify()(treeData);
layout = d3
.sugiyama() // base layout
.decross(d3.decrossTwoLayer().order(d3.twolayerAgg())) // minimize number of crossings
.nodeSize((node) => [(node ? 3.6 : 0.25) * nodeWidth, 2 * nodeWidth]); // set node size instead of constraining to fit
}
/**
* modify top layer nodes positions to keep them at the top
*/
function keepTopLayersNodesUp()
{
let rootsNodesY = 100000;
let rootsNodesX = [];
// get roots nodes y-coordinate(the minimum to make it constant for all of them) and x-coordinates
dag.descendants().forEach(function(node){
if(node.y<rootsNodesY)
{
rootsNodesY = node.y;
}
if(rootsNodesIds.includes(node.data.id))
{
rootsNodesX.push(node.x);
}
})
rootsNodesX.sort(function(a, b){return a - b});
let xStep = (rootsNodesX[rootsNodesIds.length - 1] - rootsNodesX[0])/rootsNodesIds.length;//horizontal space between roots nodes
let i = 0;
// Set the new coordinates to the roots nodes
dag.descendants().forEach(function(node){
if(rootsNodesIds.includes(node.data.id))
{
node.y = rootsNodesY;
node.x = rootsNodesX[0] + i*xStep;
rootsNodesCoord[node.data.id] = [node.x,node.y]
i+=1;
}
})
// Set the new coordinates to the roots nodes links
dag.links().forEach(function(node){
if(rootsNodesIds.includes(node.source.data.id))
{
node.points[0].x = rootsNodesCoord[node.source.data.id][0];
node.points[0].y = rootsNodesCoord[node.source.data.id][1];
}
})
}
function expandTree()
{
currentTree = structuredClone(treeData);
for(let i = 0;i<currentTree.length;++i)
{
shownNodesMap[currentTree[i]["id"]] = 1;
}
updateShownNodeMap(currentTree);
if(zoomTransform !== undefined)
{
zoomTransform.k = 1;
zoomTransform.x = 0;
zoomTransform.y = 0;
}
drawTree(currentTree,"expand tree");
graph.attr('transform', zoomTransform);
}
function collapseTree()
{
currentTree = [];
shownNodesMap = {};
leavesNodesIds = [];
rootsNodesIds = [];
rootsNodesCoord = {};
initGraph();
}