StructV2/demo/Layouter/BinaryTree.js

157 lines
3.0 KiB
JavaScript
Raw Normal View History

2021-10-29 07:46:18 +00:00
SV.registerLayout('BinaryTree', {
2022-02-28 17:32:28 +00:00
defineOptions() {
return {
2022-03-02 14:07:27 +00:00
node: {
2022-02-28 17:32:28 +00:00
default: {
type: 'binary-tree-node',
size: [60, 30],
2022-03-02 14:07:27 +00:00
label: '[id]',
2022-02-28 17:32:28 +00:00
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
},
};
},
/**
* 对子树进行递归布局
*/
2022-03-02 14:07:27 +00:00
layoutItem(node, layoutOptions) {
2022-02-28 17:32:28 +00:00
// 次双亲不进行布局
if (!node) {
return null;
}
let bound = node.getBound(),
width = bound.width,
height = bound.height,
group = new Group(node),
leftGroup = null,
2022-03-02 14:07:27 +00:00
rightGroup = null,
leftBound = null,
rightBound = null;
2022-02-28 17:32:28 +00:00
if (node.visited) {
return null;
}
if (node.child && node.child[0]) {
2022-03-02 14:07:27 +00:00
leftGroup = this.layoutItem(node.child[0], layoutOptions);
2022-02-28 17:32:28 +00:00
}
if (node.child && node.child[1]) {
2022-03-02 14:07:27 +00:00
rightGroup = this.layoutItem(node.child[1], layoutOptions);
2022-02-28 17:32:28 +00:00
}
2022-03-02 14:07:27 +00:00
if (leftGroup) {
leftBound = leftGroup.getBound();
node.set('y', leftBound.y - layoutOptions.yInterval - height);
}
2022-02-28 17:32:28 +00:00
2022-03-02 14:07:27 +00:00
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);
2022-02-28 17:32:28 +00:00
}
}
if (leftGroup) {
2022-03-02 14:07:27 +00:00
leftBound = leftGroup.getBound();
2022-02-28 17:32:28 +00:00
node.set('x', leftBound.x + leftBound.width + layoutOptions.xInterval / 2 - width);
}
2022-02-28 17:32:28 +00:00
if(rightGroup) {
2022-03-02 14:07:27 +00:00
rightBound = rightGroup.getBound();
2022-02-28 17:32:28 +00:00
node.set('x', rightBound.x - layoutOptions.xInterval / 2 - width);
}
2022-02-28 17:32:28 +00:00
node.visited = true;
2022-02-28 17:32:28 +00:00
if (leftGroup) {
group.add(leftGroup);
}
2022-02-28 17:32:28 +00:00
if (rightGroup) {
group.add(rightGroup);
}
2022-02-28 17:32:28 +00:00
return group;
},
2022-02-28 17:32:28 +00:00
/**
* 布局函数
* @param {*} elements
* @param {*} layoutOptions
*/
layout(elements, layoutOptions) {
let root = elements[0];
2022-03-02 14:07:27 +00:00
this.layoutItem(root, layoutOptions);
2022-02-28 17:32:28 +00:00
},
2021-07-31 16:43:01 +00:00
});
2022-02-28 17:32:28 +00:00
[
{
id: 6385328,
data: '',
external: ['L'],
root: true,
after: null,
next: null,
},
];