forked from grassator/aa-tree-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualizer.js
135 lines (119 loc) · 3.91 KB
/
visualizer.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
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
function TreeVisualizer(svgContainerSelector) {
this.canvas = d3.select('svg');
this.root = this.canvas.append('g')
.attr('class', 'root');
this.nodes = [];
}
TreeVisualizer.prototype.update = function(rootNode) {
var treeLayout = d3.layout.tree()
.nodeSize([20, 30])
.separation(function(a, b) {
return (a.parent == b.parent ? 5 : 10) / a.depth;
})
.children(function(d) {
var result = [];
if(d.left !== TreeNode.nil) result.push(d.left);
if(d.right !== TreeNode.nil) result.push(d.right);
return result;
});
this.root
.attr('transform', 'translate(' + (window.outerWidth / 2) + ', 80)');
// Stash the old positions for transition.
this.nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
this.nodes = treeLayout.nodes(rootNode);
var links = treeLayout.links(this.nodes);
var diagonal = d3.svg.diagonal();
var pathSelection = this.root.selectAll("path.link")
.data(links, function(d) {
if(d.source.depth > d.target.depth) {
return d.source.key + ',' + d.target.key;
} else {
return d.target.key + ',' + d.source.key;
}
});
pathSelection.enter()
.append("path")
.attr("class", "link")
.attr("d", diagonal);
pathSelection.exit()
.attr('opacity', 1)
.attr('class', 'link deleting')
.transition()
.duration(1000)
.attr('transform', 'translate(50, -50)')
.attr('opacity', 0)
.remove();
pathSelection
.attr("d", function(d) {
var source = {
x: d.source.x0 === undefined ? d.source.x : d.source.x0,
y: d.source.y0 === undefined ? d.source.y : d.source.y0,
};
var target = {
x: d.target.x0 === undefined ? d.target.x : d.target.x0,
y: d.target.y0 === undefined ? d.target.y : d.target.y0,
};
return diagonal({ source: source, target: target });
})
.transition()
.duration(1000)
.attr("d", diagonal);
nodeSelection = this.root.selectAll('.node')
.data(this.nodes, function(d) {
return d.key;
});
nodeSelection
.moveToFront()
.transition()
.duration(1000)
.attr('transform', function(d) {
return "translate(" + d.x + "," + d.y + ")";
});
var nodeEnter = nodeSelection.enter().append('g');
nodeSelection.exit()
.attr('opacity', 1)
.transition()
.duration(1000)
.attr('transform', 'translate(50, -50)')
.attr('opacity', 0)
.remove();
nodeEnter
.attr('transform', function(d) {
return "translate(" + d.x + "," + d.y + ")";
})
.attr('opacity', function(d) { return d.key === undefined ? 0.5 : 1})
.attr('class', 'node');
nodeEnter.append('foreignObject')
.attr('x', -10)
.attr('y', -10)
.attr('width', 20)
.attr('height', 20)
.append('xhtml:div')
.attr('class', 'node-html')
.attr('title', function(d) {
return d.value === undefined ? '' : d.value;
})
.text(function(d) {
return d.key === undefined ? 'nil' : d.key;
});
// nodeEnter
// .append('circle')
// .attr('r', 5)
// .attr('cy', 5)
// .attr('class', 'node-dot')
// nodeEnter.append('text')
// .attr("dx", 5)
// .attr("dy", 3)
// .style("text-anchor", "start")
// .text(function(d) {
// return d.key === undefined ? 'nil' : d.key;
// });
}