-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtimeline.mjs
More file actions
468 lines (405 loc) · 16.8 KB
/
Copy pathtimeline.mjs
File metadata and controls
468 lines (405 loc) · 16.8 KB
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
import { filterData, sortNodesByCategory, getDataEntry, showStudyModal } from "./dataUtility.mjs";
import { createLegend, drawNode, highlightNode, removeHighlighting } from "./d3DrawingUtility.mjs";
// Load data from the backend
const coauthorMatrix = $("#timeline-graph-container").data("coauthor");
const citationMatrix = $("#timeline-graph-container").data("citation");
const filterCategories = $("body").data("filter-categories");
const excludedCategories = $(".category-dropdown-container").data("excluded-categories");
const infoCirclePath = $("#timelineConnectionsModal").data("info-circle-path");
// Citing order
const citingOrder = {"Cites": 0, "Cited By": 1, "Coauthor": 2};
// Populate the timeline dropdown menu
filterCategories.forEach(category => {
if (excludedCategories.includes(category)) return;
const shortCategory = category.split("_").pop();
$("#timelineColorCategory").append(
`<option value="${category}">${shortCategory}</option>`
);
});
// Set the default value for the dropdown menu
let colorCategory = window.sessionStorage.getItem("colorCategory") || "";
$("#timelineColorCategory").val(colorCategory);
// Set the shared authors option
let showSharedAuthors = window.sessionStorage.getItem("showSharedAuthors") || "true";
$("#timeline-toggle-shared-authors").prop("checked", showSharedAuthors === "true");
// Set the current citation mode
let citationMode = window.sessionStorage.getItem("citationMode") || "both";
$(`#timeline-mode-${citationMode}`).prop("checked", true);
function showNetworkModal(id) {
// Get the data entry for the selected ID
const entry = getDataEntry(id);
// Get links which represent a citing relationship (the source cites the target)
const citingLinks = d3.selectAll(".citing")
.filter(d => d.sourceID === id).data();
// Get links which represent a cited by relationship (the source is cited by the target)
const citedByLinks = d3.selectAll(".cited-by")
.filter(d => d.sourceID === id).data();
// Get the links that represent a coauthor relationship
const coauthorLinks = d3.selectAll(".coauthor")
.filter(d => d.sourceID === id).data();
// Combine target IDs that appear in more than one type of link
const targetIDs = {};
citingLinks.forEach(link => (targetIDs[link.targetID] = (targetIDs[link.targetID] || [])).push("Cites"));
citedByLinks.forEach(link => (targetIDs[link.targetID] = (targetIDs[link.targetID] || [])).push("Cited By"));
coauthorLinks.forEach(link => (targetIDs[link.targetID] = (targetIDs[link.targetID] || [])).push("Coauthor"));
const orderedIDs = Object.keys(targetIDs).sort((a, b) => {
if (targetIDs[a].length !== targetIDs[b].length) {
return targetIDs[b].length - targetIDs[a].length; // Sort by number of connections
}
return citingOrder[targetIDs[a][0]] - citingOrder[targetIDs[b][0]]; // Sort by connection type priority
})
const colgroupHTML = `
<colgroup>
<col style="width: 3%;"> <!-- Info icon column -->
<col style="width: 5%;"> <!-- ID column -->
<col style="width: 17%;"> <!-- Authors column -->
<col style="width: 8%;"> <!-- Year column -->
<col style="width: 15%;"> <!-- Location column -->
<col style="width: 12%;"> <!-- Body Part column -->
<col style="width: 18%;"> <!-- Gesture column -->
<col style="width: 10%;"> <!-- Empty column for alignment -->
</colgroup>
`;
const headerHTML = `
<h5 class="mb-3 text-start">Selected Study</h5>
<div class="table-responsive">
<table class="table table-striped">
${colgroupHTML}
<thead>
<tr>
<th class="centered-cell" ></th>
<th class="centered-cell" >ID</th>
<th class="centered-cell" >Authors</th>
<th class="centered-cell" >Year</th>
<th class="centered-cell" >Location</th>
<th class="centered-cell" >Body Part</th>
<th class="centered-cell" >Gesture</th>
<th class="centered-cell" ></th> <!-- Empty header for alignment -->
</tr>
</thead>
<tbody>
<tr class="selected-study-row">
<td class="centered-cell" ><img src="${infoCirclePath}" alt="Info cirle for this row" title="View details" data-ID=${entry["ID"]} class="info-circle"/></td>
<td class="centered-cell" >${entry["ID"]}</td>
<td class="centered-cell" >${entry["Main Author"]}</td>
<td class="centered-cell" >${entry["Year"]}</td>
<td class="centered-cell" >${entry["Location"]}</td>
<td class="centered-cell" >${entry["Input Body Part"]}</td>
<td class="centered-cell" >${entry["Gesture"]}</td>
<td class="centered-cell" ></td> <!-- Empty cell for alignment -->
</tr>
</tbody>
</table>
</div>
`;
let connectionsHTML;
if (citingLinks.length === 0 && citedByLinks.length === 0 && coauthorLinks.length === 0) {
connectionsHTML = "<h5 class='text-start'>Study Network</h5><p>No connections found with the current filter settings.</p>";
} else {
connectionsHTML = `
<h5 class="mb-3 text-start">Study Network</h5>
<div class="table-responsive">
<table class="table table-striped">
${colgroupHTML}
<thead>
<tr>
<th class="centered-cell"></th>
<th class="centered-cell">ID</th>
<th class="centered-cell">Authors</th>
<th class="centered-cell">Year</th>
<th class="centered-cell">Location</th>
<th class="centered-cell">Body Part</th>
<th class="centered-cell">Gesture</th>
<th class="centered-cell">Connection Type</th>
</tr>
</thead>
<tbody>
${orderedIDs.map(targetID => {
const entry = getDataEntry(targetID);
return `
<tr>
<td class="centered-cell"><img src="${infoCirclePath}" alt="Info cirle for this row" title="View details" data-ID=${entry["ID"]} class="info-circle"/></td>
<td class="centered-cell">${entry["ID"]}</td>
<td class="centered-cell">${entry["Main Author"]}</td>
<td class="centered-cell">${entry["Year"]}</td>
<td class="centered-cell">${entry["Location"]}</td>
<td class="centered-cell">${entry["Input Body Part"]}</td>
<td class="centered-cell">${entry["Gesture"]}</td>
<td class="centered-cell">${targetIDs[targetID].map(formatConnectionType).join(", ")}</td>
</tr>`;
}).join("\n")}
</tbody>
</table>
</div>
<p class="text-start"> Total connections: ${citingLinks.length + citedByLinks.length + coauthorLinks.length} </p>
`;
};
// Append the generated HTML to the modal and show it
$("#timelineConnectionsContainer").html(headerHTML);
$("#timelineConnectionsContainer").append(connectionsHTML);
$("#timelineConnectionsModal").modal("show");
}
function formatConnectionType(value) {
switch (value) {
case "Cites":
return "<span class='text-primary'>Cites</span>";
case "Cited By":
return "<span class='text-success'>Cited By</span>";
default:
return "<span class='text-secondary'>Shared Authors</span>";
};
};
/*
* Preparing the data for the timeline graph.
* The graph will be rendered using the coauthor and citation matrices.
*/
function generateTimelineData() {
// Get the currently active nodes based on the selected category and filters
const activeNodes = filterData(JSON.parse(window.sessionStorage.getItem("filters"))).map(item => item["ID"].toString());
// Order the nodes by the selected category
const { sortedNodes, colorScale } = sortNodesByCategory(activeNodes, colorCategory);
// Append the year to each node
const nodes = sortedNodes.map(node => {
return {
id: node,
year: getDataEntry(node, "Year"),
}
});
const years = {};
nodes.forEach(node => {
if (!years[node.year]) {
years[node.year] = [node.id];
} else {
years[node.year].push(node.id);
}
});
const maxYears = Math.max(...Object.keys(years).map(year => years[year].length));
// Create links for co-authors and citations
const links = {coauthorLinks: [], citingLinks: [], citedByLinks: []};
for (const node of sortedNodes) {
for (const other of sortedNodes) {
// Populate the links for co-authors
if (coauthorMatrix[node][other]) {
links.coauthorLinks.push({
sourceID: node,
targetID: other,
});
};
// Populate the links for citations
if (citationMatrix[node][other]) {
links.citingLinks.push({
sourceID: node,
targetID: other,
});
};
if (citationMatrix[other][node]) {
links.citedByLinks.push({
sourceID: node,
targetID: other,
});
}
}
}
return {
nodes,
years,
links,
maxYears,
colorScale
}
}
function drawTimelineGraph() {
// Clear the previous graph
$("#timeline-graph-container").empty();
$("#timeline-graph-container").height("auto");
$("#legend").empty();
const { nodes, years, links, maxYears, colorScale } = generateTimelineData();
const { coauthorLinks, citingLinks, citedByLinks } = links;
const maxYearsCount = Math.max(...Object.values(years).map(year => year.length));
// If there are no nodes, do not draw the graph
if (nodes.length === 0) {
$("#timeline-graph-container").append("<p class='m-2 p-0'>No studies available for the selected sidebar filters. Please select some of the criteria from the sidebar at the right.</p>");
return;
}
// Set up layout dimensions for the graph
const margin = { top: 20, right: 50, bottom: 50, left: 50 };
const innerWidth = $("#timeline-graph-container").width() - margin.left - margin.right;
const nodeRadius = innerWidth / 150;
const height = Math.max(250, maxYearsCount * (nodeRadius * 4));
const innerHeight = height - margin.top - margin.bottom;
const axisHeight = innerHeight;
// Create the svg container for the timeline graph
const svg = d3.select("#timeline-graph-container")
.append("svg")
.attr("width", $("#timeline-graph-container").width())
.attr("height", height)
.attr("viewBox", `0 0 ${$("#timeline-graph-container").width()} ${height}`);
// Create an x scale for the years
const xScale = d3.scalePoint()
.domain(Object.keys(years).map(Number))
.range([0, innerWidth]);
// Create a y scale for the nodes
const yScale = d3.scaleLinear()
.domain([0, maxYears])
.range([axisHeight - margin.bottom, 0]);
// Create an x axis for the years
const xAxis = d3.axisBottom(xScale)
.tickFormat(d3.format("d"))
.tickSize(5);
// Draw the x axis
svg.append("g")
.attr("class", "x-axis")
.attr("transform", `translate(${margin.left}, ${axisHeight + margin.top})`)
.call(xAxis);
// Format the labels for the axis
svg.selectAll(".x-axis text")
.style("font-size", "1.2em")
.style("user-select", "none");
// Create a link group for the links
const linkGroup = svg.append("g")
.attr("class", "links")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
// Create a node group for the nodes
const nodeGroup = svg.append("g")
.attr("class", "nodes")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
const arc = d3.arc()
.innerRadius(0)
.outerRadius(nodeRadius);
// Draw the nodes on the timeline
nodeGroup.selectAll(".node")
.data(nodes.map(node => node.id))
.join("g")
.attr("class", "node")
.attr("transform", d => {
const year = nodes.find(node => node.id === d).year;
return `translate(${xScale(year)}, ${yScale(years[year].indexOf(d))})`;
})
.each(function(d) {
drawNode(d3.select(this), colorCategory, arc, colorScale)
})
.on("click", function(event, d) {
showNetworkModal(d);
})
.on("mouseover", function(event, d) {
// Show the tooltip next to the node
nodeTooltip.style("visibility", "visible");
nodeTooltip.style("left", `${event.pageX + 15}px`);
nodeTooltip.style("top", `${event.pageY}px`);
// Populate the tooltip with the node data
const entry = getDataEntry(d);
nodeTooltip.html(`
<strong>Study ${entry["ID"]}</strong>
<p>${entry["Main Author"]} (${entry["Year"]})</p>
<p>Location: ${entry["Location"]}</p>
`);
// Highlight the node and its connections
highlightNode(d, nodeRadius, citationMode === "cited-by" || citationMode === "cites");
})
.on("mouseout", function(event, d) {
// Hide the tooltip when the mouse leaves the node
nodeTooltip.style("visibility", "hidden");
removeHighlighting(nodeRadius);
});
const nodeTooltip = d3.select("#timeline-graph-container").append("div")
.attr("class", "node-tooltip")
.style("visibility", "hidden");
// Draw the links between the nodes
if (showSharedAuthors === "true") {
linkGroup.selectAll(".link .coauthor")
.data(coauthorLinks)
.join("path")
.attr("class", "coauthor link")
.attr("d", d => drawLink(d));
};
if (citationMode === "both" || citationMode === "cites") {
linkGroup.selectAll(".link .citing")
.data(citingLinks)
.join("path")
.attr("class", "citing link")
.attr("d", d => drawLink(d));
};
if (citationMode === "both" || citationMode === "cited-by") {
linkGroup.selectAll(".link .cited-by")
.data(citedByLinks)
.join("path")
.attr("class", "cited-by link")
.attr("d", d => drawLink(d));
}
function drawLink(d) {
const sourceNode = nodes.find(node => node.id === d.sourceID);
const targetNode = nodes.find(node => node.id === d.targetID);
const sourceX = xScale(sourceNode.year);
const targetX = xScale(targetNode.year);
const sourceY = yScale(years[sourceNode.year].indexOf(sourceNode.id));
const targetY = yScale(years[targetNode.year].indexOf(targetNode.id));
if (sourceX === targetX) {
// If the souce and target are in the same year, draw an arc
const midY = (sourceY + targetY) / 2;
return `M ${sourceX},${sourceY} Q ${Math.min(sourceX + xScale.step(), $("#timeline-graph-container").width() - margin.right / 2)},${midY} ${targetX},${targetY}`;
} else {
// For different years, create a bezier curve
const dx = targetX - sourceX;
const controlOffset = Math.min(Math.abs(dx) * 0.4, 100); // Limit control point offset
// Calculate control points - higher curves for connections between distant years
const controlX1 = sourceX + Math.sign(dx) * controlOffset;
const controlY1 = sourceY - 40; // Curve upward
const controlX2 = targetX - Math.sign(dx) * controlOffset;
const controlY2 = targetY - 40; // Curve upward
// If the source and target are in different years, draw a bezier curve
return `M ${sourceX},${sourceY} C ${controlX1},${controlY1} ${controlX2},${controlY2} ${targetX},${targetY}`;
};
};
// Add hover title to links
linkGroup.selectAll(".citing")
.append("title")
.text(d => `[${d.sourceID}] cites [${d.targetID}]`);
linkGroup.selectAll(".cited-by")
.append("title")
.text(d => `[${d.sourceID}] is cited by [${d.targetID}]`);
linkGroup.selectAll(".coauthor")
.append("title")
.text(d => `[${d.sourceID}] shares authors with [${d.targetID}]`);
// Create a legend for the colors
createLegend(nodes.map(node => node.id), colorScale, colorCategory, $("#legend"));
}
$(document).ready(function() {
drawTimelineGraph();
$("#timeline-toggle-shared-authors").on("click", function() {
showSharedAuthors = $(this).is(":checked").toString();
window.sessionStorage.setItem("showSharedAuthors", showSharedAuthors);
drawTimelineGraph();
})
$("input[name='citation-mode']").on("click", function() {
citationMode = $(this).val();
window.sessionStorage.setItem("citationMode", citationMode);
drawTimelineGraph();
});
// Set the coloring strategy to the session storage
$("#timelineColorCategory").on("change", function() {
colorCategory = $(this).val();
window.sessionStorage.setItem("colorCategory", colorCategory);
drawTimelineGraph();
});
window.addEventListener("resize", function() {
drawTimelineGraph();
});
$(".value-filter").on("change", function() {
drawTimelineGraph();
});
$(".exclusive-filter").on("click", function() {
drawTimelineGraph();
});
$(".range-slider").each(function() {
this.noUiSlider.on("end", function() {
drawTimelineGraph();
})
});
// Handle clicks on the info circles in the connections modal
$("#timelineConnectionsContainer").on("click", ".info-circle", function() {
const id = $(this).data("id");
// Close the network modal if it's open
$("#timelineConnectionsModal").modal("hide");
showStudyModal(id);
});
})