diff options
| author | Pliable Pixels <pliablepixels@gmail.com> | 2017-09-21 12:49:18 -0400 |
|---|---|---|
| committer | Pliable Pixels <pliablepixels@gmail.com> | 2017-09-21 12:49:18 -0400 |
| commit | b28028ac4082842143b0f528d6bc539da6ccb419 (patch) | |
| tree | 1e26ea969a781ed8e323fca4e3c76345113fc694 /www/lib/vis/examples/network/exampleUtil.js | |
| parent | 676270d21beed31d767a06c89522198c77d5d865 (diff) | |
mega changes, including updates and X
Diffstat (limited to 'www/lib/vis/examples/network/exampleUtil.js')
| -rw-r--r-- | www/lib/vis/examples/network/exampleUtil.js | 133 |
1 files changed, 133 insertions, 0 deletions
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 |
