-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_var_0.html
More file actions
211 lines (171 loc) · 5.46 KB
/
tree_var_0.html
File metadata and controls
211 lines (171 loc) · 5.46 KB
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
<!-- D3 collapsible tree in version 5
MIT licence
The code is based on d3noob's Collapsible tree diagram in v4
https://gist.github.com/d3noob/43a860bc0024792f8803bba8ca0d5ecd
-->
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
.node{
cursor: pointer;
}
.node circle{
fill: #ea5e51;
stroke: steelblue;
strocke-width: 3px
}
.node text{
font: 12px sans-serif;
}
.link{
fill: none;
stroke: #35838d;
}
</style>
</head>
<body>
<script src="https://d3js.org/d3.v5.js"></script>
<script>
// Tree diagram
var margin ={top:20, left:120, right:120, bottom:20},
w = 2480 - margin.left - margin.right,
h = 3508 - margin.top - margin. bottom,
max_lw= 20;
var svg = d3.select("body").append("svg")
.attr("width", w + margin.right + margin.left)
.attr("height", h + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
var i = 0,
duration = 750,
root;
var treeload = d3.tree().size([h,w]);
d3.json("example2.json").then(function(treeData) {
root = d3.hierarchy(treeData, function (d){ return d.children})
root.y0=h/2
root.x0=50
max_leaves=root.leaves().length
function collapse(d){
if (d.children){
d._children = d.children;
d._children.forEach(collapse);
d.children=null;
}
};
// root.children.forEach(collapse);
update(root);
function update(source){
//Assigns x and y positions for the nodes0
var treeData = treeload(root)
//create a new tree layout
var nodes = treeData.descendants(),
links = treeData.descendants().slice(1);
//Normalize node depth, by default its is zero for the root node and increases by 1 for each descendant generation
nodes.forEach(function(d){ d.y = d.depth * 50 });
//******NODES******
//Update nodes
var node = svg.selectAll('g.node')
.data(nodes, function(d){return d.id || (d.id = ++i);});
//Enter new nodes
var nodeEnter = node.enter().append('g')
.attr("class", "node")
.attr("transform", function(d){
return "translate(" + source.y0 + "," + source.x0 + ")";})
.on("click", click);
//Add node shape color the node different if it contains children or not
nodeEnter.append("circle")
.attr("class", "node")
.attr("r", 1e-6)
.style("fill", function (d){
return d._children ? "#94c8d8" : "#ea5e51";});
//Add node labels, text of internal nodes is shown to the left of the circle and to thw right for terminal nodes
nodeEnter.append("text")
.attr("dy", ".35em")
.attr("x", function(d){
return d.children || d._children ? -5 : 5})
.attr("text-anchor", function(d){
return d.children || d._children ? "end" : "start"})
.text(function (d){return d.data.name;})
//Update nodes
var nodeUpdate = nodeEnter.merge(node);
//Transition to the node position
nodeUpdate.transition()
.duration(duration)
.attr("transform", function(d){
return "translate(" + d.y + "," + d.x + ")";
});
//Update node attributes and style
nodeUpdate.select("circle.node")
.attr("r", 3)
.style("fill", function(d){
return d._children ? "#94c8d8" : "#ea5e51"})
.attr("cursor", "pointer");
//Remove any collapsed nodes
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d){
return "translate(" + source.y + "," + source.x + ")";
})
.remove();
nodeExit.select("circle")
.attr("r", 1e-6)
nodeExit.select("text")
.style("fill-opacity", 1e-6)
//********LINKS********
//Update links
var link = svg.selectAll("path.link")
.data(links, function(d){return d.id;});
var linkEnter = link.enter().insert("path", "g")
.attr("class", "link")
.attr("stroke-width",
function(d){return Math.log(d.descendants().length) > 10 ? "10px" : Math.log(d.descendants().length) + "1px" })
.attr("d", function(d){
var origin = {x: source.x0, y: source.y0};
return edges(origin, origin);
});
//UPDATE
var linkUpdate = linkEnter.merge(link);
//Transition to the parent position
linkUpdate.transition()
.duration(duration)
.attr("d", function(d){
return edges(d, d.parent)
});
//Remove exiting links
var linkExit = link.exit().transition()
.duration(duration)
.attr('d', function(d){
var origin = {x: source.x, y: source.y};
return edges(origin, origin);
})
.remove();
//Store the old position for the transition
nodes.forEach(function(d){
d.x0 = d.x;
d.y0 = d.y;
});
//Curve diagonal path from parent to child nodes
function edges(o,t){
path = `M ${o.y} ${o.x}
C ${(o.y + t.y)/2} ${o.x},
${(o.y + t.y)/2} ${t.x},
${t.y} ${t.x}`
return path
};
//Toggle children on click
function click (d){
if (d.children){
d._children = d.children;
d.children = null;
} else{
d.children = d._children;
d._children = null;
}
update(d);
}}})
;
</script>
</body>
</html>