-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawTree.html
183 lines (138 loc) · 4.47 KB
/
drawTree.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Collapsible Tree Example</title>
<style>
.node circle {
fill: #fff;
stroke: steelblue;
stroke-width: 3px;
}
.node text { font: 12px sans-serif; }
.link {
fill: none;
stroke: #ccc;
stroke-width: 2px;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<div id="control_row">
Tree data:
<input id="treeDataText" type=text size=100>
<input id="drawTreeButton" type=button value="drawTree">
</div>
<!-- load the d3.js library -->
<script>
var data = [];
// create the tree array
var treeData = [];
var nodecnt = 0;
// *********** Convert flat data into a nice tree ***************
// create a name: node map
function traverseInorder(root, parentval) {
if (root) {
var pval = root.val+"_"+nodecnt;
nodecnt++;
traverseInorder(root.left, pval)
//console.log(root.val)
data.push({"name":pval, "parent":parentval})
traverseInorder(root.right, pval)
}
}
function createTreeData(root) {
traverseInorder(root, null);
var dataMap = data.reduce(function(map, node) {
map[node.name] = node;
return map;
}, {});
data.forEach(function(node) {
// add to parent
var parent = dataMap[node.parent];
if (parent) {
// create child array if it doesn't exist
(parent.children || (parent.children = []))
// add node to child array
.push(node);
} else {
// parent is null or missing
treeData.push(node);
}
});
}
// ************** Generate the tree diagram *****************
var margin = {top: 50, right: 20, bottom: 20, left: 20},
width = 2000 - margin.right - margin.left,
height = 1000 - margin.top - margin.bottom;
var i = 0;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.x, d.y]; });
var svg = d3.select("body").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);
function update(root) {
// 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 * 75; });
// 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 ? -15 : 15; })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) { return d.name.split("_")[0]; })
.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);
}
// 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);
}
var drawbutton = document.getElementById("drawTreeButton");
drawbutton.addEventListener("click",function(e){
var tdata = document.getElementById("treeDataText").value
var root = createTree(tdata);
console.log("Creating the Tree data for d3")
data = [];
treeData = [];
createTreeData(root);
troot = treeData[0];
update(troot);
},false);
</script>
<script type="text/javascript" src="tree.js"></script>
</body>
</html>