-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawTree.js
67 lines (51 loc) · 1.9 KB
/
drawTree.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
// ************** Generate the tree diagram *****************
function drawTree(treeData, treeDepth, divId) {
var margin = {top: 40, right: 40, bottom: 20, left: 40},
width = 1200;
height = 100*treeDepth;
var tree = d3.layout.tree()
.size([1200, 100*treeDepth]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.x, d.y]; });
$(divId).empty();
var svg = d3.select(divId).append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
update(root, tree, svg, diagonal);
}
function update(source, tree, svg, diagonal) {
var i = 0;
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 100; });
// Declare the nodes…
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
// Enter the nodes.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) {
return "translate(" + d.x + "," + d.y + ")"; });
nodeEnter.append("circle")
.attr("r", 10)
.style("fill", "#fff");
nodeEnter.append("text")
.attr("y", function(d) {
return d.children || d._children ? -18 : 18; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.name + (d.value != undefined ? "|" + d.value : "") + (d.uniqueId != undefined ? "|" + d.uniqueId : ""); })
.style("fill-opacity", 1);
// Declare the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter the links.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", diagonal);
}