From d06a0e15e88051d1cb1f6ce3bace2f8b2c5e819d Mon Sep 17 00:00:00 2001 From: cjc <431909623@qq.com> Date: Fri, 11 Mar 2022 17:07:09 +0800 Subject: [PATCH] =?UTF-8?q?=20=E4=BF=AE=E6=94=B9=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E5=9B=9E=E6=8C=87=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/Layouter/BinaryTree.js | 231 ++++++++++++++++++------------------ demo/data.js | 132 +++++++++------------ 2 files changed, 168 insertions(+), 195 deletions(-) diff --git a/demo/Layouter/BinaryTree.js b/demo/Layouter/BinaryTree.js index 3abff47..8610928 100644 --- a/demo/Layouter/BinaryTree.js +++ b/demo/Layouter/BinaryTree.js @@ -1,99 +1,101 @@ SV.registerLayout('BinaryTree', { - defineOptions() { - return { - node: { - default: { - type: 'binary-tree-node', - size: [60, 30], - label: '[id]', - style: { - fill: '#b83b5e', - stroke: '#333', - cursor: 'pointer', - }, - }, - }, - link: { - child: { - type: 'line', - sourceAnchor: index => (index === 0 ? 3 : 1), - targetAnchor: 0, - style: { - stroke: '#333', - lineAppendWidth: 6, - cursor: 'pointer', - endArrow: 'default', - startArrow: { - path: G6.Arrow.circle(2, -1), - fill: '#333', - }, - }, - }, - }, - marker: { - external: { - type: 'pointer', - anchor: 0, - offset: 14, - style: { - fill: '#f08a5d', - }, - labelOptions: { - style: { - fill: '#000099', - }, - }, - }, - }, - layout: { - xInterval: 40, - yInterval: 40, - }, - behavior: { - // dragNode: false - }, - }; - }, + defineOptions() { + return { + node: { + default: { + type: 'binary-tree-node', + size: [60, 30], + label: '[data]', + style: { + fill: '#b83b5e', + stroke: '#333', + cursor: 'pointer', + }, + }, + }, + link: { + child: { + type: 'line', + sourceAnchor: index => (index === 0 ? 3 : 1), + targetAnchor: 0, + style: { + stroke: '#333', + lineAppendWidth: 6, + cursor: 'pointer', + endArrow: 'default', + startArrow: { + path: G6.Arrow.circle(2, -1), + fill: '#333', + }, + }, + }, + }, + marker: { + external: { + type: 'pointer', + anchor: 0, + offset: 14, + style: { + fill: '#f08a5d', + }, + labelOptions: { + style: { + fill: '#000099', + }, + }, + }, + }, + layout: { + xInterval: 40, + yInterval: 40, + }, + behavior: { + // dragNode: false + }, + }; + }, - /** - * 对子树进行递归布局 - */ - layoutItem(node, layoutOptions) { - // 次双亲不进行布局 - if (!node) { - return null; - } + /** + * 对子树进行递归布局 + */ + layoutItem(node, layoutOptions) { + // 次双亲不进行布局 + if (!node) { + return null; + } - let bound = node.getBound(), - width = bound.width, - height = bound.height, - group = new Group(node), - leftGroup = null, - rightGroup = null, + let bound = node.getBound(), + width = bound.width, + height = bound.height, + group = new Group(node), + leftGroup = null, + rightGroup = null, leftBound = null, rightBound = null; - if (node.visited) { - return null; - } + if (node.visited) { + return null; + } - if (node.child && node.child[0]) { - leftGroup = this.layoutItem(node.child[0], layoutOptions); - } + node.visited = true; - if (node.child && node.child[1]) { - rightGroup = this.layoutItem(node.child[1], layoutOptions); - } + if (node.child && node.child[0]) { + leftGroup = this.layoutItem(node.child[0], layoutOptions); + } + + if (node.child && node.child[1]) { + rightGroup = this.layoutItem(node.child[1], layoutOptions); + } if (leftGroup) { leftBound = leftGroup.getBound(); node.set('y', leftBound.y - layoutOptions.yInterval - height); } - if(rightGroup) { + if (rightGroup) { rightBound = rightGroup.getBound(); - if(leftGroup) { + if (leftGroup) { rightGroup.translate(0, leftBound.y - rightBound.y) } @@ -102,55 +104,52 @@ SV.registerLayout('BinaryTree', { } // 处理左右子树相交问题 - if (leftGroup && rightGroup) { - let move = Math.abs(rightBound.x - layoutOptions.xInterval - leftBound.x - leftBound.width); - if (move > 0) { - leftGroup.translate(-move / 2, 0); - rightGroup.translate(move / 2, 0); - } - } + if (leftGroup && rightGroup) { + let move = Math.abs(rightBound.x - layoutOptions.xInterval - leftBound.x - leftBound.width); + if (move > 0) { + leftGroup.translate(-move / 2, 0); + rightGroup.translate(move / 2, 0); + } + } if (leftGroup) { leftBound = leftGroup.getBound(); node.set('x', leftBound.x + leftBound.width + layoutOptions.xInterval / 2 - width); } - if(rightGroup) { + if (rightGroup) { rightBound = rightGroup.getBound(); node.set('x', rightBound.x - layoutOptions.xInterval / 2 - width); } - node.visited = true; - if (leftGroup) { - group.add(leftGroup); - } + if (leftGroup) { + group.add(leftGroup); + } - if (rightGroup) { - group.add(rightGroup); - } + if (rightGroup) { + group.add(rightGroup); + } - return group; - }, + return group; + }, - /** - * 布局函数 - * @param {*} elements - * @param {*} layoutOptions - */ - layout(elements, layoutOptions) { - let root = elements[0]; - this.layoutItem(root, layoutOptions); - }, + /** + * 布局函数 + * @param {*} elements + * @param {*} layoutOptions + */ + layout(elements, layoutOptions) { + let root = elements[0]; + this.layoutItem(root, layoutOptions); + }, }); -[ - { - id: 6385328, - data: '', - external: ['L'], - root: true, - after: null, - next: null, - }, -]; +[{ + id: 6385328, + data: '', + external: ['L'], + root: true, + after: null, + next: null, +}, ]; \ No newline at end of file diff --git a/demo/data.js b/demo/data.js index 4909b35..ed18639 100644 --- a/demo/data.js +++ b/demo/data.js @@ -1,103 +1,77 @@ const SOURCES_DATA = [{ - "LinkList0": { + "BinaryTree0": { "data": [{ - "id": "0x616eb0", - "data": "Z", - "next": "0x616ef0", - "loopNext": null, - "rootExternal": [ - "L" + "external": [ + "T1" ], + "child": [ + "0x617ee0", + "0x617f10" + ], + "id": "0x617eb0", + "name": "T1", + "data": "Z", + "root": true, "type": "default" }, { - "freed": true, - "id": "0x616ef0", - "data": "", - "next": "0x605010", - "loopNext": null, + "child": [ + "0x0", + "0x0" + ], + "id": "0x617ee0", + "name": "T1->lchild", + "data": "D", "type": "default" }, { - "id": "0x605010", - "data": "1", - "external": [ - "t" + "child": [ + "0x617f70", + "0x617f40" ], - "next": null, - "loopNext": null, + "id": "0x617f10", + "name": "T1->rchild", + "data": "C", "type": "default" - } - ], - "layouter": "LinkList" - }, - "handleUpdate": { - "isEnterFunction": false, - "isFirstDebug": false - } -}, { - "LinkList0": { - "data": [{ - "id": "0x616eb0", - "data": "Z", - "external": [ - "L" + }, + { + "child": [ + "0x0", + "0x0" ], - "next": "0x616ef0", - "loopNext": null + "id": "0x617f70", + "name": "(T1->rchild)->lchild", + "data": "A", + "type": "default" }, { - "freed": true, - "id": "0x616ef0", - "data": "", - "next": "0x605010", - "loopNext": null - }, - { - "id": "0x605010", - "data": "1", + "child": [ + "0x0", + "0x617fa0" + ], + "id": "0x617f40", + "name": "(T1->rchild)->rchild", + "data": "B", + "type": "default", "external": [ "t" - ], - "next": null, - "loopNext": null - } - ], - "layouter": "LinkList" - }, - "handleUpdate": { - "isEnterFunction": false, - "isFirstDebug": false - } -}, { - "LinkList0": { - "data": [{ - "id": "0x616eb0", - "data": "Z", - "external": [ - "L" - ], - "next": "0x616ef0", - "loopNext": null + ] }, { - "freed": true, - "id": "0x616ef0", - "data": "", - "next": "0x605010", - "loopNext": null - }, - { - "id": "0x605010", - "data": "1", - "external": [ - "t" + "child": [ + "0x0", + "0x617f40" ], - "next": null, - "loopNext": null + "id": "0x617fa0", + "name": "((T1->rchild)->rchild)->rchild", + "data": "E", + "type": "default", + "external": [ + "r" + ] } ], - "layouter": "LinkList" + "layouter": "BinaryTree" }, "handleUpdate": { "isEnterFunction": false,