2023-02-02 10:31:36 +08:00
|
|
|
import G6 from '@antv/g6';
|
|
|
|
|
2023-08-28 00:17:22 +08:00
|
|
|
// TODO: do not add this demo, onTick cannot assign the original layout data
|
|
|
|
|
2023-02-02 10:31:36 +08:00
|
|
|
const graphDiv = document.getElementById('container');
|
|
|
|
const descriptionDiv = document.createElement('div');
|
2023-08-28 00:17:22 +08:00
|
|
|
descriptionDiv.innerHTML = 'Constrians the nodes to be layed in the gray area with force-directed layout';
|
2023-02-02 10:31:36 +08:00
|
|
|
graphDiv.appendChild(descriptionDiv);
|
|
|
|
|
|
|
|
const container = document.getElementById('container');
|
|
|
|
const width = container.scrollWidth;
|
|
|
|
const height = container.scrollHeight || 500;
|
|
|
|
|
|
|
|
fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/relations.json')
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((data) => {
|
|
|
|
// 灰色区域
|
|
|
|
const constrainBox = { x: 60, y: 50, width: 500, height: 150 };
|
|
|
|
|
|
|
|
const backrect = document.createElement('div');
|
|
|
|
backrect.style.backgroundColor = '#666';
|
|
|
|
backrect.style.opacity = 0.1;
|
|
|
|
backrect.style.marginLeft = `${constrainBox.x}px`;
|
|
|
|
backrect.style.marginTop = `${constrainBox.y}px`;
|
|
|
|
backrect.style.width = `${constrainBox.width}px`;
|
|
|
|
backrect.style.height = `${constrainBox.height}px`;
|
|
|
|
backrect.style.position = 'absolute';
|
|
|
|
graphDiv.appendChild(backrect);
|
|
|
|
|
2023-08-28 00:17:22 +08:00
|
|
|
const onTick = (nodes) => {
|
2023-02-02 10:31:36 +08:00
|
|
|
let minx = 99999999;
|
|
|
|
let maxx = -99999999;
|
|
|
|
let miny = 99999999;
|
|
|
|
let maxy = -99999999;
|
|
|
|
let maxsize = -9999999;
|
|
|
|
nodes.forEach((node) => {
|
2023-08-28 00:17:22 +08:00
|
|
|
if (minx > node.data.x) {
|
|
|
|
minx = node.data.x;
|
2023-02-02 10:31:36 +08:00
|
|
|
}
|
2023-08-28 00:17:22 +08:00
|
|
|
if (maxx < node.data.x) {
|
|
|
|
maxx = node.data.x;
|
2023-02-02 10:31:36 +08:00
|
|
|
}
|
2023-08-28 00:17:22 +08:00
|
|
|
if (miny > node.data.y) {
|
|
|
|
miny = node.data.y;
|
2023-02-02 10:31:36 +08:00
|
|
|
}
|
2023-08-28 00:17:22 +08:00
|
|
|
if (maxy < node.data.y) {
|
|
|
|
maxy = node.data.y;
|
2023-02-02 10:31:36 +08:00
|
|
|
}
|
|
|
|
if (maxsize < node.size) {
|
|
|
|
maxsize = node.size;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
const scalex = (constrainBox.width - maxsize) / (maxx - minx);
|
|
|
|
const scaley = (constrainBox.height - maxsize) / (maxy - miny);
|
|
|
|
nodes.forEach((node) => {
|
2023-08-28 00:17:22 +08:00
|
|
|
node.data.x = (node.data.x - minx) * scalex + constrainBox.x;
|
|
|
|
node.data.y = (node.data.y - miny) * scaley + constrainBox.y;
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const graph = new G6.Graph({
|
|
|
|
container: 'container',
|
|
|
|
width,
|
|
|
|
height,
|
2023-08-28 00:17:22 +08:00
|
|
|
transform: ['transform-v4-data'],
|
2023-02-02 10:31:36 +08:00
|
|
|
layout: {
|
|
|
|
type: 'force',
|
2023-08-28 00:17:22 +08:00
|
|
|
animated: true,
|
2023-02-02 10:31:36 +08:00
|
|
|
onTick,
|
|
|
|
},
|
2023-08-28 00:17:22 +08:00
|
|
|
data,
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
if (typeof window !== 'undefined')
|
|
|
|
window.onresize = () => {
|
2023-08-28 00:17:22 +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 00:17:22 +08:00
|
|
|
graph.setSize([container.scrollWidth, container.scrollHeight]);
|
2023-02-02 10:31:36 +08:00
|
|
|
};
|
|
|
|
});
|