-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzoomable_sunburst_d3.html
457 lines (423 loc) · 14 KB
/
zoomable_sunburst_d3.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
<!DOCTYPE html>
<meta charset="utf-8">
<style>
path {
cursor: pointer;
stroke: #fff;
stroke-width: 1;
}
text {
cursor: pointer;
font-family: Arial, Helvetica, sans-serif;
}
body {
margin: 0 auto;
text-align: center;
}
h1 {
margin: 0.5em 0;
text-align: center;
}
p#intro {
margin: 1em 0;
text-align: center;
}
#vis {
border-radius: 50%;
}
</style>
<body>
<div id="vis"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>
<script>
window.onresize = update;
var json=[];
d3.json("graph.json", function(error, j) {
json = j;
draw(j);
});
function update() {
d3.select("svg").remove();
draw(json);
}
function draw(json){
var w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0];
var window_w = w.innerWidth || e.clientWidth || g.clientWidth;
window_h = w.innerHeight || e.clientHeight || g.clientHeight
//console.log(w.innerHeight);
if (Math.min(window_w,window_h) >= 400 && Math.min(window_w,window_h) < 450) {
fontSize = '5.5px';
}
if (Math.min(window_w,window_h) >= 450 && Math.min(window_w,window_h) < 500) {
fontSize = '6.5px';
}
if (Math.min(window_w,window_h) >= 500 && Math.min(window_w,window_h) < 560) {
fontSize = '8px';
}
if (Math.min(window_w,window_h) >= 560 && Math.min(window_w,window_h) < 600) {
fontSize = '9px';
}
if (Math.min(window_w,window_h) >= 600 && Math.min(window_w,window_h) < 700) {
fontSize = '10px';
}
if (Math.min(window_w,window_h) >= 700 && Math.min(window_w,window_h) < 750) {
fontSize = '11px';
}
if (Math.min(w.innerWidth,w.innerHeight) >= 750) {
fontSize = '12px';
}
if (window_h > window_w) {
var width = window_w/1.01,
height = width;
} else {
var height = window_h/1.01 ,
width = height;
}
var radius = width / 2,
x = d3.scale.linear().range([0, 2 * Math.PI]),
y = d3.scale.pow().exponent(1.3).domain([0, 1]).range([0, radius]),
padding = 5,
duration = 500;
var color = d3.scale.category20c();
var div = d3.select("#vis");
var vis = div.append("svg")
.attr("width", width + padding * 2)
.attr("height", height + padding * 2)
.append("g")
.attr("id", "container")
.attr("transform", "translate(" + [radius + padding, radius + padding] + ")");
var partition = d3.layout.partition()
.sort(null)
.value(function(d) { return 5.8 - d.depth; });
var arc = d3.svg.arc()
.startAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x))); })
.endAngle(function(d) { return Math.max(0, Math.min(2 * Math.PI, x(d.x + d.dx))); })
.innerRadius(function(d) { return Math.max(0, d.y ? y(d.y) : d.y); })
.outerRadius(function(d) { return Math.max(0, y(d.y + d.dy)); });
//d3.json("graph.json", function(error, json) {
//d3.json("http://localhost:9292/idoc-solar-test.ias.u-psud.fr/project/solar/graph?media=json", function(error, json) {
d3.select("#container").on("mouseleave", mouseleave);
d3.select("#container").on("touchend", mouseleave);
json.graph.text = 'Solar';
json.graph.children = json.graph.nodeList;
var nodes = partition.nodes({children: json.graph.children});
var path = vis.selectAll("path").data(nodes);
var pathEnter = path.enter().append("path")
.attr("id", function(d, i) { return "path-" + i; })
.attr("d", arc)
.attr("fill-rule", "evenodd")
.style("fill", function(d, i) { if (i==0) return d.color = '#FDB813'; else return d.color = color((d.children ? d : d.parent).text);})
.on("mouseover", mouseover)
.on("touchenter", mouseover)
.on("touchstart", mouseover)
.on("click", click);
var tooltips = pathEnter
.append("title")
.text(function(d, i) { if (i != 0) return d.description? d.description : d.text.split('<br/>')[0]; });
var text = vis.selectAll("text").data(nodes);
var textEnter = text.enter().append("text")
.style("fill-opacity", 1)
.style("fill", function(d) {
return brightness(d3.rgb(d.color)) < 125 ? "#fff" : "#000";
})
.attr("text-anchor", function(d, i) {
if (i == 0) {
return "middle";
}
return x(d.x + d.dx / 2) > Math.PI ? "end" : "start";
})
.attr("dy", ".2em")
.attr("dx", function(d, i){
if (i == 0) return 0;
return x(d.x + d.dx / 2) > Math.PI ? "-1.2em" : "1.2em";
})
.attr("transform", function(d, i) {
if (i == 0) {
return null;
}
var multiline = (d.text || "").split(" ").length > 1,
angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,
rotate = angle + (multiline ? -.5 : 0);
return "rotate(" + rotate + ")translate(" + (y(d.y) + padding) + ")rotate(" + (angle > 90 ? -180 : 0) + ")";
})
.on("click", click);
textEnter.append("title")
.text(function(d, i) { if (i != 0) return d.description? d.description : d.text.split('<br/>')[0]; });
var image = vis.selectAll("image").data(nodes);
imageEnter = image.enter().append("image")
.attr("class","dataIconImg")
.attr("xlink:href",function(d, i){
if (i == 0) return "";
else {
if (d.type == "node") {
if (d.text.search("Query form") > -1)
return brightness(d3.rgb(d.color)) < 125 ? "search-white.png" : "search-black.png";
else if (d.text.search("Display data") > -1)
return brightness(d3.rgb(d.color)) < 125 ? "database-white.png" : "database-black.png";
else
return "";
}
else return brightness(d3.rgb(d.color)) < 125 ? "database-white.png" : "database-black.png";
}
})
.attr('width', "1em")
.attr('height', "1em")
.style("fill-opacity", 1)
.style("fill", function(d) {
return brightness(d3.rgb(d.color)) < 125 ? "#fff" : "#000";
})
.style('cursor','pointer')
.attr("y", "-0.5em")
.attr("x", function(d){
return x(d.x + d.dx / 2) > Math.PI ? "-1em" : "0em";
})
.attr("transform", function(d, i) {
if (i == 0) {
return null;
}
var multiline = (d.text || "").split(" ").length > 1,
angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,
rotate = angle + (multiline ? -.5 : 0);
return "rotate(" + rotate + ")translate(" + (y(d.y) + padding) + ")rotate(" + (angle > 90 ? -180 : 0) + ")";
})
.on("click", function(d,i) {
if (i == 0){
return false;
}
if (d.type == "node" && (d.text.search("Query form")>-1))
sitools.user.utils.DatasetUtils.clickDatasetIcone(d.text.split("clickDatasetIcone('")[1].split(", 'forms'")[0], 'forms');
if (d.type == "node" && (d.text.search("Display data")>-1))
sitools.user.utils.DatasetUtils.clickDatasetIcone(d.text.split("clickDatasetIcone('")[1].split("', 'data'")[0], 'data');
if (d.type == "dataset")
sitools.user.utils.DatasetUtils.clickDatasetIcone(d.url, 'data');
return false;
})
.on("mouseover", iconhover);
function iconhover(d,i) {
//alert('ddd');
imageEnter.attr("width", function(e){
if (d.text == e.text) {
return "1.1em";
} else {
return "1em";
}
})
.attr("height", function(e){
if (d.text == e.text) {
return "1.1em";
} else {
return "1em";
}
});
}
image.append("svg:title")
.text(function(d, i) {
if (i == 0) return "";
else {
if (d.type == "node") {
if (d.text.search("Query form") > -1)
return "Query form";
else if (d.text.search("Display data") > -1)
return "Display data";
else
return "";
}
else return "Display data";
}
});
if (Math.min(window_h, window_w) >= 400){
var tspan = textEnter.append("tspan")
.attr("x", 0)
.style("font-weight", function(d, i) { return i == 0 ? "bold" : ""; })
.style("font-size", function(d, i) { return i == 0 ? "1.6vmin" : "1.6vmin";})
.text(function(d, i) { if (i == 0) { return 'SOLAR'; } return d.depth ? d.text.split("<br/>")[0] : ""; });
}
function click(d) {
d3.select("#container").on("mouseleave", null);
d3.selectAll("path").on("mouseover", null);
path.transition()
.duration(duration)
.attrTween("d", arcTween(d));
d3.selectAll("path")
.style("opacity", 1);
setTimeout(function(){
d3.select("#container").on("mouseleave", mouseleave);
d3.selectAll("path").on("mouseover", mouseover)
.on("touchstart", mouseover)
.on("touchenter", mouseover);
}, duration);
// Somewhat of a hack as we rely on arcTween updating the scales.
text.style("visibility", function(e) {
console.log(this);
return isParentOf(d, e) ? null : d3.select(this).style("visibility");
})
.transition()
.duration(duration)
.attrTween("text-anchor", function(d, i) {
if (i == 0) {
return null;
}
return function() {
return x(d.x + d.dx / 2) > Math.PI ? "end" : "start";
};
})
.attr("dx", function(d, i){
if (i == 0) return 0;
else return x(d.x + d.dx / 2) > Math.PI ? "-1.2em" : "1.2em";
})
.attrTween("transform", function(d, i) {
if (i == 0) {
return null;
}
var multiline = (d.text || "").split(" ").length > 1;
return function() {
var angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,
rotate = angle + (multiline ? -.5 : 0);
return "rotate(" + rotate + ")translate(" + (y(d.y) + padding) + ")rotate(" + (angle > 90 ? -180 : 0) + ")";
};
})
.style("fill-opacity", function(e) { return isParentOf(d, e) ? 1 : 1e-6; })
.each("end", function(e) {
d3.select(this).style("visibility", isParentOf(d, e) ? null : "hidden");
});
setTimeout(function(){
text.attr("dx", function(d, i){
if (i == 0) return 0;
else return x(d.x + d.dx / 2) > Math.PI ? "-1.2em" : "1.2em";
});
}, 550);
image.style("visibility", function(e) {
return isParentOf(d, e) ? null : d3.select(this).style("visibility");
})
.transition()
.duration(duration)
.attr("y", "-0.5em")
.attr("x", function(d){
return x(d.x + d.dx / 2) > Math.PI ? "-1em" : "0em";
})
.attrTween("transform", function(d, i) {
if (i == 0) {
return null;
}
var multiline = (d.text || "").split(" ").length > 1;
return function() {
var angle = x(d.x + d.dx / 2) * 180 / Math.PI - 90,
rotate = angle + (multiline ? -.5 : 0);
return "rotate(" + rotate + ")translate(" + (y(d.y) + padding) + ")rotate(" + (angle > 90 ? -180 : 0) + ")";
};
})
.style("fill-opacity", function(e) { return isParentOf(d, e) ? 1 : 1e-6; })
.each("end", function(e) {
d3.select(this).style("visibility", isParentOf(d, e) ? null : "hidden");
});
setTimeout(function(){
image.attr("x", function(d){
return x(d.x + d.dx / 2) > Math.PI ? "-1em" : "0em";;
});
}, 550);
}
//});
function mouseleave(d) {
// Hide the breadcrumb trail
//d3.select("#trail")
// .style("visibility", "hidden");
// Deactivate all segments during transition.
d3.selectAll("path").on("mouseover", null);
// Transition each segment to full opacity and then reactivate it.
d3.selectAll("path")
.transition()
.duration(500)
.style("opacity", 1)
.each("end", function() {
d3.select(this).on("mouseover", mouseover);
d3.select(this).on("touchstart", mouseover);
d3.select(this).on("touchenter", mouseover);
});
d3.selectAll("text")
.transition()
.duration(500)
.style("opacity", 1);
d3.selectAll("image")
.transition()
.duration(500)
.style("opacity", 1);
}
function mouseover(d) {
var sequenceArray = getAncestors(d);
//updateBreadcrumbs(sequenceArray, percentageString);
// Fade all the segments.
d3.selectAll("path")
.style("opacity", 0.3);
d3.selectAll("text")
.style("opacity", 0.3);
d3.selectAll("image")
.style("opacity", 0.3);
// Then highlight only those that are an ancestor of the current segment.
vis.selectAll("path")
.filter(function(node) {
return (sequenceArray.indexOf(node) >= 0);
})
.style("opacity", 1);
vis.selectAll("text")
.filter(function(node) {
return (sequenceArray.indexOf(node) >= 0);
})
.style("opacity", 1);
vis.selectAll("image")
.filter(function(node) {
return (sequenceArray.indexOf(node) >= 0);
})
.style("opacity", 1);
}
function isParentOf(p, c) {
if (p === c) return true;
if (p.children) {
return p.children.some(function(d) {
return isParentOf(d, c);
});
}
return false;
}
function getAncestors(node) {
var path = [];
var current = node;
while (current.parent) {
path.unshift(current);
current = current.parent;
}
path.unshift(current);
return path;
}
function colour(d) {
if (d.children) {
// There is a maximum of two children!
var colours = d.children.map(colour),
a = d3.hsl(colours[0]),
b = d3.hsl(colours[1]);
// L*a*b* might be better here...
return d3.hsl((a.h + b.h) / 2, a.s * 1.2, a.l / 1.2);
}
return d.colour || "#fff";
}
// Interpolate the scales!
function arcTween(d) {
var my = maxY(d),
xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
yd = d3.interpolate(y.domain(), [d.y, my]),
yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
return function(d) {
return function(t) { x.domain(xd(t)); y.domain(yd(t)).range(yr(t)); return arc(d); };
};
}
function maxY(d) {
return d.children ? Math.max.apply(Math, d.children.map(maxY)) : d.y + d.dy;
}
// http://www.w3.org/WAI/ER/WD-AERT/#color-contrast
function brightness(rgb) {
return rgb.r * .299 + rgb.g * .587 + rgb.b * .114;
}
}
</script>