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,