-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
475 lines (417 loc) · 16.9 KB
/
script.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// This JavaScript file is an implementation of a D3 Sunburst visualization that fetches data from a JSON file.
// The visualization provides several interactive features, including:
// - Checkbox filtering for root's children
// - Updating the visualization based on selected children
// - Changing color schemes for colorblind users
// - Increasing and decreasing text size
// - Bold and reset text styles
// - Clickable arcs for drilling down into deeper levels of the hierarchy
// The getData function fetches data from the JSON file and returns the parsed JSON data.
async function getData() {
const response = await fetch('data/data.json');
const jsonData = await response.json();
return jsonData;
}
// The main function is the entry point of the script, where the visualization is created and rendered.
async function main() {
var root = await getData();
var width = window.innerWidth,
height = window.innerHeight,
radius = Math.min(width, height) * 0.48;
var svg = d3.select(".visualization").append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", `0 0 ${width} ${height}`)
.append("g").attr("transform", "translate(" + width / 2 + "," + (height / 2 + 10) + ") rotate(-90 0 0)");
// Create checkboxes for children
var checkboxContainer = d3.select(".checkbox-container");
root.children.forEach((child, index) => {
if (index === 0){
index = 2
} else if (index === 1) {
index = 3
} else if (index === 2) {
index = 0
} else if (index === 3) {
index = 1
}
checkboxContainer.append("input")
.attr("type", "checkbox")
.attr("id", `checkbox-${index}`)
.attr("checked", true);
checkboxContainer.append("label")
.attr("for", `checkbox-${index}`)
.text(child.name);
checkboxContainer.append("br");
});
// Function to update the visualization based on the selected children
function updateVisualization() {
// Filter the children based on the selected checkboxes
var selectedChildren = root.children.filter((_, index) => {
return document.getElementById(`checkbox-${index}`).checked;
});
// Create a new root object with the selected children
var newRoot = {
name: root.name,
children: selectedChildren
};
// Clear the existing visualization
svg.selectAll("*").remove();
// Render the new visualization
renderVisualization(newRoot);
}
// Render the visualization initially
renderVisualization(root);
// The renderVisualization function is responsible for drawing the Sunburst chart based on the provided root node.
function renderVisualization(root) {
var x = d3.scale.linear().range([0, 2 * Math.PI]);
var y = d3.scale.sqrt()
.range([0, radius])
var color = d3.scale.category20();
// The Sunburst visualization uses D3's partition layout to calculate the arc sizes and positions based on the hierarchical data.
var partition = d3.layout.partition()
.value(function (d) {
return d.size;
});
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, y(d.y));
})
.outerRadius(function (d) {
return Math.max(0, y(d.y + d.dy));
});
var g = svg.selectAll("g")
.data(partition.nodes(root)).enter().append("g");
var path = g.append("path").attr("d", arc)
.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
})
.on("click", click)
.on("mouseover", onMouseOver)
.on("mouseout", onMouseOut)
.attr("opacity", function(d) {
return d.depth <= 1 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 1 ? "auto" : "none";
});
var textSize = "20"
var text = g.append("text")
.attr("x", function (d) {
return y(d.y);
})
.attr("dx", "8")
.attr("dy", ".35em")
.attr("transform", function (d) {
return "rotate(" + computeTextRotation(d) + ")";
})
.text(function (d) {
return d.name;
})
.style("fill", "#f7f7f7")
.style("font-size", textSize+"px")
.attr("opacity", function(d) {
return d.depth <= 1 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 1 ? "auto" : "none";
});
// The visualization supports different color schemes to cater to users with color vision deficiencies.
// Color scale for Protanomaly (Red-Weak)
var protanomalyColor = d3.scale.ordinal().range([
"#009E73",
"#F0E442",
"#0072B2",
"#D55E00",
"#CC79A7",
]);
// Color scale for Deuteranomaly (Green-Weak)
var deuteranomalyColor = d3.scale.ordinal().range([
"#0072B2",
"#F0E442",
"#009E73",
"#D55E00",
"#CC79A7",
]);
// Color scale for Tritanomaly (Blue-Weak)
var tritanomalyColor = d3.scale.ordinal().range([
"#D55E00",
"#F0E442",
"#0072B2",
"#009E73",
"#CC79A7",
]);
// Color scale for Protanopia (Red-Blind)
var protanopiaColor = d3.scale.ordinal().range([
"#009E73",
"#E69F00",
"#56B4E9",
"#D55E00",
"#CC79A7",
]);
// Color scale for Deuteranopia (Green-Blind)
var deuteranopiaColor = d3.scale.ordinal().range([
"#0072B2",
"#E69F00",
"#56B4E9",
"#D55E00",
"#CC79A7",
]);
// Color scale for Tritanopia (Blue-Blind)
var tritanopiaColor = d3.scale.ordinal().range([
"#D55E00",
"#E69F00",
"#56B4E9",
"#009E73",
"#CC79A7",
]);
// Color scale for Monochromacy/Achromatopsia
var achromatopsiaColor = d3.scale.ordinal().range([
"#CCCCCC",
"#999999",
"#666666",
"#333333",
"#000000",
]);
// Color scale for Blue Cone Monochromacy
var blueConeMonochromacyColor = d3.scale.ordinal().range([
"#AA00AA",
"#550055",
"#AADDAA",
"#558855",
"#999999",
]);
// The text size and style can be adjusted using the provided buttons, and the arcs can be clicked to drill down into deeper levels of the hierarchy.
function increaseTextSize() {
textSize = Number(textSize) + 1
text.style("font-size", textSize);
}
function decreaseTextSize() {
textSize = Number(textSize) - 1
text.style("font-size", textSize);
}
function boldtext() {
text.style("font-weight", "bold")
}
function defaulttext() {
text
.style("font-weight", "normal")
.style("font-size", "20px") // Set this value to your default font size
.style("fill", "#f7f7f7") // Set this value to your default text color
.style("text-shadow", "none");
}
function onMouseOver(d) {
var tooltip = d3.select(".tooltip");
tooltip.transition().duration(200).style("opacity", 1);
tooltip.html(`<b>Name:</b> ${d.name}<br><b>Size:</b> ${d.size || 0}`);
d3.select(this).style("cursor", "pointer");
}
function onMouseOut() {
var tooltip = d3.select(".tooltip");
tooltip.transition().duration(500).style("opacity", 0);
d3.select(this).style("cursor", "default");
}
function computeTextRotation(d) {
var angle = x(d.x + d.dx / 2) - Math.PI / 2;
return angle / Math.PI * 180;
}
function click(d) {
if (d.size !== undefined) {
d.size += 100;
};
if (d.depth === 0) {
path
.attr("opacity", function(d) {
return d.depth <= 1 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 1 ? "auto" : "none";
});
text
.attr("opacity", function(d) {
return d.depth <= 1 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 1 ? "auto" : "none";
});
} else if (d.depth === 1) {
path
.attr("opacity", function(d) {
return d.depth <= 2 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 2 ? "auto" : "none";
});
text
.attr("opacity", function(d) {
return d.depth <= 2 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 2 ? "auto" : "none";
});
} else if (d.depth === 2) {
path
.attr("opacity", function (d) {
return d.depth <= 3 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 3 ? "auto" : "none";
});
text
.attr("opacity", function(d) {
return d.depth <= 3 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 3 ? "auto" : "none";
});
} else if (d.depth === 3) {
path
.attr("opacity", function (d) {
return d.depth <= 4 ? 1 : 0;
})
.style("pointer-events", function (d) {
return d.depth <= 4 ? "auto" : "none";
});
text
.attr("opacity", function (d) {
return d.depth <= 4 ? 1 : 0;
})
.style("pointer-events", function (d) {
return d.depth <= 4 ? "auto" : "none";
});
}
text.style("font-size", textSize + "px");
text.transition().attr("opacity", 0);
path.transition().duration(750)
.attrTween("d", arcTween(d))
.each("end", function (e, i) {
if (e.x >= d.x && e.x < (d.x + d.dx)) {
var arcText = d3.select(this.parentNode).select("text");
arcText.transition().duration(750)
.attr("opacity", 1)
.attr("transform", function () {
return "rotate(" + computeTextRotation(e) + ")"
})
.attr("x", function (d) {
return y(d.y);
});
}
})
}
// The script also includes helper functions for handling arc animations and updating the color scheme.
function arcTween(d) {
var xd = d3.interpolate(x.domain(), [d.x, d.x + d.dx]),
yd = d3.interpolate(y.domain(), [d.y, 1]),
yr = d3.interpolate(y.range(), [d.y ? 20 : 0, radius]);
return function (d, i) {
return i ? function (t) {
return arc(d);
} : function (t) {
x.domain(xd(t));
y.domain(yd(t)).range(yr(t));
return arc(d);
};
};
}
function updateColorScheme(scheme) {
if (scheme === 'default') {
color = d3.scale.category20();
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
} else if (scheme === 'protanomaly') {
color = protanomalyColor;
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
} else if (scheme === 'deuteranomaly') {
color = deuteranomalyColor;
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
} else if (scheme === 'tritanomaly') {
color = tritanomalyColor;
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
} else if (scheme === 'protanopia') {
color = protanopiaColor;
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
} else if (scheme === 'deuteranopia') {
color = deuteranopiaColor;
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
} else if (scheme === 'tritanopia') {
color = tritanopiaColor;
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
} else if (scheme === 'achromatopsia') {
color = achromatopsiaColor;
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
} else if (scheme === 'blueConeMonochromacy') {
color = blueConeMonochromacyColor;
path.style("fill", function (d) {
return color((d.children ? d : d.parent).name);
});
}
}
function displayAllDepths() {
path
.attr("opacity", 1)
.style("pointer-events", "auto");
text
.attr("opacity", function(d) {
return d.depth !== 4 ? 1 : 0;
})
.style("pointer-events", "auto")
}
function resetView() {
path
.attr("opacity", function(d) {
return d.depth <= 1 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 1 ? "auto" : "none";
});
text
.attr("opacity", function(d) {
return d.depth <= 1 ? 1 : 0;
})
.style("pointer-events", function(d) {
return d.depth <= 1 ? "auto" : "none";
});
}
document.getElementById("display-all-depths").addEventListener("click", displayAllDepths);
document.getElementById("reset-view").addEventListener("click", resetView);
// Update the visualization when the "Update Visualization" button is clicked
document.getElementById("update-btn").addEventListener("click", updateVisualization);
// Interactions with the visualization are managed through event listeners and callbacks.
document.getElementById("increase-text-size").addEventListener("click", increaseTextSize);
document.getElementById("decrease-text-size").addEventListener("click", decreaseTextSize);
document.getElementById("bold-text").addEventListener("click", boldtext);
document.getElementById("default-text").addEventListener("click", defaulttext);
document.getElementById("color-scheme").addEventListener("change", function () {
updateColorScheme(this.value);
});
document.getElementById('go-to-description').addEventListener('click', function() {
window.location.href = 'description.html';
});
document.getElementById('go-to-visualization').addEventListener('click', function() {
window.location.href = 'index.html';
});
d3.select(self.frameElement).style("height", height + "px");
}
}
main();