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.

79 lines
2.2 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Basic usage</title>
  5. <script type="text/javascript" src="../../../../dist/vis.js"></script>
  6. <link href="../../../../dist/vis.css" rel="stylesheet" type="text/css" />
  7. <style type="text/css">
  8. #mynetwork {
  9. width: 600px;
  10. height: 400px;
  11. border: 1px solid lightgray;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <p>
  17. Create a simple network with some nodes and edges.
  18. </p>
  19. <input type="button" onclick="addNode()" value="add node dynamically">
  20. <input type="button" onclick="changeNode1()" value="change node 1's color dynamically">
  21. <input type="button" onclick="removeRandomNode()" value="remove a random Node">
  22. <div id="mynetwork"></div>
  23. <script type="text/javascript">
  24. // this list is kept to remove a random node.. we do not add node 1 here because it's used for changes
  25. var nodeIds = [2,3,4,5];
  26. // create an array with nodes
  27. var nodes = new vis.DataSet([
  28. {id: 1, label: 'Node 1'},
  29. {id: 2, label: 'Node 2'},
  30. {id: 3, label: 'Node 3'},
  31. {id: 4, label: 'Node 4'},
  32. {id: 5, label: 'Node 5'}
  33. ]);
  34. // create an array with edges
  35. var edges = new vis.DataSet([
  36. {from: 1, to: 3},
  37. {from: 1, to: 2},
  38. {from: 2, to: 4},
  39. {from: 2, to: 5}
  40. ]);
  41. // create a network
  42. var container = document.getElementById('mynetwork');
  43. var data = {
  44. nodes: nodes,
  45. edges: edges
  46. };
  47. var options = {};
  48. var network = new vis.Network(container, data, options);
  49. function addNode() {
  50. var newId = (Math.random() * 1e7).toString(32);
  51. nodes.add({id:newId, label:"I'm new!"});
  52. nodeIds.push(newId);
  53. }
  54. function changeNode1() {
  55. var newColor = '#' + Math.floor((Math.random() * 255 * 255 * 255)).toString(16);
  56. nodes.update([{id:1, color:{background:newColor}}]);
  57. }
  58. function removeRandomNode() {
  59. var randomNodeId = nodeIds[Math.floor(Math.random() * nodeIds.length)];
  60. nodes.remove({id:randomNodeId});
  61. var index = nodeIds.indexOf(randomNodeId);
  62. nodeIds.splice(index,1);
  63. }
  64. </script>
  65. <script src="../../../googleAnalytics.js"></script>
  66. </body>
  67. </html>