g6/packages/site/examples/net/forceDirected/demo/forceConstrainedInRect.js

81 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-02-02 10:31:36 +08:00
import G6 from '@antv/g6';
// 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');
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);
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) => {
if (minx > node.data.x) {
minx = node.data.x;
2023-02-02 10:31:36 +08:00
}
if (maxx < node.data.x) {
maxx = node.data.x;
2023-02-02 10:31:36 +08:00
}
if (miny > node.data.y) {
miny = node.data.y;
2023-02-02 10:31:36 +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) => {
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,
transform: ['transform-v4-data'],
2023-02-02 10:31:36 +08:00
layout: {
type: 'force',
animated: true,
2023-02-02 10:31:36 +08:00
onTick,
},
data,
2023-02-02 10:31:36 +08:00
});
if (typeof window !== 'undefined')
window.onresize = () => {
if (!graph || graph.destroyed) return;
2023-02-02 10:31:36 +08:00
if (!container || !container.scrollWidth || !container.scrollHeight) return;
graph.setSize([container.scrollWidth, container.scrollHeight]);
2023-02-02 10:31:36 +08:00
};
});