-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisjs_se.js
288 lines (246 loc) · 4.96 KB
/
visjs_se.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
//import graph data sets
import {
nodesComplete,
edgesComplete
} from './data.js';
export {
search
};
var searchNodeId = 0;
//data set of initially visible nodes
var nodes = new vis.DataSet([{
id: 9,
label: "root",
physics: false
}]);
//data set of initially visible edges
var edges = new vis.DataSet([]);
var container = document.getElementById("mynetwork");
var data = {
nodes: nodes,
edges: edges,
};
//some layout options
var options = {
autoResize: true,
height: '100%',
width: '100%',
locale: 'en',
physics: {
enabled: true,
minVelocity: 15.0,
stabilization: {
enabled: true,
iterations: 1000,
fit: true
},
solver: 'forceAtlas2Based',
/*
forceAtlas2Based: {
theta: 0.1,
gravitationalConstant: -1000,
centralGravity: 0.01,
springLength: 50,
springConstant: 0.1,
avoidOverlap: 0,
damping: 1
},
forceAtlas2Based: {
theta: 0.1,
gravitationalConstant: -5000,
centralGravity: 0.05,
springLength: 50,
springConstant: 0.5,
avoidOverlap: 0,
damping: 1
},
*/
forceAtlas2Based: {
theta: 0.1,
gravitationalConstant: -1500,
centralGravity: 0.05,
springLength: 15,
springConstant: 0.1,
avoidOverlap: 0.0,
damping: 1.0
},
hierarchicalRepulsion: {
nodeDistance: 400,
centralGravity: 0.5,
avoidOverlap: 0.5
},
barnesHut: {
gravitationalConstant: -10000,
springConstant: 0.01
}
},
layout: {
improvedLayout: false,
hierarchical: {
enabled: false
}
},
edges: {
smooth: {
enabled: false
},
arrows: {
to: {
enabled: true,
scaleFactor: 1,
type: "arrow"
}
}
},
nodes: {
shapeProperties: {
interpolation: false
},
color: {
highlight: {
border: '#3e7de5',
background: '#d4e5fd'
}
}
}
};
var network = new vis.Network(container, data, options);
network.on("click", function(properties) {
var node = properties.nodes[0];
clickEvent(node);
});
network.on("deselectNode", function(properties) {
var node = properties.nodes[0];
deselectNodeEvent(node);
});
/**
* function for collapse/expand clickEvents on node elements
*
* @param {node} node Node-element that fired the click event
**/
function clickEvent(node) {
var connectedEdgesList = edges.get({
filter: function(item) {
return (item.from == node);
}
});
if (connectedEdgesList.length > 0) {
collapse(node);
} else {
expand(node);
}
}
/**
* function for the delectNode event listener. Makes sure that red painted nodes from a search result go back to normal when another node is being selected.
*
*
* @param {node} node Node-element that fired the click event
**/
function deselectNodeEvent(node) {
if (searchNodeId != 0) {
nodes.update({
id: searchNodeId,
color: {
highlight: {
// hardcoded colors, ugly!
border: '#3e7de5',
background: '#d4e5fd'
}
}
});
searchNodeId = 0;
}
}
/**
* searches for a specific string in the node labels given by the search text field and highlights all results
*
**/
function search() {
var searchString = document.getElementById("searchString").value;
var nodeList = nodesComplete.getIds({
filter: function(item) {
if (item.label == searchString) {
recursiveDiscovery(item.id);
searchNodeId = item.id;
nodes.update({
id: item.id,
color: {
highlight: "red"
}
});
return true;
}
}
});
network.selectNodes(nodeList);
network.once('stabilized', function(params) {
network.focus(nodeList[0]);
});
network.moveTo({
scale: 0.4
});
}
/**
* backtraces from a specific node to the root element and expands all visited nodes
*
* @param {node} node
**/
function recursiveDiscovery(node) {
try {
nodes.add(nodesComplete.get(node));
for (const e of edgesComplete.get()) {
if (e.to == node) {
recursiveDiscovery(e.from);
}
}
} catch (ex) {
console.log("Node already exists");
} finally {
expand(node);
}
}
/**
* "expands" a specific node by determining (edge-)connected nodes and adding the nodes and edges to the visible nodes and edges datasets
*
* @param {node} node The node-element to be expanded
**/
function expand(node) {
for (const e of edgesComplete.get()) {
if (e.from == node) {
try {
nodes.add(nodesComplete.get(e.to));
} catch (ex) {
console.info("Node already exists");
}
try {
edges.add(e);
} catch (ex) {
console.info("Edge already exists");
}
}
}
}
/**
* "collapses" a specific node by determining (edge-)connected nodes and removing the nodes and edges from the visible nodes and edges datasets
*
* @param {node} node The node-element to be collapsed
**/
function collapse(node) {
var outgoingEdgesList = edges.get({
filter: function(item) {
return (item.from == node);
}
});
for (const e of outgoingEdgesList) {
var incomingEdgesList = edges.get({
filter: function(item) {
return (item.to == e.to);
}
});
if (e.from != e.to && incomingEdgesList.length < 2) {
collapse(e.to);
nodes.remove(e.to);
}
edges.remove(e);
}
}