diff --git a/demoV2/Layouter/BinaryTree.js b/demoV2/Layouter/BinaryTree.js index 35aff49..3abff47 100644 --- a/demoV2/Layouter/BinaryTree.js +++ b/demoV2/Layouter/BinaryTree.js @@ -1,11 +1,11 @@ SV.registerLayout('BinaryTree', { defineOptions() { return { - element: { + node: { default: { type: 'binary-tree-node', size: [60, 30], - label: '[data]', + label: '[id]', style: { fill: '#b83b5e', stroke: '#333', @@ -58,7 +58,7 @@ SV.registerLayout('BinaryTree', { /** * 对子树进行递归布局 */ - layoutItem(node, parent, index, layoutOptions) { + layoutItem(node, layoutOptions) { // 次双亲不进行布局 if (!node) { return null; @@ -69,43 +69,54 @@ SV.registerLayout('BinaryTree', { height = bound.height, group = new Group(node), leftGroup = null, - rightGroup = null; + rightGroup = null, + leftBound = null, + rightBound = null; if (node.visited) { return null; } if (node.child && node.child[0]) { - leftGroup = this.layoutItem(node.child[0], node, 0, layoutOptions); + leftGroup = this.layoutItem(node.child[0], layoutOptions); } if (node.child && node.child[1]) { - rightGroup = this.layoutItem(node.child[1], node, 1, layoutOptions); + rightGroup = this.layoutItem(node.child[1], layoutOptions); } - // 处理左右子树相交问题 - if (leftGroup && rightGroup) { - let intersection = Bound.intersect(leftGroup.getBound(), rightGroup.getBound()), - move = 0; + if (leftGroup) { + leftBound = leftGroup.getBound(); + node.set('y', leftBound.y - layoutOptions.yInterval - height); + } - if (intersection && intersection.width > 0) { - move = (intersection.width + layoutOptions.xInterval) / 2; - leftGroup.translate(-move, 0); - rightGroup.translate(move, 0); + if(rightGroup) { + rightBound = rightGroup.getBound(); + + if(leftGroup) { + rightGroup.translate(0, leftBound.y - rightBound.y) + } + + rightBound = rightGroup.getBound(); + node.set('y', rightBound.y - layoutOptions.yInterval - height); + } + + // 处理左右子树相交问题 + 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) { - let leftBound = leftGroup.getBound(); - - node.set('y', leftBound.y - layoutOptions.yInterval - height); + leftBound = leftGroup.getBound(); node.set('x', leftBound.x + leftBound.width + layoutOptions.xInterval / 2 - width); } if(rightGroup) { - let rightBound = rightGroup.getBound(); - - node.set('y', rightBound.y - layoutOptions.yInterval - height); + rightBound = rightGroup.getBound(); node.set('x', rightBound.x - layoutOptions.xInterval / 2 - width); } @@ -129,7 +140,7 @@ SV.registerLayout('BinaryTree', { */ layout(elements, layoutOptions) { let root = elements[0]; - this.layoutItem(root, null, -1, layoutOptions); + this.layoutItem(root, layoutOptions); }, }); diff --git a/demoV2/Layouter/LinkList.js b/demoV2/Layouter/LinkList.js index 951d011..a6dc7d8 100644 --- a/demoV2/Layouter/LinkList.js +++ b/demoV2/Layouter/LinkList.js @@ -77,25 +77,37 @@ SV.registerLayout('LinkList', { layout: { xInterval: 50, yInterval: 50 - }, - behavior: { - dragNode: false } }; }, - layout(elements, layoutOptions) { - for (let i = 0; i < elements.length; i++) { - let node = elements[i], - prev = elements[1 - 1], - width = node.get('size')[0]; - - if (prev) { - node.set('y', prev.get('y')); - node.set('x', prev.get('x') + layoutOptions.xInterval + width); - } + /** + * 对子树进行递归布局 + * @param node + * @param parent + */ + layoutItem(node, prev, layoutOptions) { + if (!node) { + return null; } + + let width = node.get('size')[0]; + + if (prev) { + node.set('y', prev.get('y')); + node.set('x', prev.get('x') + layoutOptions.xInterval + width); + } + + if (node.next) { + this.layoutItem(node.next, node, layoutOptions); + } + }, + + + layout(elements, layoutOptions) { + let root = elements[0]; + this.layoutItem(root, null, layoutOptions); } }); diff --git a/demoV2/Layouter/LinkStack.js b/demoV2/Layouter/LinkStack.js index 9634ddd..dc49a13 100644 --- a/demoV2/Layouter/LinkStack.js +++ b/demoV2/Layouter/LinkStack.js @@ -20,7 +20,7 @@ element: { default: { type: 'link-list-node', - label: '[id]', + label: '[data]', size: [60, 30], style: { stroke: '#333', diff --git a/demoV2/data.js b/demoV2/data.js new file mode 100644 index 0000000..40d5929 --- /dev/null +++ b/demoV2/data.js @@ -0,0 +1,120 @@ +const SOURCES_DATA = [ + { + LinkList0: { + data: [ + { + id: '0x617eb0', + data: 'Z', + next: '0x617ef0', + rootExternal: ['L'], + type: 'default', + }, + { + id: '0x617ef0', + data: 'A', + next: '0x617f10', + type: 'default', + }, + { + id: '0x617f10', + data: 'B', + next: '0x617f30', + type: 'default', + }, + { + id: '0x617f30', + data: 'C', + external: ['r', 't'], + next: null, + type: 'default', + }, + ], + layouter: 'LinkList', + }, + LinkList1: { + data: [ + { + id: '0x617ed0', + data: 'Y', + next: '0x617f50', + rootExternal: ['L2'], + type: 'default', + }, + { + id: '0x617f50', + data: 'a', + next: '0x617f70', + type: 'default', + }, + { + id: '0x617f70', + data: 'b', + external: ['r2', 't2'], + loopNext: 'LinkList0#0x617f30', + type: 'default', + }, + ], + layouter: 'LinkList', + }, + isEnterFunction: false, + }, + { + LinkList0: { + data: [ + { + id: '0x617eb0', + data: 'Z', + next: '0x617ef0', + rootExternal: ['L'], + type: 'default', + }, + { + id: '0x617ef0', + data: 'A', + next: '0x617f10', + type: 'default', + }, + { + id: '0x617f10', + data: 'B', + next: '0x617f30', + type: 'default', + }, + { + id: '0x617f30', + data: 'C', + external: ['r', 't'], + next: null, + type: 'default', + }, + ], + layouter: 'LinkList', + }, + LinkList1: { + data: [ + { + id: '0x617ed0', + data: 'Y', + next: '0x617f50', + rootExternal: ['L2'], + type: 'default', + }, + { + id: '0x617f50', + data: 'a', + next: '0x617f70', + type: 'default', + }, + { + id: '0x617f70', + data: 'b', + external: ['r2', 't2'], + next: null, + type: 'default', + }, + ], + layouter: 'LinkList', + }, + isEnterFunction: false, + }, +]; diff --git a/demoV2/demo2.html b/demoV2/demo2.html index 82e01d7..511956d 100644 --- a/demoV2/demo2.html +++ b/demoV2/demo2.html @@ -1,237 +1,151 @@ +
+ + +