index.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <head>
  2. <style> body { margin: 0; } </style>
  3. <script src="//unpkg.com/dat.gui"></script>
  4. <script src="//unpkg.com/3d-force-graph"></script>
  5. <!-- <script src="../../dist/3d-force-graph.js"></script> -->
  6. </head>
  7. <body>
  8. <div id="3d-graph"></div>
  9. <script>
  10. // Create Random tree
  11. const N = 20;
  12. const gData = {
  13. nodes: [...Array(N).keys()].map(i => ({ id: i })),
  14. links: [...Array(N).keys()]
  15. .filter(id => id)
  16. .map(id => ({
  17. source: id,
  18. target: Math.round(Math.random() * (id-1)),
  19. color: id % 2
  20. }))
  21. };
  22. const graph = ForceGraph3D()
  23. (document.getElementById('3d-graph'))
  24. .nodeLabel(node => node.id)
  25. .linkColor(link => link.color ? 'red' : 'green' )
  26. .linkOpacity(1)
  27. .graphData(gData);
  28. const linkForce = graph
  29. .d3Force('link')
  30. .distance(link => link.color ? settings.redDistance : settings.greenDistance);
  31. //Define GUI
  32. const Settings = function() {
  33. this.redDistance = 20;
  34. this.greenDistance = 20;
  35. };
  36. const settings = new Settings();
  37. const gui = new dat.GUI();
  38. const controllerOne = gui.add(settings, 'redDistance', 0, 100);
  39. const controllerTwo = gui.add(settings, 'greenDistance', 0, 100);
  40. controllerOne.onChange(updateLinkDistance);
  41. controllerTwo.onChange(updateLinkDistance);
  42. function updateLinkDistance() {
  43. linkForce.distance(link => link.color ? settings.redDistance : settings.greenDistance);
  44. graph.numDimensions(3); // Re-heat simulation
  45. }
  46. </script>
  47. </body>