2023-08-29 21:58:37 +08:00
|
|
|
import { Graph, Extensions, extend } from '@antv/g6';
|
2023-02-02 10:31:36 +08:00
|
|
|
|
2023-08-29 21:58:37 +08:00
|
|
|
const ExtGraph = extend(Graph, {
|
|
|
|
layouts: {
|
|
|
|
fruchterman: Extensions.FruchtermanLayout,
|
|
|
|
},
|
|
|
|
});
|
2023-08-28 00:17:22 +08:00
|
|
|
|
2023-02-02 10:31:36 +08:00
|
|
|
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) => {
|
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-08-31 18:16:13 +08:00
|
|
|
transforms: ['transform-v4-data'],
|
2023-08-28 00:17:22 +08:00
|
|
|
node: {
|
|
|
|
labelShape: {
|
|
|
|
text: {
|
|
|
|
fields: ['id'],
|
|
|
|
formatter: (node) => node.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
labelBackgroundShape: {},
|
|
|
|
animates: {
|
|
|
|
update: [
|
|
|
|
{
|
|
|
|
fields: ['opacity'],
|
2023-08-31 22:16:30 +08:00
|
|
|
states: ['selected', 'active'],
|
2023-08-28 00:17:22 +08:00
|
|
|
shapeId: 'haloShape',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: ['lineWidth'],
|
2023-08-31 22:16:30 +08:00
|
|
|
states: ['selected', 'active'],
|
2023-08-28 00:17:22 +08:00
|
|
|
shapeId: 'keyShape',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
layout: {
|
|
|
|
type: 'fruchterman',
|
2023-08-31 22:16:30 +08:00
|
|
|
speed: 50,
|
2023-08-28 00:17:22 +08:00
|
|
|
gravity: 1,
|
|
|
|
animated: true,
|
|
|
|
maxIteration: 500,
|
|
|
|
},
|
|
|
|
modes: {
|
|
|
|
default: ['zoom-canvas', 'drag-canvas', 'drag-node', 'click-select'],
|
|
|
|
},
|
|
|
|
data,
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
|
2023-08-28 00:17:22 +08:00
|
|
|
/******** 拖拽固定节点的逻辑 *********/
|
2023-02-02 10:31:36 +08:00
|
|
|
graph.on('node:dragstart', function (e) {
|
2023-08-28 00:17:22 +08:00
|
|
|
graph.stopLayout();
|
2023-02-02 10:31:36 +08:00
|
|
|
});
|
|
|
|
graph.on('node:drag', function (e) {
|
|
|
|
refreshDragedNodePosition(e);
|
|
|
|
});
|
2023-08-28 00:17:22 +08:00
|
|
|
graph.on('node:dragend', (e) => {
|
|
|
|
graph.layout();
|
|
|
|
});
|
|
|
|
function refreshDragedNodePosition(e) {
|
|
|
|
const { x, y } = e.canvas;
|
|
|
|
graph.updateData('node', {
|
|
|
|
id: e.itemId,
|
|
|
|
data: {
|
|
|
|
fx: x,
|
|
|
|
fy: y,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/*********************************/
|
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
|
|
|
};
|
|
|
|
});
|