-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
360 lines (312 loc) · 10.4 KB
/
app.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
var selected = [];
MiniBarOptions = {
barType: "default",
minBarSize: 100,
alwaysShowBars: true,
};
sigma.classes.graph.addMethod('neighbors', function (nodeId) {
var k,
neighbors = {},
index = this.allNeighborsIndex[nodeId] || {};
for (k in index)
neighbors[k] = this.nodesIndex[k];
return neighbors;
});
// Define a function to map edge weights to colors dynamically
function getEdgeColor(weight, maxWeight) {
var colorScale = ['', '#FFB14E', '#FA8775', '#EA5F94', '#CD34B5', '#9D02D7', '#0000FF', '#00008B'];
var index = Math.floor(weight / maxWeight * (colorScale.length - 1));
return colorScale[index];
}
// Initialise sigma with settings
var s = new sigma({
renderers: [
{
type: 'canvas',
container: document.getElementById('graph-container'),
freeStyle: true
}
],
settings: {
minNodeSize: 1,
maxNodeSize: 20,
minEdgeSize: 0.1,
maxEdgeSize: 4,
defaultEdgeType: "curve", // only works on canvas renderer
// labelColor: "node",
defaultLabelColor: "#2d3937",
defaultHoverLabelBGColor: "#171c1c",
defaultLabelHoverColor: "#dffcff",
font: "Noto Sans",
drawLabels: true,
mouseWheelEnabled: true,
doubleClickEnabled: false,
touchEnabled: true,
},
});
// Variable to store the maximum weight
var maxWeight = 0;
// Load data to the graph
sigma.parsers.gexf("everythinggraph.gexf", s,
function (s) {
// Iterate over edges to find the maximum weight
s.graph.edges().forEach(function (e) {
if (e.weight > maxWeight) {
maxWeight = e.weight;
}
});
// Add various parameters to nodes and edges
s.graph.nodes().forEach(function (n) {
n.label = n.label;
n.color = getColor(n.attributes["modularity_class"]);
n.originalColor = n.color;
n.inactiveColor = "rgba(69, 84, 84, 0.6)";
n.selectedColor = "#ffff00";
});
s.graph.edges().forEach(function (e) {
e.originalColor = e.color;
e.inactiveColor = "0000";
// Use the getEdgeColor function to assign colors based on edge weights
e.color = getEdgeColor(e.weight, maxWeight);
});
s.refresh();
populateSearchList(s.graph.nodes());
// Add event listeners to buttons
var inputBox = document.getElementById('search-input');
inputBox.addEventListener("change", searchChange);
});
function searchChange(e) {
var value = e.target.value;
var nodeFound = false;
// Check if node exists and toggle selection
s.graph.nodes().forEach(function (n) {
if (n.label === value) {
nodeFound = true;
if (selected[n.id]) {
delete selected[n.id]; // Deselect if already selected
} else {
selected[n.id] = n; // Select if not already selected
}
}
});
if (nodeFound) {
nodeSelect(s, selected);
} else {
// Optionally handle the case where the search term does not match any node
}
// Ensure input is cleared or refocus to allow re-searching the same node
e.target.value = '';
}
function selectNodeByLabel(label) {
var found = false;
s.graph.nodes().forEach(function (n) {
if (n.label === label) {
found = true;
if (!selected[n.id]) {
selected[n.id] = n;
} else {
delete selected[n.id];
}
}
});
if (found) {
nodeSelect(s, selected); // Assume nodeSelect handles graph updates for selection
} else {
// Handle case where label is not found, if necessary
}
}
function zoomIn() {
var c = s.camera;
c.goTo({
ratio: c.ratio / c.settings('zoomingRatio')
});
}
function zoomOut() {
var c = s.camera;
c.goTo({
ratio: c.ratio * c.settings('zoomingRatio')
});
}
// Variables to track the initial camera position on mouse down
var initialCameraX, initialCameraY;
// Listen for mouse down to capture the initial camera position
s.renderers[0].container.addEventListener('mousedown', function (e) {
initialCameraX = s.camera.x;
initialCameraY = s.camera.y;
}, false);
// Click event for node
s.bind('clickNode', function (e) {
var cameraMoved = initialCameraX !== s.camera.x || initialCameraY !== s.camera.y;
if (!cameraMoved) { // Only modify selection if the camera hasn't moved
// Add or remove from selected array
if (!selected[e.data.node.id]) {
selected[e.data.node.id] = e.data.node;
} else {
delete selected[e.data.node.id];
}
nodeSelect(s, selected);
}
});
// Mouse over event
s.bind('overNode', function (e) {
nodeHover(s, e.data.node, selected);
});
// Mouse out event
s.bind('outNode', function (e) {
nodeHoverOut(s, selected);
});
// When the background is clicked, we reset the graph
s.bind('clickStage', function (e) {
var cameraMoved = initialCameraX !== s.camera.x || initialCameraY !== s.camera.y;
if (!cameraMoved) { // Reset selections only if the camera hasn't moved
selected = [];
resetStates(s);
showSelectedNodes(selected);
s.refresh();
}
});
function populateSearchList(nodes) {
var container = document.getElementById('nodes-datalist');
nodes.forEach(function (n) {
var item = document.createElement('option');
item.value = n.label;
container.appendChild(item);
});
}
function getColor(modularityClass) {
// Adjust this function to dynamically generate colors based on the modularity class
// Here's a simple example using HSL color space to ensure diverse colors, excluding yellow
var excludedColor = '#FFD700'; // Yellow color code
var hueStep = 360 / (modularityClass + 2); // Divide the hue range by the number of modularity classes
var hue = (modularityClass * hueStep) % 360; // Calculate hue based on modularity class
if (hue < 60) { // Adjust hue to skip yellow
hue += hueStep;
}
return 'hsl(' + hue + ', 100%, 50%)'; // Return HSL color string
}
// Show all selected nodes next to the notes for quick reference
function showSelectedNodes(selected) {
// Remove old selected nodes
document.querySelectorAll('.selected-node').forEach(function (a) {
a.remove()
});
if (Object.keys(selected).length > 0) {
var selected_container = document.getElementById('selected-nodes');
Object.keys(selected).forEach(function (key) {
var selected_item = document.createElement("div");
selected_item.classList.add('selected-node');
selected_item.innerHTML = selected[key].label;
selected_container.appendChild(selected_item);
});
}
}
// Toggle select a node
function nodeSelect(s, selected) {
// Reset graph to default state before applying new selections
resetStates(s);
if (Object.keys(selected).length > 0) {
var toKeep = nodesToKeep(s, selected);
setSelectedColor(s, selected, toKeep);
setEdgesToInactive(s, toKeep);
}
showSelectedNodes(selected);
s.refresh();
}
// Highlight hovered node and relevant nodes by greying out all else
function nodeHover(s, node, selected) {
var selectedAndHovered = [];
// Make a copy of selected and add the hovered node
Object.assign(selectedAndHovered, selected);
selectedAndHovered[node.id] = node;
var toKeep = nodesToKeep(s, selectedAndHovered);
s.graph.nodes().forEach(function (n) {
if (toKeep[n.id]) {
n.color = n.originalColor;
} else {
n.color = n.inactiveColor;
}
});
// Grey out irrelevant edges
setEdgesToInactive(s, toKeep);
s.refresh();
}
// Return graph to pre-hover state
function nodeHoverOut() {
// Start clean, and then figure out what needs to be greyed out according to selected nodes
resetStates(s);
if (Object.keys(selected).length > 0) {
var toKeep = nodesToKeep(s, selected);
setSelectedColor(s, selected, toKeep);
setEdgesToInactive(s, toKeep);
}
s.refresh();
}
// Reset all selections
function resetStates(s) {
s.graph.nodes().forEach(function (n) {
n.color = n.originalColor;
});
s.graph.edges().forEach(function (e) {
e.color = e.originalColor;
});
}
// Return matching items from two arrays
function returnMatchingArrayItems(array1, array2) {
retain = [];
for (var i = 0; i < array1.length; i += 1) {
if (array2.indexOf(array1[i]) > -1) {
retain.push(array1[i]);
}
}
return retain;
}
// Return matching nodes from two arrays -
// for instance when two nodes are selected only common neighbors should be shown
function returnMatchingNodes(array1, array2) {
var retainKeys = returnMatchingArrayItems(Object.keys(array1), Object.keys(array2)),
retainNodes = [];
for (let id of retainKeys) {
retainNodes[id] = array1[id];
}
return retainNodes;
}
// Return all relevant nodes to be kept
function nodesToKeep(s, selected) {
// Make sure selected is not empty when calling this
var toKeep,
i = 0;
Object.keys(selected).forEach(function (key) {
if (i == 0) {
toKeep = s.graph.neighbors(key);
toKeep[key] = selected[key];
} else {
var keep = s.graph.neighbors(key);
keep[key] = selected[key];
toKeep = returnMatchingNodes(toKeep, keep);
}
i++;
});
return toKeep;
}
// Set the color of all selected nodes
function setSelectedColor(s, selected, toKeep) {
s.graph.nodes().forEach(function (n) {
if (selected[n.id]) {
n.color = n.selectedColor;
}
else if (!toKeep[n.id]) {
n.color = n.inactiveColor;
}
});
}
// Grey out all edges that are not between active nodes
function setEdgesToInactive(s, nodesToKeep) {
s.graph.edges().forEach(function (e) {
if (nodesToKeep[e.source] && nodesToKeep[e.target]) {
e.color = e.originalColor;
} else {
// Set the color of non-connected edges to #0000 to make them invisible
e.color = '#00000000'; // Note: The correct value for fully transparent is '#00000000' or 'rgba(0,0,0,0)'
}
});
}