Tree.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <template>
  2. <div id="tree-container" :style="`height:${HEIGHT}px`">
  3. <div id="node-container" :style="`height:${HEIGHT}px; width:${WIDTH}px`">
  4. <template v-for="(col, index) in treeNodeColList" :key="index">
  5. <TreeNode
  6. v-for="item in col"
  7. v-bind="item"
  8. :processor="item.processorName"
  9. :priority="item.priority"
  10. :key="item.id"
  11. :position-x="index"
  12. :node-style="NODE_STYLE"
  13. :depth="item.deep"
  14. @addChild="addChild"
  15. @deleteNode="deleteNode"
  16. @nodeClick="nodeClick"
  17. :ref="`treenode-${item.id}`"
  18. />
  19. </template>
  20. <!-- <TreeNode-->
  21. <!-- v-for="requirement in data"-->
  22. <!-- :key="requirement.id"-->
  23. <!-- v-bind="requirement"-->
  24. <!-- :ref="`treenode-${requirement.id}`"-->
  25. <!-- @addChild="addChild"-->
  26. <!-- @deleteNode="deleteNode"-->
  27. <!-- @nodeClick="nodeClick"-->
  28. <!-- />-->
  29. </div>
  30. <canvas id="line-drawer" :height="fitHeight" :width="WIDTH"></canvas>
  31. </div>
  32. </template>
  33. <script>
  34. import TreeNode from './TreeNode.vue';
  35. // 容器宽高
  36. const HEIGHT = 700;
  37. const WIDTH = 1000;
  38. const TREE_MAX_HEIGHT = 4;
  39. const NODE_STYLE = {
  40. width: 250,
  41. height: 140,
  42. padding: 25,
  43. };
  44. export default {
  45. name: 'Tree',
  46. components: { TreeNode },
  47. props: {
  48. data: Array,
  49. },
  50. computed: {
  51. /**
  52. * @returns {*[][]}
  53. */
  54. treeNodeColList() {
  55. // 将点存入map便于调用
  56. const nodeMap = new Map();
  57. this.data.forEach((item) => {
  58. nodeMap.set(item.id, item);
  59. });
  60. // 将array转为树状结构(父组件中已处理无需再处理);顺便拆成col
  61. let rootNode;
  62. const colList = [];
  63. for (let i = 0; i < TREE_MAX_HEIGHT; i += 1) {
  64. colList.push([]);
  65. }
  66. if (this.data.length === 0) {
  67. return colList;
  68. }
  69. this.data.forEach((item) => {
  70. if (item.deep > 0 && item.deep <= TREE_MAX_HEIGHT) {
  71. colList[item.deep - 1].push(item);
  72. } else {
  73. console.warn('需求树数据有误,树高度错误');
  74. }
  75. if (!item.parentId) rootNode = item;
  76. });
  77. // 计算一个节点包含他的后代所需要的空间
  78. const calculateNodeContentHeight = (node) => {
  79. if (!node.childs) {
  80. node.contentHeight = 1;
  81. return 1;
  82. }
  83. node.contentHeight = node.childs.reduce(
  84. (total, current) => total + calculateNodeContentHeight(current),
  85. 0,
  86. );
  87. return node.contentHeight;
  88. };
  89. calculateNodeContentHeight(rootNode);
  90. // 计算节点基准Y轴位置
  91. rootNode.positionY = 0;
  92. for (let i = 1; i < TREE_MAX_HEIGHT; i += 1) {
  93. let sum = 0;
  94. let tempParent = colList[0][0].id;
  95. colList[i].forEach((item) => {
  96. if (item.parentId !== tempParent) {
  97. sum = 0;
  98. tempParent = item.parentId;
  99. }
  100. item.positionY = nodeMap.get(item.parentId).positionY + sum;
  101. sum += item.contentHeight;
  102. });
  103. }
  104. // 计算节点Y轴偏移位置
  105. for (let i = TREE_MAX_HEIGHT - 2; i >= 0; i -= 1) {
  106. colList[i].forEach((item) => {
  107. if (item.childs) {
  108. const { length } = item.childs;
  109. item.positionY = item.childs.reduce((total, current) => total + current.positionY / length, 0);
  110. }
  111. });
  112. }
  113. // 计算所需容器高度
  114. let maxPositionY = 0;
  115. this.data.forEach((item) => {
  116. if (item.positionY && item.positionY > maxPositionY) maxPositionY = item.positionY;
  117. });
  118. this.drawLine((maxPositionY * 140 + 100).toFixed(0));
  119. // 返回col
  120. console.warn(colList);
  121. return colList;
  122. },
  123. },
  124. data() {
  125. return {
  126. HEIGHT,
  127. WIDTH,
  128. TREE_MAX_HEIGHT,
  129. NODE_STYLE,
  130. fitHeight: 0,
  131. ctx: null,
  132. };
  133. },
  134. mounted() {
  135. this.initCanvasCtx();
  136. // this.regenerateTree();
  137. },
  138. methods: {
  139. initCanvasCtx() {
  140. this.ctx = document.getElementById('line-drawer').getContext('2d');
  141. },
  142. drawLine(height) {
  143. this.fitHeight = height > HEIGHT ? height : HEIGHT;
  144. setTimeout(() => {
  145. this.ctx.clearRect(0, 0, WIDTH, height > HEIGHT ? height : HEIGHT);
  146. this.ctx.lineWidth = 1;
  147. this.ctx.strokeStyle = '#bbbbbb';
  148. this.data.forEach((item) => {
  149. if (!item.childs) return;
  150. const parentCard = this.$refs[`treenode-${item.id}`].getTreeNodeSizeAndPosition();
  151. const start = {
  152. x: parentCard.left + parentCard.width,
  153. y: parentCard.top + parentCard.height / 2,
  154. };
  155. item.childs.forEach((child) => {
  156. const childCard = this.$refs[`treenode-${child.id}`].getTreeNodeSizeAndPosition();
  157. const end = {
  158. x: childCard.left,
  159. y: childCard.top + childCard.height / 2,
  160. };
  161. this.ctx.beginPath();
  162. this.ctx.moveTo(start.x, start.y);
  163. this.ctx.lineTo(end.x, end.y);
  164. this.ctx.stroke();
  165. });
  166. });
  167. });
  168. },
  169. addChild(id) {
  170. this.$emit('change', {
  171. type: 'addChildNode',
  172. id,
  173. });
  174. },
  175. deleteNode(id) {
  176. this.$confirm('是否确认删除该节点?', '提示', {
  177. confirmButtonText: '确定',
  178. cancelButtonText: '取消',
  179. type: 'warning',
  180. }).then(() => {
  181. this.$emit('change', {
  182. type: 'deleteTreeNode',
  183. id,
  184. });
  185. }).catch(() => {
  186. this.$message({
  187. type: 'info',
  188. message: '已取消删除',
  189. });
  190. });
  191. },
  192. nodeClick(id) {
  193. this.$emit('node-click', id);
  194. },
  195. },
  196. };
  197. </script>
  198. <style lang="scss">
  199. #tree-container {
  200. border-radius: 4px;
  201. box-shadow: 0 0 3px 0px #BDC3C7;
  202. position: relative;
  203. display: flex;
  204. overflow-y: auto;
  205. justify-content:center;
  206. background: url("https://devcloud.cn-north-4.huaweicloud.com/projectman/plan_back.05189e7a664e6edc2f8e.png") repeat;
  207. }
  208. #node-container {
  209. position:absolute;
  210. z-index: 11;
  211. }
  212. #line-drawer {
  213. position: absolute;
  214. }
  215. .jsmind-inner {
  216. position:relative;
  217. overflow:auto;
  218. width:100%;
  219. height:100%;
  220. user-select:none;
  221. }
  222. canvas {
  223. position:absolute;
  224. z-index:1;
  225. }
  226. jmnodes {
  227. position:absolute;
  228. z-index:2;
  229. background-color:rgba(0,0,0,0);
  230. }
  231. jmnode {
  232. position:absolute;
  233. cursor:default;
  234. max-width:400px;
  235. white-space:nowrap;
  236. overflow:hidden;
  237. text-overflow:ellipsis;
  238. padding:10px;
  239. background-color:#fff;
  240. color:#333;
  241. border-radius:1px;
  242. border: 1px solid #333;
  243. font:16px/1.125 Verdana,Arial,Helvetica,sans-serif;
  244. &:hover {
  245. box-shadow:2px 2px 4px #333;
  246. background-color:#ebebeb;
  247. color:#333;
  248. }
  249. &.selected {
  250. background-color:#11f;
  251. color:#fff;
  252. box-shadow:2px 2px 8px #000;
  253. }
  254. &.root {
  255. font-size:24px;
  256. }
  257. }
  258. jmexpander {
  259. position:absolute;
  260. width:11px;
  261. height:11px;
  262. display:block;
  263. overflow:hidden;
  264. line-height:12px;
  265. font-size:12px;
  266. text-align:center;
  267. border-radius:6px;
  268. border-width:1px;
  269. border-style:solid;
  270. cursor:pointer;
  271. border-color:gray;
  272. &:hover {
  273. border-color:#000;
  274. }
  275. }
  276. @media screen and (max-device-width: 1024px) {
  277. jmnode{padding:5px;border-radius:3px;font-size:14px;}
  278. jmnode.root{font-size:21px;}
  279. }
  280. </style>