forked from dssg/data-portal-treemap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
358 lines (296 loc) · 9.83 KB
/
index.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html>
<meta charset="utf-8">
<title>Zoomable Treemap of Chicago Data Portal</title>
<style>
#chart {
width: 820px;
height: 700px;
background: #bbb;
margin: 1px auto;
position: relative;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
text {
pointer-events: none;
}
.grandparent text { /* header text */
font-weight: bold;
font-size: medium;
font-family: "Open Sans", Helvetica, Arial, sans-serif;
}
rect {
fill: none;
stroke: #fff;
}
rect.parent,
.grandparent rect {
stroke-width: 2px;
}
.grandparent rect {
fill: #fff;
}
.children rect.parent,
.grandparent rect {
cursor: pointer;
}
rect.parent {
pointer-events: all;
}
.children:hover rect.child,
.grandparent:hover rect {
fill: #aaa;
}
.textdiv { /* text in the boxes */
font-size: x-small;
padding: 5px;
font-family: "Open Sans", Helvetica, Arial, sans-serif;
}
</style>
<p id="chart">
<script src="http://d3js.org/d3.v2.js"></script>
<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script>
/*
* If running inside bl.ocks.org we want to resize the iframe to fit both graphs
* This bit of code was shared originally at https://gist.github.com/benjchristensen/2657838
*/
if(parent.document.getElementsByTagName("iframe")[0]) {
parent.document.getElementsByTagName("iframe")[0].setAttribute('style', 'height: 700px !important');
}
var margin = {top: 20, right: 0, bottom: 0, left: 0},
width = 820,
height = 700 - margin.top - margin.bottom,
formatNumber = d3.format(",d"),
transitioning;
/* create x and y scales */
var x = d3.scale.linear()
.domain([0, width])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, height])
.range([0, height]);
var treemap = d3.layout.treemap()
.children(function(d, depth) { return depth ? null : d.children; })
.sort(function(a, b) { return a.value - b.value; })
.ratio(height / width * 0.5 * (1 + Math.sqrt(5)))
.round(false);
/* create svg */
var svg = d3.select("#chart").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.bottom + margin.top)
.style("margin-left", -margin.left + "px")
.style("margin.right", -margin.right + "px")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.style("shape-rendering", "crispEdges");
var color = d3.scale.category20c();
var grandparent = svg.append("g")
.attr("class", "grandparent");
grandparent.append("rect")
.attr("y", -margin.top)
.attr("width", width)
.attr("height", margin.top);
grandparent.append("text")
.attr("x", 6)
.attr("y", 6 - margin.top)
.attr("dy", ".75em");
/* load in data, display root */
d3.json("portaldata.json", function(root) {
var replaceAll = function(src, oldr, newr){
while(src.indexOf(oldr) > -1){
src = src.replace(oldr, newr);
}
return src;
};
var getURLvar = function(nm){nm=nm.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");var rxS="[\\?&]"+nm+"=([^&#]*)";var rx=new RegExp(rxS);var rs=rx.exec(window.location.href);if(!rs){return null;}else{return rs[1];}};
var moveSquare = getURLvar("sq");
var chooseroot = root;
if(moveSquare){
var findMatch = function(node, chain, searchname){
if(typeof node.name != "undefined" && replaceAll(replaceAll(chain.join(".") + "." + name(node)," ",""),"&","") == searchname){
return node;
}
if(typeof node.children != "undefined"){
chain.push( name(node) );
for(var c=0;c<node.children.length;c++){
var makeMatch = findMatch( node.children[c], chain, searchname, node );
if(makeMatch){
node.children[c].parent = node;
return makeMatch;
}
}
chain.pop();
}
};
var wantroot = findMatch(root, [], moveSquare);
if(wantroot){
chooseroot = wantroot;
}
}
initialize(chooseroot);
accumulate(chooseroot);
layout(chooseroot);
display(chooseroot);
initialize(root);
accumulate(root);
layout(root);
//display(root);
function initialize(root) {
root.x = root.y = 0;
root.dx = width;
root.dy = height;
root.depth = 0;
}
// Aggregate the values for internal nodes. This is normally done by the
// treemap layout, but not here because of the custom implementation.
function accumulate(d) {
return d.children
? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0)
: d.value;
}
// Compute the treemap layout recursively such that each group of siblings
// uses the same size (1×1) rather than the dimensions of the parent cell.
// This optimizes the layout for the current zoom state. Note that a wrapper
// object is created for the parent node for each group of siblings so that
// the parent’s dimensions are not discarded as we recurse. Since each group
// of sibling was laid out in 1×1, we must rescale to fit using absolute
// coordinates. This lets us use a viewport to zoom.
function layout(d) {
if (d.children) {
treemap.nodes({children: d.children});
d.children.forEach(function(c) {
c.x = d.x + c.x * d.dx;
c.y = d.y + c.y * d.dy;
c.dx *= d.dx;
c.dy *= d.dy;
c.parent = d;
layout(c);
});
}
}
/* display shows the treemap and writes the embedded transition function */
function display(d) {
/* create grandparent bar at top */
grandparent
.datum(d.parent)
.on("click", transition)
.select("text")
.text(name(d));
if(typeof history != "undefined" && typeof history.pushState != "undefined"){
// add history to URL
history.pushState(null, null, "?sq=" + replaceAll(replaceAll(name(d)," ",""),"&","") );
}
window.onpopstate = function(e) {
// hit back button -> go back to parent
if(d != root){
transition( d.parent );
}
};
var g1 = svg.insert("g", ".grandparent")
.datum(d)
.attr("class", "depth");
/* add in data */
var g = g1.selectAll("g")
.data(d.children)
.enter().append("g");
/* transition on child click */
g.filter(function(d) { return d.children; })
.classed("children", true)
.on("click", transition);
/* write children rectangles */
g.selectAll(".child")
.data(function(d) { return d.children || [d]; })
.enter().append("rect")
.attr("class", "child")
.call(rect)
.append("title")
.text(function(d) { return d.name + " " + formatNumber(d.size); });
/* write parent rectangle */
g.append("rect")
.attr("class", "parent")
.call(rect)
/* open new window based on the json's URL value for leaf nodes */
/* Chrome displays this on top */
.on("click", function(d) {
if(!d.children){
window.open(d.url);
}
})
.append("title")
.text(function(d) { return d.name + " " + formatNumber(d.size); }); /*should be d.value*/
/* Adding a foreign object instead of a text object, allows for text wrapping */
g.append("foreignObject")
.call(rect)
/* open new window based on the json's URL value for leaf nodes */
/* Firefox displays this on top */
.on("click", function(d) {
if(!d.children){
window.open(d.url);
}
})
.attr("class","foreignobj")
.append("xhtml:div")
.attr("dy", ".75em")
.html(function(d) { return d.name;
})
.attr("class","textdiv"); //textdiv class allows us to style the text easily with CSS
/* create transition function for transitions */
function transition(d) {
if (transitioning || !d) return;
transitioning = true;
var g2 = display(d),
t1 = g1.transition().duration(750),
t2 = g2.transition().duration(750);
// Update the domain only after entering new elements.
x.domain([d.x, d.x + d.dx]);
y.domain([d.y, d.y + d.dy]);
// Enable anti-aliasing during the transition.
svg.style("shape-rendering", null);
// Draw child nodes on top of parent nodes.
svg.selectAll(".depth").sort(function(a, b) { return a.depth - b.depth; });
// Fade-in entering text.
g2.selectAll("text").style("fill-opacity", 0);
g2.selectAll("foreignObject div").style("display", "none"); /*added*/
// Transition to the new view.
t1.selectAll("text").call(text).style("fill-opacity", 0);
t2.selectAll("text").call(text).style("fill-opacity", 1);
t1.selectAll("rect").call(rect);
t2.selectAll("rect").call(rect);
t1.selectAll(".textdiv").style("display", "none"); /* added */
t1.selectAll(".foreignobj").call(foreign); /* added */
t2.selectAll(".textdiv").style("display", "block"); /* added */
t2.selectAll(".foreignobj").call(foreign); /* added */
// Remove the old node when the transition is finished.
t1.remove().each("end", function() {
svg.style("shape-rendering", "crispEdges");
transitioning = false;
});
}//endfunc transition
return g;
}//endfunc display
function text(text) {
text.attr("x", function(d) { return x(d.x) + 6; })
.attr("y", function(d) { return y(d.y) + 6; });
}
function rect(rect) {
rect.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) { return y(d.y); })
.attr("width", function(d) { return x(d.x + d.dx) - x(d.x); })
.attr("height", function(d) { return y(d.y + d.dy) - y(d.y); })
.style("background", function(d) { return d.parent ? color(d.name) : null; });
}
function foreign(foreign){ /* added */
foreign.attr("x", function(d) { return x(d.x); })
.attr("y", function(d) { return y(d.y); })
.attr("width", function(d) { return x(d.x + d.dx) - x(d.x); })
.attr("height", function(d) { return y(d.y + d.dy) - y(d.y); });
}
function name(d) {
return d.parent
? name(d.parent) + "." + d.name
: d.name;
}
});
</script>