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.

153 lines
4.8 KiB

  1. // some global vars
  2. var network;
  3. var connectedNodes = [];
  4. var largeNode = undefined;
  5. var NORMAL_SIZE = 60;
  6. var LARGE_SIZE = 60;
  7. var IMAGE_PATH = './images/exampleScreenshots/';
  8. function loadVis(SPACING) {
  9. // getting the sizes of the containers
  10. var linksContainer = document.getElementById('contentContainer');
  11. var networkContainer = document.getElementById('networkContainer');
  12. var linksContainerRect = linksContainer.getBoundingClientRect();
  13. var linksContainerHeight = linksContainerRect.bottom - linksContainerRect.top;
  14. networkContainer.style.height = (linksContainerHeight + LARGE_SIZE) + 'px';
  15. linksContainer.style.marginTop = '-' + (linksContainerHeight + LARGE_SIZE) + 'px';
  16. // constructing nodes and edges from links with class exampleLink
  17. var links = linksContainer.getElementsByTagName('a');
  18. var nodesArray = [];
  19. var edgesArray = [];
  20. var idCounter = 0;
  21. var firstLink = undefined;
  22. for (var i = 0; i < links.length; i++) {
  23. var link = links[i];
  24. if (link.className === 'exampleLink') {
  25. var exampleName = link.getAttribute('href').replace('.html','').replace('examples/','');
  26. var linkRect = link.getBoundingClientRect();
  27. var linkWidth = linkRect.right - linkRect.left;
  28. if (firstLink === undefined) {
  29. firstLink = link;
  30. }
  31. nodesArray.push({id:idCounter,
  32. x:link.offsetLeft + linkWidth,
  33. y:link.offsetTop,
  34. color:{
  35. background:'rgba(0,0,0,0.0)',
  36. border:'rgba(70,158,255,0)'
  37. },
  38. shape:'dot',
  39. size:2,
  40. fixed:true
  41. });
  42. nodesArray.push({id:idCounter + 'image',
  43. x:link.offsetLeft + 400 + 100 + (idCounter % 3) * SPACING,
  44. y:link.offsetTop,
  45. color:{
  46. border:'rgba(70,158,255,1)'
  47. },
  48. shadow:{
  49. size:2,
  50. x:0,
  51. y:0
  52. },
  53. shape:'image',
  54. image:IMAGE_PATH+(exampleName)+'.png',
  55. size:NORMAL_SIZE
  56. });
  57. edgesArray.push({from: idCounter, to: idCounter+'image', arrows:'from'});
  58. idCounter += 1;
  59. }
  60. }
  61. // making a dataset.
  62. var nodes = new vis.DataSet(nodesArray);
  63. var edges = new vis.DataSet(edgesArray);
  64. // creating a network
  65. var networkContainer = document.getElementById('networkContainer');
  66. var data = {
  67. nodes: nodes,
  68. edges: edges
  69. };
  70. var options = {
  71. edges:{
  72. color:{inherit:'both'},
  73. smooth:false
  74. },
  75. physics: false,
  76. interaction:{
  77. zoomView:false,
  78. dragView: false
  79. }
  80. };
  81. network = new vis.Network(networkContainer, data, options);
  82. // get the offset or the camera
  83. var firstLinkPos = {x:firstLink.offsetLeft, y:firstLink.offsetTop};
  84. var firstLinkRect = firstLink.getBoundingClientRect();
  85. var firstLinkWidth = firstLinkRect.right - firstLinkRect.left;
  86. var firstLinkHeight = firstLinkRect.bottom - firstLinkRect.top;
  87. var networkContainerRect = networkContainer.getBoundingClientRect();
  88. var networkContainerWidth = networkContainerRect.right - networkContainerRect.left;
  89. var networkContainerHeight = networkContainerRect.bottom - networkContainerRect.top;
  90. var ydiff = linksContainer.offsetTop - networkContainer.offsetTop;
  91. // move the camera
  92. network.moveTo({
  93. scale: 1,
  94. position: network.getPositions([0])[0],
  95. offset: {
  96. x: -0.5 * networkContainerWidth + firstLinkPos.x + firstLinkWidth,
  97. y: -0.5 * networkContainerHeight + firstLinkPos.y + ydiff + 0.5 * firstLinkHeight
  98. },
  99. animation: false
  100. });
  101. // onclick handler
  102. linksContainer.onclick = function (event) {
  103. var nodeUnderCursor = network.getNodeAt({x:event.layerX, y:event.layerY+ydiff});
  104. if (nodeUnderCursor !== undefined) {
  105. var url = nodes.get(nodeUnderCursor).image.replace(IMAGE_PATH, './examples/').replace(".png",".html");
  106. window.location.href = url;
  107. }
  108. }
  109. // throttled mouse move handler
  110. var t0 = new Date().valueOf();
  111. linksContainer.onmousemove = function (event) {
  112. if (new Date().valueOf() - t0 > 60) {
  113. handleMouse(event);
  114. t0 = new Date().valueOf();
  115. }
  116. }
  117. // get a node at a position, select the node, update the dataset
  118. function handleMouse(event) {
  119. var nodeUnderCursor = network.getNodeAt({x:event.layerX, y:event.layerY+ydiff});
  120. if (connectedNodes.length > 0) {
  121. nodes.update([{id: connectedNodes[0], color: {border: 'rgba(70,158,255,0)'}}])
  122. connectedNodes = []
  123. }
  124. if (largeNode !== undefined) {
  125. nodes.update([{id: largeNode, size: NORMAL_SIZE}]);
  126. largeNode = undefined;
  127. }
  128. if (nodeUnderCursor !== undefined) {
  129. connectedNodes = network.getConnectedNodes(nodeUnderCursor);
  130. largeNode = nodeUnderCursor;
  131. nodes.update([{id: largeNode, size: LARGE_SIZE}, {id: connectedNodes[0],color: {border: 'rgba(70,158,255,1)'}}]);
  132. network.selectNodes([nodeUnderCursor]);
  133. }
  134. else {
  135. network.unselectAll();
  136. }
  137. }
  138. }