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.

126 lines
2.5 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) {
  68. var nodes = [];
  69. var edges = [];
  70. var connectionCount = [];
  71. randomSeed = 764;
  72. // randomly create some nodes and edges
  73. for (var i = 0; i < nodeCount; i++) {
  74. nodes.push({
  75. id: i,
  76. label: String(i)
  77. });
  78. connectionCount[i] = 0;
  79. // create edges in a scale-free-network way
  80. if (i == 1) {
  81. var from = i;
  82. var to = 0;
  83. edges.push({
  84. from: from,
  85. to: to
  86. });
  87. connectionCount[from]++;
  88. connectionCount[to]++;
  89. }
  90. else if (i > 1) {
  91. var conn = edges.length * 2;
  92. var rand = Math.floor(seededRandom() * conn);
  93. var cum = 0;
  94. var j = 0;
  95. while (j < connectionCount.length && cum < rand) {
  96. cum += connectionCount[j];
  97. j++;
  98. }
  99. var from = i;
  100. var to = j;
  101. edges.push({
  102. from: from,
  103. to: to
  104. });
  105. connectionCount[from]++;
  106. connectionCount[to]++;
  107. }
  108. }
  109. return {nodes:nodes, edges:edges};
  110. }