-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathexample-graph.html
66 lines (58 loc) · 2.36 KB
/
example-graph.html
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
<html>
<head>
<title>
Graph
</title>
<script src="../third-party-libs/vis/vis.js" type="text/javascript"></script>
<script src="../src/jsgraphs.js" type="text/javascript"></script>
<link href="../third-party-libs/vis/vis.css" type="text/css" />
</head>
<body>
<h2>Graph</h2>
<div id="mynetwork"></div>
<script type="text/javascript">
(function(){
var g = new jsgraphs.Graph(6); // 6 is the number vertices in the graph
g.addEdge(0, 5); // add undirected edge connecting vertex 0 to vertex 5
g.addEdge(2, 4);
g.addEdge(2, 3);
g.addEdge(1, 2);
g.addEdge(0, 1);
g.addEdge(3, 4);
g.addEdge(3, 5);
g.addEdge(0, 2);
var g_nodes = [];
var g_edges = [];
for(var v=0; v < g.V; ++v){
g.node(v).label = 'Node ' + v; // assigned 'Node {v}' as label for node v
g_nodes.push({
id: v,
label: g.node(v).label
});
var adj_v = g.adj(v);
for(var i = 0; i < adj_v.length; ++i) {
var w = adj_v[i];
if(w > v) continue; // make sure only one edge between w and v since the graph is undirected
g_edges.push({
from: v,
to: w
});
};
}
console.log(g.V); // display 6, which is the number of vertices in g
console.log(g.adj(0)); // display [5, 1, 2], which is the adjacent list to vertex 0
var nodes = new vis.DataSet(g_nodes);
// create an array with edges
var edges = new vis.DataSet(g_edges);
// create a network
var container = document.getElementById('mynetwork');
var data = {
nodes: nodes,
edges: edges
};
var options = {};
var network = new vis.Network(container, data, options);
})();
</script>
</body>
</html>