index.html 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <head>
  2. <style> body { margin: 0; } </style>
  3. <script src="//unpkg.com/d3-dsv"></script>
  4. <script src="//unpkg.com/dat.gui"></script>
  5. <script src="//unpkg.com/d3-octree"></script>
  6. <script src="//unpkg.com/d3-force-3d"></script>
  7. <script src="//unpkg.com/3d-force-graph"></script>
  8. <!--<script src="../../dist/3d-force-graph.js"></script>-->
  9. </head>
  10. <body>
  11. <div id="graph"></div>
  12. <script>
  13. // controls
  14. const controls = { 'DAG Orientation': 'td'};
  15. const gui = new dat.GUI();
  16. gui.add(controls, 'DAG Orientation', ['td', 'bu', 'lr', 'rl', 'zout', 'zin', 'radialout', 'radialin', null])
  17. .onChange(orientation => graph && graph.dagMode(orientation));
  18. // graph config
  19. const NODE_REL_SIZE = 1;
  20. const graph = ForceGraph3D()
  21. .dagMode('td')
  22. .dagLevelDistance(200)
  23. .backgroundColor('#101020')
  24. .linkColor(() => 'rgba(255,255,255,0.2)')
  25. .nodeRelSize(NODE_REL_SIZE)
  26. .nodeId('path')
  27. .nodeVal('size')
  28. .nodeLabel('path')
  29. .nodeAutoColorBy('module')
  30. .nodeOpacity(0.9)
  31. .linkDirectionalParticles(2)
  32. .linkDirectionalParticleWidth(0.8)
  33. .linkDirectionalParticleSpeed(0.006)
  34. .d3Force('collision', d3.forceCollide(node => Math.cbrt(node.size) * NODE_REL_SIZE))
  35. .d3VelocityDecay(0.3);
  36. // Decrease repel intensity
  37. graph.d3Force('charge').strength(-15);
  38. fetch('../datasets/d3-dependencies.csv')
  39. .then(r => r.text())
  40. .then(d3.csvParse)
  41. .then(data => {
  42. const nodes = [], links = [];
  43. data.forEach(({ size, path }) => {
  44. const levels = path.split('/'),
  45. level = levels.length - 1,
  46. module = level > 0 ? levels[1] : null,
  47. leaf = levels.pop(),
  48. parent = levels.join('/');
  49. const node = {
  50. path,
  51. leaf,
  52. module,
  53. size: +size || 20,
  54. level
  55. };
  56. nodes.push(node);
  57. if (parent) {
  58. links.push({source: parent, target: path, targetNode: node});
  59. }
  60. });
  61. graph(document.getElementById('graph'))
  62. .graphData({ nodes, links });
  63. });
  64. </script>
  65. </body>