2023-08-28 20:39:59 +08:00
|
|
|
import G6, { extend } from '@antv/g6';
|
2023-02-02 10:31:36 +08:00
|
|
|
|
|
|
|
const data = {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
id: '0',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'A',
|
|
|
|
cluster: 'part1',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '1',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'B',
|
|
|
|
cluster: 'part1',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '2',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'C',
|
|
|
|
cluster: 'part1',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '3',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'D',
|
|
|
|
cluster: 'part1',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '4',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'E',
|
|
|
|
cluster: 'part1',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '5',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'F',
|
|
|
|
cluster: 'part1',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '6',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'a',
|
|
|
|
cluster: 'part2',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '7',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'b',
|
|
|
|
cluster: 'part2',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '8',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'c',
|
|
|
|
cluster: 'part2',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '9',
|
2023-08-28 20:39:59 +08:00
|
|
|
data: {
|
|
|
|
label: 'd',
|
|
|
|
cluster: 'part2',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
],
|
|
|
|
edges: [
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-270',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '0',
|
|
|
|
target: '6',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-483',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '0',
|
|
|
|
target: '7',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-942',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '0',
|
|
|
|
target: '9',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-569',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '1',
|
|
|
|
target: '6',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-152',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '1',
|
|
|
|
target: '9',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-540',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '1',
|
|
|
|
target: '7',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-754',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '2',
|
|
|
|
target: '8',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-78',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '2',
|
|
|
|
target: '9',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-824',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '2',
|
|
|
|
target: '6',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-308',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '3',
|
|
|
|
target: '8',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-254',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '4',
|
|
|
|
target: '6',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-283',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '4',
|
|
|
|
target: '7',
|
|
|
|
},
|
|
|
|
{
|
2023-08-28 20:39:59 +08:00
|
|
|
id: 'edge-360',
|
2023-02-02 10:31:36 +08:00
|
|
|
source: '5',
|
|
|
|
target: '9',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2023-08-28 20:39:59 +08:00
|
|
|
class BiLayout {
|
|
|
|
// implements Layout<{}>
|
|
|
|
id = 'bi-layout';
|
|
|
|
|
|
|
|
constructor(options = {}) {
|
|
|
|
this.options = options;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Return the positions of nodes and edges(if needed).
|
|
|
|
*/
|
|
|
|
async execute(graph, options = {}) {
|
|
|
|
return this.genericLineLayout(false, graph, options);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* To directly assign the positions to the nodes.
|
|
|
|
*/
|
|
|
|
async assign(graph, options = {}) {
|
|
|
|
this.genericLineLayout(true, graph, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
async genericLineLayout(assign, graph, options = {}) {
|
|
|
|
const {
|
|
|
|
center = [0, 0],
|
|
|
|
biSep = 100,
|
|
|
|
nodeSep = 20,
|
|
|
|
nodeSize = 32,
|
|
|
|
direction = 'horizontal',
|
|
|
|
} = { ...this.options, ...options };
|
2023-02-02 10:31:36 +08:00
|
|
|
let part1Pos = 0;
|
|
|
|
let part2Pos = 0;
|
|
|
|
if (direction === 'horizontal') {
|
|
|
|
part1Pos = center[0] - biSep / 2;
|
|
|
|
part2Pos = center[0] + biSep / 2;
|
|
|
|
}
|
2023-08-28 20:39:59 +08:00
|
|
|
const nodes = graph.getAllNodes();
|
|
|
|
const edges = graph.getAllEdges();
|
|
|
|
|
2023-02-02 10:31:36 +08:00
|
|
|
const part1Nodes = [];
|
|
|
|
const part2Nodes = [];
|
|
|
|
const part1NodeMap = new Map();
|
|
|
|
const part2NodeMap = new Map();
|
|
|
|
// separate the nodes and init the positions
|
|
|
|
nodes.forEach(function (node, i) {
|
2023-08-28 20:39:59 +08:00
|
|
|
if (node.data.cluster === 'part1') {
|
2023-02-02 10:31:36 +08:00
|
|
|
part1Nodes.push(node);
|
|
|
|
part1NodeMap.set(node.id, i);
|
|
|
|
} else {
|
|
|
|
part2Nodes.push(node);
|
|
|
|
part2NodeMap.set(node.id, i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// order the part1 node
|
2023-08-28 20:39:59 +08:00
|
|
|
const indexMap = {};
|
2023-02-02 10:31:36 +08:00
|
|
|
part1Nodes.forEach(function (p1n) {
|
|
|
|
let index = 0;
|
|
|
|
let adjCount = 0;
|
|
|
|
edges.forEach(function (edge) {
|
|
|
|
const sourceId = edge.source;
|
|
|
|
const targetId = edge.target;
|
|
|
|
if (sourceId === p1n.id) {
|
|
|
|
index += part2NodeMap.get(targetId);
|
|
|
|
adjCount += 1;
|
|
|
|
} else if (targetId === p1n.id) {
|
|
|
|
index += part2NodeMap.get(sourceId);
|
|
|
|
adjCount += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
index /= adjCount;
|
2023-08-28 20:39:59 +08:00
|
|
|
indexMap[p1n.id] = index;
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
part1Nodes.sort(function (a, b) {
|
2023-08-28 20:39:59 +08:00
|
|
|
return indexMap[a.id] - indexMap[b.id];
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
part2Nodes.forEach(function (p2n) {
|
|
|
|
let index = 0;
|
|
|
|
let adjCount = 0;
|
|
|
|
edges.forEach(function (edge) {
|
|
|
|
const sourceId = edge.source;
|
|
|
|
const targetId = edge.target;
|
|
|
|
if (sourceId === p2n.id) {
|
|
|
|
index += part1NodeMap.get(targetId);
|
|
|
|
adjCount += 1;
|
|
|
|
} else if (targetId === p2n.id) {
|
|
|
|
index += part1NodeMap.get(sourceId);
|
|
|
|
adjCount += 1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
index /= adjCount;
|
2023-08-28 20:39:59 +08:00
|
|
|
indexMap[p2n.id] = index;
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
part2Nodes.sort(function (a, b) {
|
2023-08-28 20:39:59 +08:00
|
|
|
return indexMap[a.id] - indexMap[b.id];
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// place the nodes
|
|
|
|
const hLength = part1Nodes.length > part2Nodes.length ? part1Nodes.length : part2Nodes.length;
|
|
|
|
const height = hLength * (nodeSep + nodeSize);
|
|
|
|
let begin = center[1] - height / 2;
|
|
|
|
if (direction === 'vertical') {
|
|
|
|
begin = center[0] - height / 2;
|
|
|
|
}
|
|
|
|
part1Nodes.forEach(function (p1n, i) {
|
|
|
|
if (direction === 'horizontal') {
|
2023-08-28 20:39:59 +08:00
|
|
|
p1n.data.x = part1Pos;
|
|
|
|
p1n.data.y = begin + i * (nodeSep + nodeSize);
|
2023-02-02 10:31:36 +08:00
|
|
|
} else {
|
2023-08-28 20:39:59 +08:00
|
|
|
p1n.data.x = begin + i * (nodeSep + nodeSize);
|
|
|
|
p1n.data.y = part1Pos;
|
2023-02-02 10:31:36 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
part2Nodes.forEach(function (p2n, i) {
|
|
|
|
if (direction === 'horizontal') {
|
2023-08-28 20:39:59 +08:00
|
|
|
p2n.data.x = part2Pos;
|
|
|
|
p2n.data.y = begin + i * (nodeSep + nodeSize);
|
2023-02-02 10:31:36 +08:00
|
|
|
} else {
|
2023-08-28 20:39:59 +08:00
|
|
|
p2n.data.x = begin + i * (nodeSep + nodeSize);
|
|
|
|
p2n.data.y = part2Pos;
|
2023-02-02 10:31:36 +08:00
|
|
|
}
|
|
|
|
});
|
2023-08-28 20:39:59 +08:00
|
|
|
|
|
|
|
const result = {
|
|
|
|
nodes: part1Nodes.concat(part2Nodes).map((node) => ({ id: node.id, data: { x: node.data.x, y: node.data.y } })),
|
|
|
|
edges,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (assign) {
|
|
|
|
layoutNodes.forEach((node) => {
|
|
|
|
graph.mergeNodeData(node.id, {
|
|
|
|
x: node.data.x,
|
|
|
|
y: node.data.y,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const CustomGraph = extend(G6.Graph, {
|
|
|
|
layouts: {
|
|
|
|
'bi-layout': BiLayout,
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const container = document.getElementById('container');
|
|
|
|
const width = container.scrollWidth;
|
|
|
|
const height = container.scrollHeight || 500;
|
2023-08-28 20:39:59 +08:00
|
|
|
const graph = new CustomGraph({
|
2023-02-02 10:31:36 +08:00
|
|
|
container: 'container',
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
layout: {
|
2023-08-28 20:39:59 +08:00
|
|
|
type: 'bi-layout',
|
2023-02-02 10:31:36 +08:00
|
|
|
biSep: 300,
|
|
|
|
nodeSep: 20,
|
2023-08-28 20:39:59 +08:00
|
|
|
nodeSize: 32,
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
2023-08-28 20:39:59 +08:00
|
|
|
theme: {
|
|
|
|
type: 'spec',
|
|
|
|
specification: {
|
|
|
|
node: {
|
|
|
|
dataTypeField: 'cluster',
|
|
|
|
},
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
modes: {
|
2023-08-28 20:39:59 +08:00
|
|
|
default: ['drag-canvas', 'drag-node', 'zoom-canvas', 'click-select'],
|
2023-02-02 10:31:36 +08:00
|
|
|
},
|
2023-08-28 20:39:59 +08:00
|
|
|
data,
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (typeof window !== 'undefined')
|
|
|
|
window.onresize = () => {
|
2023-08-28 20:39:59 +08:00
|
|
|
if (!graph || graph.destroyed) return;
|
2023-02-02 10:31:36 +08:00
|
|
|
if (!container || !container.scrollWidth || !container.scrollHeight) return;
|
2023-08-28 20:39:59 +08:00
|
|
|
graph.setSize([container.scrollWidth, container.scrollHeight]);
|
2023-02-02 10:31:36 +08:00
|
|
|
};
|