StructV2/demo/dataStruct/LinkStack.js

129 lines
3.1 KiB
JavaScript
Raw Normal View History

2021-04-16 07:38:52 +00:00
/**
* 单链表
*/
2021-04-20 11:12:41 +00:00
class LinkStack extends Engine {
2021-04-16 07:38:52 +00:00
defineOptions() {
return {
element: {
2021-04-20 11:12:41 +00:00
default: {
2021-04-16 07:38:52 +00:00
type: 'link-list-node',
label: '[id]',
size: [60, 30],
style: {
stroke: '#333',
fill: '#b83b5e'
}
}
},
link: {
next: {
type: 'line',
sourceAnchor: 1,
2021-04-20 11:12:41 +00:00
targetAnchor: 2,
2021-04-16 07:38:52 +00:00
style: {
stroke: '#333',
endArrow: {
path: G6.Arrow.triangle(8, 6, 0),
fill: '#333'
},
startArrow: {
path: G6.Arrow.circle(2, -1),
fill: '#333'
}
}
}
},
pointer: {
external: {
2021-04-20 11:12:41 +00:00
offset: 8,
2021-04-16 07:38:52 +00:00
style: {
fill: '#f08a5d'
}
}
},
layout: {
xInterval: 50,
2021-04-20 11:12:41 +00:00
yInterval: 30
2021-04-16 07:38:52 +00:00
}
};
}
/**
* 对子树进行递归布局
* @param node
* @param parent
*/
layoutItem(node, prev, layoutOptions) {
if(!node) {
return null;
}
2021-04-20 11:12:41 +00:00
let height = node.get('size')[1];
2021-04-16 07:38:52 +00:00
if(prev) {
2021-04-20 11:12:41 +00:00
node.set('x', prev.get('x'));
node.set('y', prev.get('y') + layoutOptions.yInterval + height);
2021-04-16 07:38:52 +00:00
}
if(node.next) {
this.layoutItem(node.next, node, layoutOptions);
}
}
layout(elements, layoutOptions) {
let nodes = elements.default,
rootNodes = [],
node,
i;
for(i = 0; i < nodes.length; i++) {
node = nodes[i];
if(node.root) {
rootNodes.push(node);
}
}
for(i = 0; i < rootNodes.length; i++) {
let root = rootNodes[i],
2021-04-20 11:12:41 +00:00
width = root.get('size')[0];
2021-04-16 07:38:52 +00:00
2021-04-20 11:12:41 +00:00
root.set('x', root.get('x') + i * (layoutOptions.xInterval + width));
2021-04-16 07:38:52 +00:00
this.layoutItem(root, null, layoutOptions);
}
}
}
2021-04-20 11:12:41 +00:00
const LStack = function(container) {
2021-04-16 07:38:52 +00:00
return{
2021-04-20 11:12:41 +00:00
engine: new LinkStack(container),
2021-04-16 07:38:52 +00:00
data: [[
2021-04-20 11:12:41 +00:00
{ id: 1, root: true, next: 2 },
2021-04-16 07:38:52 +00:00
{ id: 2, next: 3 },
{ id: 3, next: 4 },
{ id: 4, next: 5 },
{ id: 5 },
{ id: 6, root: true, next: 7 },
{ id: 7, next: 8 },
{ id: 8, next: 4 },
{ id: 9, root: true, next: 10 },
{ id: 10 }
],
[
2021-04-20 11:12:41 +00:00
{ id: 1, root: true, next: 2 },
2021-04-16 07:38:52 +00:00
{ id: 2, next: 3 },
{ id: 3, next: 6 },
{ id: 6, next: 7 },
{ id: 7, next: 8 },
{ id: 8 }
]]
}
};