-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollapsible.php
More file actions
228 lines (192 loc) · 5.64 KB
/
collapsible.php
File metadata and controls
228 lines (192 loc) · 5.64 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
<?php
// TODO get a node count from the database
$filename = "data/" . $_GET["tag"] . "-force.json";
$filesize = filesize($filename);
$size = 500 + 10 * $filesize / 1000;
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style type="text/css">
circle.node {
cursor: pointer;
stroke: #000;
stroke-width: .5px;
}
div#graph {
width: <?php print $size; ?>px;
}
line.link {
fill: none;
stroke: #9ecae1;
stroke-width: 1.5px;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="graphs.js"></script>
<script type="text/javascript">
$(document).ready(function () {
// scroll to where the graph will be created
setTimeout(centerViewport, 100);
disableContextMenu();
createControls("<?php print $_GET["tag"]; ?>", "collapsible");
$("div#controls").append("<ul>");
$("div#controls ul").append("<li><a href='javascript:void(0)' onclick='expand(root);update()'>expand all nodes</a><br>");
$("div#controls").append("</ul>");
depthLegend();
});
</script>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js"></script>
<link rel='stylesheet' type='text/css' href='style.css'>
<script type="text/javascript">
var width = <?php print $size; ?>,
height = <?php print $size; ?>,
node,
link,
root;
function distance(d) {
switch(d.target.nodeType) {
case "chapter":
return 150;
case "section":
return 10;
case "tag":
return 5;
}
}
var global;
var force = d3.layout.force()
.on("tick", tick)
.charge(-30)
.linkDistance(distance)
.size([width, height - 160]);
d3.select("body").append("div")
.attr("id", "graph")
.attr("width", width)
.attr("height", height);
var vis = d3.select("div#graph")
.append("svg")
.attr("width", width)
.attr("height", height);
function displaySectionInfo(node) {
displayTooltip(node, "Section " + node.book_id + ": " + node.tagName);
}
function displayChapterInfo(node) {
displayTooltip(node, "Chapter " + node.book_id + ": " + node.tagName);
}
d3.json("data/<?php print $_GET["tag"]; ?>-packed.json", function(json) {
root = json;
root.fixed = true;
root.x = width / 2;
root.y = height / 2 - 80;
update();
});
function update() {
var nodes = flatten(root),
links = d3.layout.tree().links(nodes);
// Restart the force layout.
force
.nodes(nodes)
.links(links)
.start();
// Update the links…
link = vis.selectAll("line.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links.
link.enter().insert("svg:line", ".node")
.attr("class", "link")
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
// Exit any old links.
link.exit().remove();
// Update the nodes…
node = vis.selectAll("circle.node")
.data(nodes, function(d) { return d.id; })
.style("fill", color)
node.transition()
.attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; });
function displayInfo(node) {
switch (node.nodeType) {
case "root":
case "tag":
return displayTagInfo(node);
case "section":
return displaySectionInfo(node);
case "chapter":
return displayChapterInfo(node);
}
}
// Enter any new nodes.
node.enter().append("svg:circle")
.attr("class", "node")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", function(d) { return d.children ? 4.5 : Math.sqrt(d.size) / 10; })
.style("fill", color)
.on("click", click)
.on("mouseover", displayInfo)
.on("mouseout", hideInfo)
.call(force.drag);
// Exit any old nodes.
node.exit().remove();
}
function tick() {
link.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
}
depthMap = {"root": "green", "chapter": "#3182bd", "section": "#c6dbef", "tag": "#fd8d3c"};
// Color leaf nodes orange, and packages white or blue.
function color(d) {
return depthMap[d.nodeType];
}
function expand(node) {
if (node._children)
click(node);
if ("children" in node) {
for (var i = 0; i < node.children.length; i++)
expand(node.children[i]);
}
}
// Toggle children on click.
function click(d) {
if (d.children) { // collapsing
d._children = d.children;
d.children = null;
} else { // expanding
d.children = d._children;
d._children = null;
}
update();
}
// Returns a list of all nodes under the root.
function flatten(root) {
var nodes = [], i = 0;
function recurse(node) {
if (node.children) node.size = node.children.reduce(function(p, v) { return p + recurse(v); }, 0);
if (!node.id) node.id = ++i;
nodes.push(node);
return node.size;
}
root.size = recurse(root);
return nodes;
}
function depthLegend(types) {
$("body").append("<div class='legend' id='legendDepth'></div>");
$("div#legendDepth").append("Legend");
$("div#legendDepth").append("<ul>");
for (type in depthMap) {
$("<li><svg height='10' width='10'><circle cx='5' cy='5' r='5' fill='" + depthMap[type] + "'/></svg>").append(" " + capitalize(type)).appendTo($("div#legendDepth ul"));
}
}
</script>
</body>
</html>