-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdraw-graph.js
97 lines (86 loc) · 2.37 KB
/
draw-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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
function submit() {
draw_graph();
// set URL Parameters
var directed = $("#directed").prop('checked')?'1':'0';
var ignore = $("#ignore").prop('checked')?'1':'0';
var data = $("#data").val();
var url = location.pathname+"?"+"data="+encodeURIComponent(data).replace(/%20/g,'+')+"&directed="+directed+"&ignore="+ignore;
window.history.replaceState("","",url);
}
function draw_graph() {
var directed = $("#directed").prop('checked');
var ignore = $("#ignore").prop('checked');
var data = $("#data").val();
data = data.split('\n');
if (ignore && data.length >= 1) {
data[0] = "";
}
for (var i in data) {
data[i] = data[i].trim().split(/ +/);
if (data[i].length >= 2) {
data[i][0] = data[i][0];
data[i][1] = data[i][1];
}
}
//create nodes
var nodes_set = new Set();
for (var i in data) {
if (data[i].length < 2) continue;
nodes_set.add(data[i][0]);
nodes_set.add(data[i][1]);
}
var nodes = [];
for (var node of nodes_set) {
nodes.push({id: node, label: node});
}
// create edges
var edges = [];
for (var i in data) {
if (data[i].length < 2) continue;
var edge = {from: data[i][0], to: data[i][1]};
if (data[i].length >= 3) {
edge.label = data[i][2];
}
if (directed) {
edge.arrows = 'to';
}
edges.push(edge);
}
// create a network
var container = document.getElementById('graph');
var data = {
nodes: new vis.DataSet(nodes),
edges: new vis.DataSet(edges)
};
var options = {};
var network = new vis.Network(container, data, options);
}
$(document).ready(function() {
// load URL Parameters
if (getUrlParameter("ignore")) {
$("#ignore").prop('checked',getUrlParameter("ignore")=="1");
}
if (getUrlParameter("directed")) {
$("#directed").prop('checked',getUrlParameter("directed")=="1");
}
if (getUrlParameter("data")) {
$("#data").val(getUrlParameter("data"));
draw_graph();
}
// Ctrl / Cmd + Enter
$("#data").keydown(function(e) {
if ((e.ctrlKey || e.metaKey) && e.keyCode == 13) {
submit();
}
});
});
var getUrlParameter = function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1).replace(/\+/g, '%20'));
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++) {
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
};