tree-compact-box.html 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. [ 1, 0.5 ]
  26. ]
  27. });
  28. G6.registerEdge('smooth', {
  29. getPath(item) {
  30. const points = item.getPoints();
  31. const start = points[0];
  32. const end = points[points.length - 1];
  33. const hgap = Math.abs(end.x - start.x);
  34. if (end.x > start.x) {
  35. return [
  36. [ 'M', start.x, start.y ],
  37. [ 'C', start.x + hgap / 4, start.y, end.x - hgap / 2, end.y, end.x, end.y ]
  38. ];
  39. }
  40. return [
  41. [ 'M', start.x, start.y ],
  42. [ 'C', start.x - hgap / 4, start.y, end.x + hgap / 2, end.y, end.x, end.y ]
  43. ];
  44. }
  45. });
  46. $.getJSON('./assets/data/modeling-methods.json', function (data) {
  47. var layout = new G6.Layouts.CompactBoxTree({
  48. // direction: 'LR', // 方向(LR/RL/H/TB/BT/V)
  49. getHGap: function getHGap() /* d */ {
  50. // 横向间距
  51. return 100;
  52. },
  53. getVGap: function getVGap() /* d */ {
  54. // 竖向间距
  55. return 10;
  56. }
  57. })
  58. var tree = new G6.Tree({
  59. id: 'mountNode', // 容器ID
  60. height: window.innerHeight, // 画布高
  61. layout,
  62. fitView: 'autoZoom' // 自动缩放
  63. });
  64. tree.node({
  65. shape: 'treeNode',
  66. size: 8
  67. }).label(function (obj) {
  68. return obj.name;
  69. });
  70. tree.edge({
  71. shape: 'smooth'
  72. });
  73. tree.on('afterchange', ()=>{
  74. tree.getNodes().forEach(node=>{
  75. const model = node.getModel();
  76. const label = node.getLabel();
  77. const keyShape = node.getKeyShape();
  78. const children = node.getChildren();
  79. const parent = node.getParent();
  80. const box = keyShape.getBBox();
  81. const labelBox = label.getBBox();
  82. let dx = (box.maxX - box.minX + labelBox.maxX - labelBox.minX) / 2+ 8;
  83. let dy = 0;
  84. if (children.length != 0) {
  85. dx = -dx;
  86. }
  87. label.translate(dx, dy);
  88. });
  89. tree.draw();
  90. });
  91. tree.read({ roots: [data] });
  92. });
  93. </script>
  94. </body>
  95. </html>