StructV2/demoV2/Layouter/PTree.js

87 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-12-13 14:05:13 +00:00
// 解析数据:
// {
// PTree:{
// data:[
// {
// id: '1001',
// data: 'A',
// parent: -1,
// index: 0
// },
// {
// id: '1002',
// data: 'B',
// parent: 0,
// index: 1
// },
// ],
// layouter: 'PTree'
// }
// }
SV.registerLayout('PTree', {
sourcesPreprocess(sources) {
let dataLength = sources.length;
let parentNodes = [];
let i;
for (i = 0; i < dataLength; i++) {
parentNodes.push({
id: `parent-${i}`,
data: sources[i].parent
});
}
sources[0].indexLeft = 'data';
parentNodes[0].indexLeft = 'parent';
sources.push(...parentNodes);
return sources;
},
2021-12-13 14:05:13 +00:00
defineOptions() {
return {
node: {
default: {
type: 'rect',
2021-12-13 14:05:13 +00:00
label: '[data]',
size: [40, 40],
labelOptions: {
style: { fontSize: 16 }
},
2021-12-13 14:05:13 +00:00
style: {
stroke: '#333',
fill: '#95e1d3',
offset: 25
}
},
},
indexLabel: {
index: { position: 'top' },
indexLeft: { position: 'left' }
}
2021-12-13 14:05:13 +00:00
};
},
layout(elements) {
let nodeLength = elements.length,
halfLength = nodeLength / 2,
size = elements[0].get('size')[0],
i;
for (i = 0; i < nodeLength; i++) {
let x = (i % halfLength) * size;
y = Math.floor(i / halfLength) * size;
elements[i].set({ x, y });
}
}
});