From 05e761abca3ff42dbba371af0560b82707dfe7c0 Mon Sep 17 00:00:00 2001 From: Pliable Pixels Date: Fri, 28 Oct 2016 13:31:36 -0700 Subject: updated vis --- www/lib/vis/examples/network/exampleUtil.js | 133 ++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 www/lib/vis/examples/network/exampleUtil.js (limited to 'www/lib/vis/examples/network/exampleUtil.js') diff --git a/www/lib/vis/examples/network/exampleUtil.js b/www/lib/vis/examples/network/exampleUtil.js new file mode 100644 index 00000000..1621a9a5 --- /dev/null +++ b/www/lib/vis/examples/network/exampleUtil.js @@ -0,0 +1,133 @@ +/** + * 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}; +} + +var randomSeed = 764; // Math.round(Math.random()*1000); +function seededRandom() { + var x = Math.sin(randomSeed++) * 10000; + return x - Math.floor(x); +} + +function getScaleFreeNetworkSeeded(nodeCount, seed) { + if (seed) { + randomSeed = Number(seed); + } + var nodes = []; + var edges = []; + var connectionCount = []; + var edgesId = 0; + + + // 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({ + id: edgesId++, + from: from, + to: to + }); + connectionCount[from]++; + connectionCount[to]++; + } + else if (i > 1) { + var conn = edges.length * 2; + var rand = Math.floor(seededRandom() * 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({ + id: edgesId++, + from: from, + to: to + }); + connectionCount[from]++; + connectionCount[to]++; + } + } + + return {nodes:nodes, edges:edges}; +} \ No newline at end of file -- cgit v1.2.3