-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtree.js
146 lines (130 loc) · 3.69 KB
/
tree.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
var width = 960,
height = 800;
var topDog,
i = 0;
var cluster = d3.layout.cluster()
.size([height, width - 160]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(80,0)");
d3.json("data.json", function(error, root) {
topDog = root;
root.x0 = height / 2;
root.y0 = 0;
var collapse = function(d) {
if (d.children) {
d._children = d.children;
d._children.forEach(collapse);
d.children = null;
}
}
root.children.forEach(collapse);
update(root);
});
d3.select(self.frameElement).style("height", height + "px");
var update = function(source) {
var nodes = cluster.nodes(topDog).reverse(),
links = cluster.links(nodes);
var node = svg.selectAll(".node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
var link = svg.selectAll(".link")
.data(links, function(d) { return d.target.id; });
var linkEnter = link.enter().append("path")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
link.transition().duration(750)
.attr("d", diagonal);
link.exit().transition()
.duration(750)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Node enter
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.on("click", click);
var w = 160,
h = 50;
nodeEnter.append("rect")
.attr("width", 1e-6)
.attr("height", 1e-6)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
nodeEnter.append("text")
.attr("dy", -9)
.style("text-anchor", "middle")
.text(function(d) { return d.name; })
.style("fill-opacity", 1e-6);
nodeEnter.append("text")
.attr("dy", 9)
.style("text-anchor", "middle")
.text(function(d) { return d.position; })
.style("fill-opacity", 1e-6);
// Node update
var nodeUpdate = node.transition()
.duration(750)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeUpdate.selectAll("text").style("fill-opacity", 1);
nodeUpdate.selectAll("rect")
.attr("width", w)
.attr("height", h)
.attr("x", -w / 2)
.attr("y", -h / 2)
.style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
// Node exit
var nodeExit = node.exit().transition()
.duration(750)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.selectAll("text").style("fill-opacity", 1e-6);
nodeExit.selectAll("rect")
.attr("width", 1e-6)
.attr("height", 1e-6)
.attr("x", 0)
.attr("y", 0);
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
var click = function(d) {
switchRoot(d);
//toggleChildren(d);
update(d);
}
var switchRoot = function(d) {
if (d !== topDog && !d._children) {
return;
}
if (d === topDog) {
if (d.parent) {
topDog = d.parent;
d._children = d.children;
d.children = null;
}
return;
}
d.parent = topDog;
d.children = d._children;
d._children = null;
topDog = d;
}
var toggleChildren = function(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
}