-
Notifications
You must be signed in to change notification settings - Fork 30
/
app3.js
280 lines (252 loc) · 6.74 KB
/
app3.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
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
function viewTree(el_id, divHeight, fileName) {
/**
* Interactive, zoomable treemap, using D3 v4
* http://bl.ocks.org/guglielmo/16d880a6615da7f502116220cb551498
*
* A port to D3 v4 of Jacques Jahnichen's Block, using the same budget data
* see: http://bl.ocks.org/JacquesJahnichen/42afd0cde7cbf72ecb81
*
* Author: Guglielmo Celata
* Date: sept 1st 2017
**/
var obj = document.getElementById(el_id);
var divWidth = obj.offsetWidth;
var margin = {top: 30, right: 0, bottom: 20, left: 0},
width = divWidth -25,
height = divHeight - margin.top - margin.bottom,
transitioning,
formatSize = d3.format(".2s"),
formatP = d3.format(".2p"),
// totalSize is initialized when the data is loaded, below.
totalSize = 1;
function formatNumber(x) {
// Display size both as absolute value and percentage.
return formatSize(x) + " / " + formatP(x/totalSize);
}
// sets x and y scale to determine size of visible boxes
var x = d3.scaleLinear()
.domain([0, width])
.range([0, width]);
var y = d3.scaleLinear()
.domain([0, height])
.range([0, height]);
// Initialize the treemap visualization.
var treemap = d3.treemap()
.tile(d3.treemapSquarify)
.size([width, height])
.paddingInner(0)
.round(false);
// color is further initialized when the data is loaded.
var color;
// Create the SVG.
var svg = d3.select('#'+el_id).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 grandparent = svg.append("g")
.attr("class", "grandparent");
grandparent.append("rect")
.attr("y", -margin.top)
.attr("width", width)
.attr("height", margin.top)
.attr("fill", '#bbbbbb');
grandparent.append("text")
.attr("x", 6)
.attr("y", 6 - margin.top)
.attr("dy", ".75em");
console.log("init done");
function display(d) {
// write text into grandparent and activate click's handler.
grandparent
.datum(d.parent)
.on("click", transition)
.select("text")
.text(name(d));
var g1 = svg.insert("g", ".grandparent")
.datum(d)
.attr("class", "depth");
var g = g1.selectAll("g")
.data(d.children)
.enter().
append("g");
// add class and click handler to all g's with childre.n
g.filter(function (d) {
return d.children;
})
.classed("children", true)
.on("click", transition);
g.selectAll(".child")
.data(function (d) {
return d.children || [d];
})
.enter().append("rect")
.attr("class", "child")
.call(rect);
// add title to parents.
g.append("rect")
.attr("class", "parent")
.call(rect)
.append("title")
.text(function (d){
return d.data.name;
});
/* Adding a foreign object instead of a text object, allows
* for text wrapping */
g.append("foreignObject")
.call(rect)
.attr("class", "foreignobj")
.append("xhtml:div")
.attr("dy", ".75em")
.html(function (d) {
return '' +
'<p class="title"> ' + d.data.name + '</p>' +
'<p>' + formatNumber(d.value) + '</p>'
;
})
.attr("class", "textdiv");
function transition(d) {
if (transitioning || !d) return;
transitioning = true;
var g2 = display(d),
t1 = g1.transition().duration(650),
t2 = g2.transition().duration(650);
// Update the domain only after entering new elements.
x.domain([d.x0, d.x1]);
y.domain([d.y0, d.y1]);
// 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");
// 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);
/* Foreign object */
t1.selectAll(".textdiv").style("display", "none");
t1.selectAll(".foreignobj").call(foreign);
t2.selectAll(".textdiv").style("display", "block");
t2.selectAll(".foreignobj").call(foreign);
// Remove the old node when the transition is finished.
t1.on("end.remove", function(){
this.remove();
transitioning = false;
});
}
return g;
}
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.x0);
})
.attr("y", function (d) {
return y(d.y0);
})
.attr("width", function (d) {
return x(d.x1) - x(d.x0);
})
.attr("height", function (d) {
return y(d.y1) - y(d.y0);
})
.attr("fill", function (d) {
return color(breadcrumbs(d)); //'#bbbbbb';
});
}
function foreign(foreign) { /* added */
foreign
.attr("x", function (d) {
return x(d.x0);
})
.attr("y", function (d) {
return y(d.y0);
})
.attr("width", function (d) {
return x(d.x1) - x(d.x0);
})
.attr("height", function (d) {
return y(d.y1) - y(d.y0);
});
}
function name(d) {
return breadcrumbs(d) + " — (" + formatNumber(d.value) + ")" +
(d.parent
? " — Click to zoom out"
: " — Click inside square to zoom in");
}
function breadcrumbs(d) {
var res = "";
d.ancestors().reverse().forEach(function(i){
res += i.data.name;
});
return res;
}
console.log("start done");
var data;
function valFnSize(d) { return d.value }
function valFnCount(d) { return d.value ? 1 : 0 }
var valFn = valFnSize;
function doDisplay() {
var root = data
.sum(valFn)
.sort(function (a, b) {
return /* b.height - a.height || */ b.value - a.value
})
// Initialize the total size for percentages.
totalSize = root.value;
console.log("total size:", totalSize);
// Create the color spectrum.
var l = [];
root.eachBefore(function (d) {
l.push(breadcrumbs(d));
});
colors = []
for (var i in l) {
colors.push(d3.interpolateSpectral(i / (l.length-1)));
}
color = d3.scaleOrdinal().domain(l).range(colors);
// Display.
treemap(root);
display(root);
}
d3.json(fileName, function(fdata) {
console.log("load done");
// Interpret the dict as a hierarchy.
data = d3.hierarchy(fdata);
var p = d3.select('#'+el_id);
p.append("br");
p
.append("button")
.text("size")
.on("click", function(d,i) {
valFn = valFnSize;
doDisplay();
});
p
.append("button")
.text("count")
.on("click", function(d,i) {
valFn = valFnCount;
doDisplay();
});
doDisplay();
});
}