2023-08-29 21:58:37 +08:00
|
|
|
import { Graph, Extensions, extend } from '@antv/g6';
|
|
|
|
|
|
|
|
const ExtGraph = extend(Graph, {
|
|
|
|
layouts: {
|
|
|
|
fruchterman: Extensions.FruchtermanLayout,
|
|
|
|
},
|
|
|
|
});
|
2023-02-02 10:31:36 +08:00
|
|
|
|
|
|
|
const container = document.getElementById('container');
|
|
|
|
const width = container.scrollWidth;
|
|
|
|
const height = (container.scrollHeight || 500) - 20;
|
|
|
|
|
|
|
|
const descriptionDiv = document.createElement('div');
|
2023-08-28 00:17:22 +08:00
|
|
|
descriptionDiv.innerHTML = 'Doing layout... web-worker is enabled in this demo, so the layout will not block the page.';
|
2023-02-02 10:31:36 +08:00
|
|
|
container.appendChild(descriptionDiv);
|
|
|
|
|
|
|
|
fetch('https://gw.alipayobjects.com/os/basement_prod/7bacd7d1-4119-4ac1-8be3-4c4b9bcbc25f.json')
|
|
|
|
.then((res) => res.json())
|
|
|
|
.then((data) => {
|
2023-08-29 21:58:37 +08:00
|
|
|
const graph = new ExtGraph({
|
2023-08-28 00:17:22 +08:00
|
|
|
container: 'container',
|
|
|
|
width,
|
|
|
|
height,
|
2023-11-29 10:59:21 +08:00
|
|
|
transforms: [
|
|
|
|
{
|
|
|
|
type: 'transform-v4-data',
|
|
|
|
activeLifecycle: ['read'],
|
|
|
|
},
|
|
|
|
],
|
2023-08-28 00:17:22 +08:00
|
|
|
modes: {
|
2023-08-31 22:16:30 +08:00
|
|
|
default: ['drag-canvas', 'drag-node', 'zoom-canvas', 'click-select'],
|
2023-08-28 00:17:22 +08:00
|
|
|
},
|
|
|
|
layout: {
|
|
|
|
type: 'fruchterman',
|
|
|
|
speed: 20,
|
|
|
|
gravity: 1,
|
|
|
|
maxIteration: 10000,
|
|
|
|
gravity: 1,
|
|
|
|
workerEnabled: true,
|
|
|
|
},
|
|
|
|
node: {
|
|
|
|
keyShape: {
|
|
|
|
r: 6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
|
|
|
|
graph.on('afterlayout', () => {
|
|
|
|
descriptionDiv.innerHTML = 'Done!';
|
|
|
|
});
|
|
|
|
|
|
|
|
if (typeof window !== 'undefined')
|
|
|
|
window.onresize = () => {
|
|
|
|
if (!graph || graph.destroyed) return;
|
|
|
|
if (!container || !container.scrollWidth || !container.scrollHeight) return;
|
|
|
|
graph.setSize([container.scrollWidth, container.scrollHeight - 20]);
|
|
|
|
};
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|