vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
2.6 KiB

  1. /**
  2. * Created by Alex on 5/20/2015.
  3. */
  4. function loadJSON(path, success, error) {
  5. var xhr = new XMLHttpRequest();
  6. xhr.onreadystatechange = function () {
  7. if (xhr.readyState === 4) {
  8. if (xhr.status === 200) {
  9. success(JSON.parse(xhr.responseText));
  10. }
  11. else {
  12. error(xhr);
  13. }
  14. }
  15. };
  16. xhr.open('GET', path, true);
  17. xhr.send();
  18. }
  19. function getScaleFreeNetwork(nodeCount) {
  20. var nodes = [];
  21. var edges = [];
  22. var connectionCount = [];
  23. // randomly create some nodes and edges
  24. for (var i = 0; i < nodeCount; i++) {
  25. nodes.push({
  26. id: i,
  27. label: String(i)
  28. });
  29. connectionCount[i] = 0;
  30. // create edges in a scale-free-network way
  31. if (i == 1) {
  32. var from = i;
  33. var to = 0;
  34. edges.push({
  35. from: from,
  36. to: to
  37. });
  38. connectionCount[from]++;
  39. connectionCount[to]++;
  40. }
  41. else if (i > 1) {
  42. var conn = edges.length * 2;
  43. var rand = Math.floor(Math.random() * conn);
  44. var cum = 0;
  45. var j = 0;
  46. while (j < connectionCount.length && cum < rand) {
  47. cum += connectionCount[j];
  48. j++;
  49. }
  50. var from = i;
  51. var to = j;
  52. edges.push({
  53. from: from,
  54. to: to
  55. });
  56. connectionCount[from]++;
  57. connectionCount[to]++;
  58. }
  59. }
  60. return {nodes:nodes, edges:edges};
  61. }
  62. var randomSeed = 764; // Math.round(Math.random()*1000);
  63. function seededRandom() {
  64. var x = Math.sin(randomSeed++) * 10000;
  65. return x - Math.floor(x);
  66. }
  67. function getScaleFreeNetworkSeeded(nodeCount, seed) {
  68. if (seed) {
  69. randomSeed = Number(seed);
  70. }
  71. var nodes = [];
  72. var edges = [];
  73. var connectionCount = [];
  74. var edgesId = 0;
  75. // randomly create some nodes and edges
  76. for (var i = 0; i < nodeCount; i++) {
  77. nodes.push({
  78. id: i,
  79. label: String(i)
  80. });
  81. connectionCount[i] = 0;
  82. // create edges in a scale-free-network way
  83. if (i == 1) {
  84. var from = i;
  85. var to = 0;
  86. edges.push({
  87. id: edgesId++,
  88. from: from,
  89. to: to
  90. });
  91. connectionCount[from]++;
  92. connectionCount[to]++;
  93. }
  94. else if (i > 1) {
  95. var conn = edges.length * 2;
  96. var rand = Math.floor(seededRandom() * conn);
  97. var cum = 0;
  98. var j = 0;
  99. while (j < connectionCount.length && cum < rand) {
  100. cum += connectionCount[j];
  101. j++;
  102. }
  103. var from = i;
  104. var to = j;
  105. edges.push({
  106. id: edgesId++,
  107. from: from,
  108. to: to
  109. });
  110. connectionCount[from]++;
  111. connectionCount[to]++;
  112. }
  113. }
  114. return {nodes:nodes, edges:edges};
  115. }