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.

69 lines
1.3 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. }