diff --git a/js/dos/ext/vis/examples/network/exampleUtil.js b/js/dos/ext/vis/examples/network/exampleUtil.js new file mode 100644 index 0000000..a5554ce --- /dev/null +++ b/js/dos/ext/vis/examples/network/exampleUtil.js @@ -0,0 +1,70 @@ +/** + * Created by Alex on 5/20/2015. + */ + +function loadJSON(path, success, error) { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function () { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + success(JSON.parse(xhr.responseText)); + } + else { + error(xhr); + } + } + }; + xhr.open('GET', path, true); + xhr.send(); +} + + +function getScaleFreeNetwork(nodeCount) { + var nodes = []; + var edges = []; + var connectionCount = []; + + // randomly create some nodes and edges + for (var i = 0; i < nodeCount; i++) { + nodes.push({ + id: i, + label: String(i) + }); + + connectionCount[i] = 0; + + // create edges in a scale-free-network way + if (i == 1) { + var from = i; + var to = 0; + edges.push({ + from: from, + to: to + }); + connectionCount[from]++; + connectionCount[to]++; + } + else if (i > 1) { + var conn = edges.length * 2; + var rand = Math.floor(Math.random() * conn); + var cum = 0; + var j = 0; + while (j < connectionCount.length && cum < rand) { + cum += connectionCount[j]; + j++; + } + + + var from = i; + var to = j; + edges.push({ + from: from, + to: to + }); + connectionCount[from]++; + connectionCount[to]++; + } + } + + return {nodes:nodes, edges:edges}; +} \ No newline at end of file