2021-08-25 10:50:34 +00:00
|
|
|
/**
|
|
|
|
* 三叉树
|
|
|
|
*/
|
2022-03-17 07:15:10 +00:00
|
|
|
SV.registerLayout('TriTree', {
|
2021-08-25 10:50:34 +00:00
|
|
|
defineOptions() {
|
|
|
|
return {
|
2022-03-17 07:15:10 +00:00
|
|
|
node: {
|
2021-08-25 10:50:34 +00:00
|
|
|
default: {
|
|
|
|
type: 'tri-tree-node',
|
|
|
|
size: [60, 30],
|
|
|
|
label: '[data]',
|
|
|
|
style: {
|
2022-03-17 07:15:10 +00:00
|
|
|
fill: '#95e1d3',
|
|
|
|
stroke: '#333',
|
|
|
|
cursor: 'pointer',
|
|
|
|
backgroundFill: '#eee'
|
|
|
|
},
|
|
|
|
labelOptions: {
|
|
|
|
style: { fontSize: 16 }
|
2021-08-25 10:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
link: {
|
2022-03-17 07:15:10 +00:00
|
|
|
child: {
|
2021-08-25 10:50:34 +00:00
|
|
|
sourceAnchor: index => index,
|
|
|
|
targetAnchor: 3,
|
2022-03-17 07:15:10 +00:00
|
|
|
type: 'line',
|
2021-08-25 10:50:34 +00:00
|
|
|
style: {
|
|
|
|
stroke: '#333',
|
2022-03-17 07:15:10 +00:00
|
|
|
lineAppendWidth: 10,
|
|
|
|
lineWidth: 1.6,
|
2021-08-25 10:50:34 +00:00
|
|
|
endArrow: 'default',
|
|
|
|
startArrow: {
|
2022-03-17 07:15:10 +00:00
|
|
|
path: G6.Arrow.circle(2, -1),
|
2021-08-25 10:50:34 +00:00
|
|
|
fill: '#333'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-03-18 12:37:02 +00:00
|
|
|
parent: {
|
|
|
|
type: 'line',
|
|
|
|
sourceAnchor: 4,
|
|
|
|
targetAnchor: 6,
|
|
|
|
style: {
|
|
|
|
stroke: '#999',
|
|
|
|
lineAppendWidth: 10,
|
|
|
|
lineWidth: 1.6,
|
|
|
|
endArrow: 'default',
|
|
|
|
startArrow: {
|
|
|
|
path: G6.Arrow.circle(2, -1),
|
|
|
|
fill: '#999'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-03-17 07:15:10 +00:00
|
|
|
r_parent: {
|
|
|
|
type: 'quadratic',
|
|
|
|
sourceAnchor: 4,
|
|
|
|
targetAnchor: 5,
|
|
|
|
curveOffset: -20,
|
|
|
|
style: {
|
|
|
|
stroke: '#999',
|
|
|
|
lineAppendWidth: 10,
|
|
|
|
lineWidth: 1.6,
|
|
|
|
endArrow: 'default',
|
|
|
|
startArrow: {
|
|
|
|
path: G6.Arrow.circle(2, -1),
|
|
|
|
fill: '#999'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
l_parent: {
|
|
|
|
type: 'quadratic',
|
2021-08-25 10:50:34 +00:00
|
|
|
sourceAnchor: 4,
|
|
|
|
targetAnchor: 2,
|
2022-03-17 07:15:10 +00:00
|
|
|
curveOffset: 20,
|
2021-08-25 10:50:34 +00:00
|
|
|
style: {
|
2022-03-17 07:15:10 +00:00
|
|
|
stroke: '#999',
|
|
|
|
lineAppendWidth: 10,
|
|
|
|
lineWidth: 1.6,
|
2021-08-25 10:50:34 +00:00
|
|
|
endArrow: 'default',
|
|
|
|
startArrow: {
|
2022-03-17 07:15:10 +00:00
|
|
|
path: G6.Arrow.circle(2, -1),
|
|
|
|
fill: '#999'
|
2021-08-25 10:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
marker: {
|
|
|
|
external: {
|
2022-03-17 07:15:10 +00:00
|
|
|
type: 'pointer',
|
2021-08-25 10:50:34 +00:00
|
|
|
anchor: 3,
|
|
|
|
offset: 14,
|
2022-03-17 07:15:10 +00:00
|
|
|
labelOffset: 2,
|
2021-08-25 10:50:34 +00:00
|
|
|
style: {
|
|
|
|
fill: '#f08a5d'
|
|
|
|
},
|
|
|
|
labelOptions: {
|
|
|
|
style: {
|
2022-03-17 07:15:10 +00:00
|
|
|
fontSize: 15,
|
|
|
|
fill: '#999'
|
2021-08-25 10:50:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2022-03-17 07:15:10 +00:00
|
|
|
addressLabel: {
|
|
|
|
style: {
|
|
|
|
fill: '#999'
|
|
|
|
}
|
|
|
|
},
|
2021-08-25 10:50:34 +00:00
|
|
|
layout: {
|
|
|
|
xInterval: 40,
|
|
|
|
yInterval: 40,
|
2022-03-17 07:15:10 +00:00
|
|
|
}
|
2021-08-25 10:50:34 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 对子树进行递归布局
|
|
|
|
*/
|
|
|
|
layoutItem(node, parent, index, layoutOptions) {
|
|
|
|
// 次双亲不进行布局
|
2022-03-17 07:15:10 +00:00
|
|
|
if (!node) {
|
2021-08-25 10:50:34 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-03-17 07:15:10 +00:00
|
|
|
|
2021-08-25 10:50:34 +00:00
|
|
|
let bound = node.getBound(), //获取包围盒
|
|
|
|
width = bound.width,
|
|
|
|
height = bound.height,
|
|
|
|
group = new Group(node); //创建分组
|
2022-03-17 07:15:10 +00:00
|
|
|
|
2021-08-25 10:50:34 +00:00
|
|
|
//有双亲,设置结点的位置
|
2022-03-17 07:15:10 +00:00
|
|
|
if (parent) {
|
2021-08-25 10:50:34 +00:00
|
|
|
// 纵坐标
|
|
|
|
node.set('y', parent.get('y') + layoutOptions.yInterval + height);
|
|
|
|
// 左节点横坐标
|
2022-03-17 07:15:10 +00:00
|
|
|
if (index === 0) {
|
2021-08-25 10:50:34 +00:00
|
|
|
node.set('x', parent.get('x') - layoutOptions.xInterval / 2 - width / 2);
|
|
|
|
}
|
|
|
|
// 右结点横坐标
|
2022-03-17 07:15:10 +00:00
|
|
|
if (index === 1) {
|
2021-08-25 10:50:34 +00:00
|
|
|
node.set('x', parent.get('x') + layoutOptions.xInterval / 2 + width / 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//有孩子
|
2022-03-17 07:15:10 +00:00
|
|
|
if (node.child && (node.child[0] || node.child[1])) {
|
2021-08-25 10:50:34 +00:00
|
|
|
let leftChild = node.child[0],
|
|
|
|
rightChild = node.child[1],
|
|
|
|
leftGroup = this.layoutItem(leftChild, node, 0, layoutOptions),
|
|
|
|
rightGroup = this.layoutItem(rightChild, node, 1, layoutOptions),
|
|
|
|
intersection = null,
|
|
|
|
move = 0;
|
2022-03-17 07:15:10 +00:00
|
|
|
|
2021-08-25 10:50:34 +00:00
|
|
|
// 处理左子树中子树相交问题
|
2022-03-17 07:15:10 +00:00
|
|
|
if (leftGroup && rightChild) {
|
2021-08-25 10:50:34 +00:00
|
|
|
//求出包围盒相交部分
|
|
|
|
intersection = Bound.intersect(leftGroup.getBound(), rightGroup.getBound());
|
|
|
|
move = 0;
|
|
|
|
//处理
|
2022-03-17 07:15:10 +00:00
|
|
|
if (intersection && intersection.width > 0) {
|
2021-08-25 10:50:34 +00:00
|
|
|
move = (intersection.width + layoutOptions.xInterval) / 2;
|
|
|
|
// console.log(move,intersection.width,layoutOptions.xInterval);
|
|
|
|
leftGroup.translate(-move, 0);
|
|
|
|
rightGroup.translate(move, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//加入分组
|
2022-03-17 07:15:10 +00:00
|
|
|
if (leftGroup) {
|
2021-08-25 10:50:34 +00:00
|
|
|
group.add(leftGroup);
|
|
|
|
}
|
2022-03-17 07:15:10 +00:00
|
|
|
if (rightGroup) {
|
2021-08-25 10:50:34 +00:00
|
|
|
group.add(rightGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
//返回分组
|
|
|
|
return group;
|
2022-03-17 07:15:10 +00:00
|
|
|
},
|
2021-08-25 10:50:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 布局函数
|
|
|
|
* @param {*} elements
|
|
|
|
* @param {*} layoutOptions
|
|
|
|
*/
|
|
|
|
layout(elements, layoutOptions) {
|
|
|
|
let root = elements[0];
|
|
|
|
this.layoutItem(root, null, -1, layoutOptions);
|
|
|
|
}
|
|
|
|
});
|