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.

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