-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataVis.js
451 lines (373 loc) · 13.8 KB
/
dataVis.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
/*
* Data Visualization - Framework
* Copyright (C) University of Passau
* Faculty of Computer Science and Mathematics
* Chair of Cognitive sensor systems
* Maintenance:
* 2024, Alexander Gall <[email protected]>
*
* All rights reserved.
*/
// scatterplot axes
let xAxis, yAxis, xAxisLabel, yAxisLabel;
// radar chart axes
let radarAxes, radarAxesAngle;
let dimensions = ["dimension 1", "dimension 2", "dimension 3", "dimension 4", "dimension 5", "dimension 6"];
//*HINT: the first dimension is often a label; you can simply remove the first dimension with
// dimensions.splice(0, 1);
// the visual channels we can use for the scatterplot
let channels = ["scatterX", "scatterY", "size"];
// size of the plots
let margin, width, height, radius;
// svg containers
let scatter, radar, dataTable;
// Add additional variables
let domainByDimension = {};
let domainByRadarDimension = {};
let data;
let selectedPoints = {};
let colorPalette = ["#000000","#FF4500","#228B22","#4169E1","#FFD700","#8B008B","#FF8C00","#00CED1","#FF1493","#008000"];
function init() {
// define size of plots
margin = {top: 20, right: 20, bottom: 20, left: 50};
width = 600;
height = 500;
radius = width / 2;
// Start at default tab
document.getElementById("defaultOpen").click();
// data table
dataTable = d3.select('#dataTable');
// scatterplot SVG container and axes
scatter = d3.select("#sp").append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
// radar chart SVG container and axes
radar = d3.select("#radar").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + (width / 2) + "," + (height / 2) + ")");
// read and parse input file
let fileInput = document.getElementById("upload"), readFile = function () {
// clear existing visualizations
clear();
let reader = new FileReader();
reader.onloadend = function () {
data = d3.csvParse(reader.result, d3.autoType);
CreateDataTable(data);
data.forEach((d, index) => d.id = index);
initVis(data);
initDashboard(data);
};
reader.readAsBinaryString(fileInput.files[0]);
};
fileInput.addEventListener('change', readFile);
}
function setDomainByDimension(_data, _dimensions){
_dimensions.forEach(dim => {
let extent = d3.extent(_data, d => d[dim]);
domainByDimension[dim] = extent;
let buffer = (extent[1] - extent[0]) * 0.5;
domainByRadarDimension[dim] = [extent[0] - buffer, extent[1] + buffer];
});
}
function initVis(_data){
dimensions = _data.columns;
dimensions = dimensions.filter(d => !isNaN(_data[0][d]));
setDomainByDimension(_data, dimensions);
// y scalings for scatterplot
let y = d3.scaleLinear()
.domain(domainByDimension[dimensions[0]])
.range([height - margin.bottom - margin.top, margin.top]);
// x scalings for scatter plot
let x = d3.scaleLinear()
.domain(domainByDimension[dimensions[0]])
.range([margin.left, width - margin.left - margin.right]);
// radius scalings for radar chart
let r = d3.scaleLinear()
.domain(domainByRadarDimension[dimensions[0]])
.range([0, radius]);
// scatterplot axes
yAxis = scatter.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + margin.left + ")")
.call(d3.axisLeft(y));
yAxisLabel = yAxis.append("text")
.style("text-anchor", "middle")
.attr("y", margin.top / 2)
.text("x");
xAxis = scatter.append("g")
.attr("class", "axis")
.attr("transform", "translate(0, " + (height - margin.bottom - margin.top) + ")")
.call(d3.axisBottom(x));
xAxisLabel = xAxis.append("text")
.style("text-anchor", "middle")
.attr("x", width - margin.right)
.text("y");
// radar chart axes
radarAxesAngle = Math.PI * 2 / dimensions.length;
let axisRadius = d3.scaleLinear()
.range([0, radius]);
let maxAxisRadius = 0.75,
textRadius = 0.8;
gridRadius = 0.1;
// radar axes
radarAxes = radar.selectAll(".axis")
.data(dimensions)
.enter()
.append("g")
.attr("class", "axis");
radarAxes.append("line")
.attr("x1", 0)
.attr("y1", 0)
.attr("x2", function(d, i){ return radarX(axisRadius(maxAxisRadius), i); })
.attr("y2", function(d, i){ return radarY(axisRadius(maxAxisRadius), i); })
.attr("class", "line")
.style("stroke", "black");
// Render grid lines
for (let level = 1; level <= 7; level++) {
let factor = level / 7;
radarAxes.each(function(d, i) {
let nextIndex = (i + 1) % dimensions.length;
radar.append("line")
.attr("x1", radarX(axisRadius(maxAxisRadius) * factor, i))
.attr("y1", radarY(axisRadius(maxAxisRadius) * factor, i))
.attr("x2", radarX(axisRadius(maxAxisRadius) * factor, nextIndex))
.attr("y2", radarY(axisRadius(maxAxisRadius) * factor, nextIndex))
.style("stroke", "grey")
.style("stroke-width", "0.5px")
.style("stroke-opacity", "0.9");
});
}
radar.selectAll(".axisLabel")
.data(dimensions)
.enter()
.append("text")
.attr("text-anchor", "middle")
.attr("dy", "0.35em")
.attr("x", function(d, i){ return radarX(axisRadius(textRadius), i); })
.attr("y", function(d, i){ return radarY(axisRadius(textRadius), i); })
.text(function(dimensions) { return dimensions; });
// init menu for the visual channels
channels.forEach(function(c){
initMenu(c, dimensions);
});
// refresh all select menus
channels.forEach(function(c){
refreshMenu(c);
});
renderScatterplot();
renderRadarChart();
}
// clear visualizations before loading a new file
function clear(){
scatter.selectAll("*").remove();
radar.selectAll("*").remove();
dataTable.selectAll("*").remove();
}
//Create Table
function CreateDataTable(_data) {
// Clear previous data table contents
dataTable.selectAll('*').remove();
// Create the table element
let table = dataTable.append('table').attr('class', 'data-table');
let thead = table.append('thead');
let tbody = table.append('tbody');
// Append the header row
thead.append('tr')
.selectAll('th')
.data(_data.columns)
.enter()
.append('th')
.text(function (column) { return column; });
// Create a row for each object in the data
let rows = tbody.selectAll('tr')
.data(_data)
.enter()
.append('tr');
// Create a cell in each row for each column
let cells = rows.selectAll('td')
.data(function (row) {
return _data.columns.map(function (column) {
return {column: column, value: row[column]};
});
})
.enter()
.append('td')
.text(function (d) { return d.value; });
// Apply click event for cell selection
cells.on('click', function (event, d) {
var isSelected = d3.select(this).classed('selected');
d3.selectAll('.data-table td').classed('selected', false);
if (!isSelected) {
d3.select(this).classed('selected', true);
}
});
cells.on("mouseover", function () {
d3.select(this).style("background-color", "#418bab");
})
.on("mouseout", function () {
d3.select(this).style("background-color", null);
});
}
function renderScatterplot(){
//get domain names from menu and label x- and y-axis
xAxisLabel.text(readMenu('scatterX'));
yAxisLabel.text(readMenu('scatterY'));
//re-render axes
let y = d3.scaleLinear()
.domain(domainByDimension[readMenu('scatterY')])
.range([height - margin.bottom - margin.top, margin.top]);
let x = d3.scaleLinear()
.domain(domainByDimension[readMenu('scatterX')])
.range([margin.left, width - margin.left - margin.right]);
// Update axes with transition
xAxis.transition().duration(1000).call(d3.axisBottom(x));
yAxis.transition().duration(1000).call(d3.axisLeft(y));
//render dots
let sizeScale = d3.scaleSqrt()
.domain(domainByDimension[readMenu('size')])
.range([2, 15]); // Adjust size range accordingly
// Bind data and create circles for each data point
let update = scatter.selectAll(".dot")
.data(data, d => d.id);
update.exit()
.transition() // for animation
.duration(1000) // time
.attr("r", 0)
.remove();
// Enter new elements in the data
let enter = update.enter().append("circle")
.attr("class", "dot")
.attr("r", 0) // initial dot size
.on("click", function(event, d) { // triggered when dot is clicked
handleDotClick(event, d);
});
enter.merge(update)
.transition() // for animation
.duration(1000) // time
.attr("cx", d => x(d[readMenu('scatterX')]))
.attr("cy", d => y(d[readMenu('scatterY')]))
.attr("r", d => sizeScale(d[readMenu('size')]))
.style("fill", d => selectedPoints[d.id] ? selectedPoints[d.id].color : "#708090")
.style("opacity", 0.7);
}
function handleDotClick(event, d) {
if (selectedPoints[d.id]) {
delete selectedPoints[d.id];
} else if (Object.keys(selectedPoints).length < 10) { // Limit to 10 selections
let availableColor = colorPalette.find(c => !Object.values(selectedPoints).find(sp => sp.color === c));
if (availableColor) {
selectedPoints[d.id] = { color: availableColor, data: d, label: d.Name };
}
}
updateLegend();
renderScatterplot();
renderRadarChart();
}
function updateLegend() {
let legend = d3.select("#legend").selectAll(".legend-item")
.data(Object.entries(selectedPoints), d => d[0]);
legend.exit().remove();
let legendEnter = legend.enter().append("div")
.attr("class", "legend-item");
// Append a colored dot before the label
legendEnter.append("span")
.style("background-color", d => d[1].color)
.style("border-radius", "50%")
.style("display", "inline-block")
.style("width", "10px")
.style("height", "10px")
.style("margin-right", "5px");
legendEnter.append("span")
.style("color", d => d[1].color)
.text(d => `${d[1].label}`);
// Using <span> for the close button and applying class "close"
legendEnter.append("span")
.attr("class", "close")
.text("x")
.on("click", function(event, d) {
delete selectedPoints[d[0]];
updateLegend();
renderScatterplot();
renderRadarChart();
event.stopPropagation();
});
// Merge and update existing items in the legend
legend.select("span.close").on("click", function(event, d) {
delete selectedPoints[d[0]];
updateLegend();
renderScatterplot();
renderRadarChart();
event.stopPropagation();
});
legend.selectAll("span").filter((d, i) => i === 1)
.style("color", d => d[1].color)
.text(d => d[1].label);
}
function renderRadarChart(){
radar.selectAll(".radar-line").remove(); // Clear previous lines
Object.entries(selectedPoints).forEach(([id, {color, data}]) => {
let pathData = dimensions.map((dim, i) => {
let rValue = radarScale(data[dim], dim);
return [radarX(rValue, i), radarY(rValue, i)];
});
radar.append("path")
.datum(pathData)
.attr("d", d3.line().curve(d3.curveLinearClosed))
.attr("stroke-width", 2)
.attr("stroke", color)
.attr("fill", "none")
.attr("fill-opacity", 0.2)
.attr("class", "radar-line");
});
}
function radarScale(value, dimension) {
return d3.scaleLinear()
.domain(domainByRadarDimension[dimension])
.range([0, radius])(value);
}
function radarX(radius, index){
return radius * Math.cos(radarAngle(index));
}
function radarY(radius, index){
return radius * Math.sin(radarAngle(index));
}
function radarAngle(index){
return radarAxesAngle * index - Math.PI / 2;
}
// init scatterplot select menu
function initMenu(id, entries) {
$("select#" + id).empty();
entries.forEach(function (d) {
$("select#" + id).append("<option>" + d + "</option>");
});
$("#" + id).selectmenu({
select: function () {
renderScatterplot();
}
});
}
// refresh menu after reloading data
function refreshMenu(id){
$( "#"+id ).selectmenu("refresh");
}
// read current scatterplot parameters
function readMenu(id){
return $( "#" + id ).val();
}
// switches and displays the tabs
function openPage(pageName,elmnt,color) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablink");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].style.backgroundColor = "";
}
document.getElementById(pageName).style.display = "block";
elmnt.style.backgroundColor = color;
}