Tree.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <div id="tree-container" :style="`height:${HEIGHT}px`">
  3. <div id="node-container" :style="`height:${HEIGHT}px; width:${WIDTH}px`">
  4. <TreeNode
  5. v-for="requirement in data"
  6. :key="requirement.id"
  7. v-bind="requirement"
  8. :ref="`treenode-${requirement.id}`"
  9. @addChild="addChild"
  10. @deleteNode="deleteNode"
  11. @nodeClick="nodeClick"
  12. />
  13. </div>
  14. <canvas id="line-drawer" :height="HEIGHT" :width="WIDTH"></canvas>
  15. </div>
  16. </template>
  17. <script>
  18. import TreeNode from './TreeNode.vue';
  19. // 容器宽高
  20. const HEIGHT = 600;
  21. const WIDTH = 1100;
  22. export default {
  23. name: 'Tree',
  24. components: { TreeNode },
  25. props: {
  26. data: Array,
  27. },
  28. data() {
  29. return {
  30. HEIGHT,
  31. WIDTH,
  32. parent2children: {},
  33. depth2node: {},
  34. layout: {},
  35. ctx: null,
  36. };
  37. },
  38. mounted() {
  39. this.initCanvasCtx();
  40. this.regenerateTree();
  41. },
  42. methods: {
  43. initCanvasCtx() {
  44. this.ctx = document.getElementById('line-drawer').getContext('2d');
  45. this.ctx.lineWidth = 1;
  46. this.ctx.strokeStyle = '#bbbbbb';
  47. },
  48. regenerateTree() {
  49. this.initParent2Children();
  50. this.initDepth2Node();
  51. this.calculateLayout();
  52. this.locateAndDraw();
  53. },
  54. initParent2Children() {
  55. this.parent2children = {};
  56. for (let i = 0; i < this.data.length; i += 1) {
  57. const parentid = this.data[i].parent;
  58. if (!Object.prototype.hasOwnProperty.call(this.parent2children, parentid)) {
  59. this.parent2children[parentid] = [];
  60. }
  61. this.parent2children[parentid].push(this.data[i]);
  62. }
  63. },
  64. initDepth2Node() {
  65. this.depth2node = {};
  66. let depth = 1;
  67. let parentList = [null];
  68. for (;depth < 5; depth += 1) {
  69. this.depth2node[depth] = [];
  70. for (let i = 0; i < this.data.length; i += 1) {
  71. if (parentList.includes(this.data[i].parent)) {
  72. this.depth2node[depth].push(this.data[i]);
  73. }
  74. }
  75. parentList = this.depth2node[depth].map((node) => node.id);
  76. }
  77. },
  78. calculateLayout() {
  79. // clear the layout
  80. this.layout = {};
  81. const MARGIN_TOP = 40;
  82. const MARGIN_LEFT = 100;
  83. const COL_WIDTH = (WIDTH - MARGIN_LEFT) / 4;
  84. for (let depth = 4; depth > 0; depth -= 1) {
  85. const offsetLeft = MARGIN_LEFT + (COL_WIDTH * (depth * 2 - 1)) / 2;
  86. let offsetTop = MARGIN_TOP;
  87. for (let i = 0; i < this.depth2node[depth].length; i += 1) {
  88. const treenode = this.$refs[`treenode-${this.depth2node[depth][i].id}`];
  89. const { width, height } = treenode.getTreeNodeSize();
  90. const offsetX = offsetLeft - width / 2;
  91. let offsetY;
  92. if (depth === 4) {
  93. offsetY = offsetTop;
  94. offsetTop += height + MARGIN_TOP;
  95. } else if (Object.prototype.hasOwnProperty.call(this.parent2children, this.depth2node[depth][i].id)) {
  96. const childrenList = this.parent2children[this.depth2node[depth][i].id].map((node) => node.id);
  97. offsetY = childrenList.reduce(
  98. (sum, curr) => sum + this.layout[curr].y + this.layout[curr].height / 2,
  99. 0,
  100. ) / childrenList.length - height / 2;
  101. } else {
  102. offsetY = offsetTop;
  103. }
  104. offsetTop = offsetY + height + MARGIN_TOP;
  105. this.layout[this.depth2node[depth][i].id] = {
  106. x: offsetX,
  107. y: offsetY,
  108. width,
  109. height,
  110. };
  111. }
  112. }
  113. },
  114. locateAndDraw() {
  115. // 定位TreeNodeContainer
  116. // eslint-disable-next-line no-restricted-syntax
  117. for (const id of Object.keys(this.layout)) {
  118. const { x, y } = this.layout[id];
  119. this.$refs[`treenode-${id}`].setTreeNodeOffset(x, y);
  120. }
  121. // clear the canvas
  122. this.ctx.clearRect(0, 0, WIDTH, HEIGHT);
  123. // 在canvas上绘制节点间连线
  124. const kvs = Object.entries(this.parent2children);
  125. // eslint-disable-next-line no-restricted-syntax
  126. for (const [parent, children] of kvs) {
  127. if (parent === 'null') {
  128. // eslint-disable-next-line no-continue
  129. continue;
  130. }
  131. const {
  132. x: startX, y: startY, width: startW, height: startH,
  133. } = this.layout[parent];
  134. const start = {
  135. x: startX + startW,
  136. y: startY + startH / 2,
  137. };
  138. // eslint-disable-next-line no-restricted-syntax
  139. for (const child of children) {
  140. const {
  141. x: endX, y: endY, height: endH,
  142. } = this.layout[child.id];
  143. const end = {
  144. x: endX,
  145. y: endY + endH / 2,
  146. };
  147. this.ctx.beginPath();
  148. this.ctx.moveTo(start.x, start.y);
  149. this.ctx.lineTo(end.x, end.y);
  150. this.ctx.stroke();
  151. }
  152. }
  153. },
  154. addChild(id) {
  155. this.$emit('change', {
  156. type: 'addChild',
  157. id,
  158. });
  159. },
  160. deleteNode(id) {
  161. this.$emit('change', {
  162. type: 'deleteNode',
  163. id,
  164. });
  165. },
  166. nodeClick(id) {
  167. this.$emit('node-click', id);
  168. },
  169. },
  170. };
  171. </script>
  172. <style lang="scss">
  173. #tree-container {
  174. display: flex;
  175. justify-content:center;
  176. }
  177. #node-container {
  178. position:absolute;
  179. z-index: 11;
  180. }
  181. #line-drawer {
  182. position: absolute;
  183. }
  184. .jsmind-inner {
  185. position:relative;
  186. overflow:auto;
  187. width:100%;
  188. height:100%;
  189. user-select:none;
  190. }
  191. canvas {
  192. position:absolute;
  193. z-index:1;
  194. }
  195. jmnodes {
  196. position:absolute;
  197. z-index:2;
  198. background-color:rgba(0,0,0,0);
  199. }
  200. jmnode {
  201. position:absolute;
  202. cursor:default;
  203. max-width:400px;
  204. white-space:nowrap;
  205. overflow:hidden;
  206. text-overflow:ellipsis;
  207. padding:10px;
  208. background-color:#fff;
  209. color:#333;
  210. border-radius:1px;
  211. border: 1px solid #333;
  212. font:16px/1.125 Verdana,Arial,Helvetica,sans-serif;
  213. &:hover {
  214. box-shadow:2px 2px 4px #333;
  215. background-color:#ebebeb;
  216. color:#333;
  217. }
  218. &.selected {
  219. background-color:#11f;
  220. color:#fff;
  221. box-shadow:2px 2px 8px #000;
  222. }
  223. &.root {
  224. font-size:24px;
  225. }
  226. }
  227. jmexpander {
  228. position:absolute;
  229. width:11px;
  230. height:11px;
  231. display:block;
  232. overflow:hidden;
  233. line-height:12px;
  234. font-size:12px;
  235. text-align:center;
  236. border-radius:6px;
  237. border-width:1px;
  238. border-style:solid;
  239. cursor:pointer;
  240. border-color:gray;
  241. &:hover {
  242. border-color:#000;
  243. }
  244. }
  245. @media screen and (max-device-width: 1024px) {
  246. jmnode{padding:5px;border-radius:3px;font-size:14px;}
  247. jmnode.root{font-size:21px;}
  248. }
  249. </style>