-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
250 lines (207 loc) · 5.66 KB
/
Copy pathindex.html
File metadata and controls
250 lines (207 loc) · 5.66 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!--
TODO - dynamic page length
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?1.27.2"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?1.27.2"></script>
<style type="text/css">
.node rect {
cursor: pointer;
fill: #fff;
fill-opacity: .5;
stroke: #3182bd;
stroke-width: 1.5px;
}
.node text {
font: 10px sans-serif;
pointer-events: none;
}
path.link {
fill: none;
stroke: #9ecae1;
stroke-width: 1.5px;
}
</style>
</head>
<body>
<input size=30 name="searchbox" type="text" onkeyup="searchTree(this.value);">
<div id="chart"></div>
<script type="text/javascript">
var w = 960,
h = 800,
i = 0,
barHeight = 20,
barWidth = w * .8,
duration = 400,
nodes,
root;
var tree = d3.layout.tree()
.size([h, 100]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var vis = d3.select("#chart").append("svg:svg")
.attr("width", w)
.attr("height", h)
.append("svg:g")
.attr("transform", "translate(20,30)");
d3.json("NAICS.json", function(json) {
root = json;
root.x0 = 0;
root.y0 = 0;
root.isMatch = true;
update(root);
//this closes all of the nodes, and sets the parent so we can find it again
root.children.forEach(closeAllNodes);
update(root);
});
function closeAllNodes(d) {
if (d.parent) {
d._parent = d.parent;
}
if (d.children) {
d.children.forEach(closeAllNodes);
hideChildren(d);
}
}
function searchAll(d, searchString) {
var stringLoc = d.name.toLowerCase().indexOf(searchString.toLowerCase());
//search visible and hidden children
if (d.children) {
d.children.forEach(function(d) {searchAll(d, searchString);})
}
if (d._children) {
d._children.forEach(function(d) {searchAll(d, searchString);})
}
if(stringLoc > -1){
d.isMatch = true;
openUpwards(d);
} else {
d.isMatch = false;
}
}
function openUpwards(d){
if(d._children){ //if you're not open, you should be open
d.children = d._children;
d._children = null;
}
if(d._parent){
openUpwards(d._parent);
}
}
function searchTree(searchString) {
//first close all nodes
closeAllNodes(root);
if (searchString.length > 2){
searchAll(root, searchString);
update(root);
}
}
function update(source) {
// Compute the flattened node list. TODO use d3.layout.hierarchy.
nodes = tree.nodes(root);
max_height = 0;
// Compute the "layout".
nodes.forEach(function(n, i) {
n.x = i * barHeight;
max_height = n.x;
});
$("svg:first").attr("height", max_height+100);
//d3.select("#chart").children(0).attr("height", max_height);
// Update the nodes…
var node = vis.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); });
var nodeEnter = node.enter().append("svg:g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
.style("opacity", 1e-6);
// Enter any new nodes at the parent's previous position.
nodeEnter.append("svg:rect")
.attr("y", -barHeight / 2)
.attr("height", barHeight)
.attr("width", barWidth)
.style("fill", color)
.on("click", click);
nodeEnter.append("svg:text")
.attr("dy", 3.5)
.attr("dx", 5.5)
.text(function(d) { return d.name; });
// Transition nodes to their new position.
nodeEnter.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
.style("opacity", 1);
node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; })
.style("opacity", 1)
.select("rect")
.style("fill", color);
// Transition exiting nodes to the parent's new position.
node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.style("opacity", 1e-6)
.remove();
// Update the links…
var link = vis.selectAll("path.link")
.data(tree.links(nodes), function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("svg:path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
})
.transition()
.duration(duration)
.attr("d", diagonal);
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// 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);
}
//same as above, but no update
function hideChildren(d) {
if (d.children) {
d._children = d.children;
d.children = null;
}
}
function color(d) {
if (d.isMatch) {
return "#FFFFA0";
} else {
return d._children ? "#3182bd" : d.children ? "#c6dbef" : "#c6dbef";
}
}
</script>
</body>
</html>