/
graph.js
81 lines (64 loc) · 1.7 KB
/
graph.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
graph_created = false;
function create_edges(nodes){
list_id = get_all_id(nodes);
edges = []
for(let i = 0; i < nodes.length; i++){
for(let j = 0; j < nodes[i].neighbors.length; j++){
from = nodes[i].id;
to = nodes[i].neighbors[j];
edge = create_specific_edge(from, to);
// Adds edge in the edges list
if(is_in_list(from, list_id) && is_in_list(to, list_id)){
edges.push(edge);
console.log('a');
// list_id.splice(list_id.indexOf(to), 1);
}
}
list_id.splice(list_id.indexOf(from), 1);
}
return edges;
}
function is_in_list(value, list) {
return list.indexOf(value) > -1;
}
function create_specific_edge(from, to){
return {from: from, to: to}; //label: String(from) + ' -> ' + String(to)
}
function get_all_id(nodes){
list_id = []
for(let i = 0; i < nodes.length; i++){
list_id.push(nodes[i].id);
}
return list_id;
}
function draw(nodes){
var container = document.getElementById('mynetwork');
if(!graph_created){
nodes = create_graph();
graph_created = true;
}
var edges = create_edges(nodes)
var data = {
nodes: nodes,
edges: edges
};
var options = {
nodes: {
borderWidth: 2,
size:30
},
physics: {
forceAtlas2Based: {
gravitationalConstant: -20,
centralGravity: 0.005,
springLength: 130,
springConstant: 0.18
},
maxVelocity: 146,
solver: 'forceAtlas2Based',
timestep: 0.35,
stabilization: {iterations: 50}
}
};
network = new vis.Network(container, data, options);
}