tree-indented.html 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>紧凑树</title>
  6. <style>
  7. ::-webkit-scrollbar {
  8. display: none;
  9. }
  10. html,
  11. body {
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="mountNode"></div>
  18. <script>/*Fixing iframe window.innerHeight 0 issue in Safari*/document.body.clientHeight;</script>
  19. <script src="./assets/jquery-3.2.1.min.js"></script>
  20. <script src="../build/g6.js"></script>
  21. <script type="text/javascript">
  22. G6.registerNode('treeNode', {
  23. anchor: [
  24. [ 0, 0.5 ],
  25. [ 0.5, 1 ]
  26. ]
  27. });
  28. G6.registerEdge('VH', {
  29. getPath(item) {
  30. const points = item.getPoints();
  31. const start = points[0];
  32. const end = points[points.length - 1];
  33. return [
  34. [ 'M', start.x, start.y ],
  35. [ 'L', start.x, end.y ],
  36. [ 'L', end.x, end.y ]
  37. ];
  38. }
  39. });
  40. $.getJSON('./assets/data/modeling-methods.json', function (data) {
  41. var layout = new G6.Layouts.IndentedTree({
  42. direction: 'LR', // 方向(LR/RL/H)
  43. indent: 30, // 缩进量
  44. getVGap: function getVGap() /* d */ {
  45. // 竖向间距
  46. return 4;
  47. }
  48. })
  49. var tree = new G6.Tree({
  50. id: 'mountNode', // 容器ID
  51. height: window.innerHeight, // 画布高
  52. layout, // 缩进树布局
  53. fitView: 'autoZoom' // 自动缩放
  54. });
  55. tree.node({
  56. shape: 'treeNode',
  57. size: 16
  58. }).label(function (obj) {
  59. return obj.name;
  60. });
  61. tree.edge({
  62. shape: 'VH'
  63. });
  64. tree.on('afterchange', ()=>{
  65. tree.getNodes().forEach(node=>{
  66. const model = node.getModel();
  67. const label = node.getLabel();
  68. const keyShape = node.getKeyShape();
  69. const children = node.getChildren();
  70. const parent = node.getParent();
  71. const box = keyShape.getBBox();
  72. const labelBox = label.getBBox();
  73. let dx = (box.maxX - box.minX + labelBox.maxX - labelBox.minX) / 2+ 8;
  74. let dy = 0;
  75. label.translate(dx, dy);
  76. });
  77. tree.draw();
  78. });
  79. tree.read({ roots: [data] });
  80. });
  81. </script>
  82. </body>
  83. </html>