g6/packages/site/examples/net/fruchtermanLayout/demo/fruchtermanFix.js

88 lines
2.2 KiB
JavaScript
Raw Normal View History

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-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({
container: 'container',
width,
height,
2023-08-31 18:16:13 +08:00
transforms: ['transform-v4-data'],
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'],
shapeId: 'haloShape',
},
{
fields: ['lineWidth'],
2023-08-31 22:16:30 +08:00
states: ['selected', 'active'],
shapeId: 'keyShape',
},
],
},
},
layout: {
type: 'fruchterman',
2023-08-31 22:16:30 +08:00
speed: 50,
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-02-02 10:31:36 +08:00
graph.on('node:dragstart', function (e) {
graph.stopLayout();
2023-02-02 10:31:36 +08:00
});
graph.on('node:drag', function (e) {
refreshDragedNodePosition(e);
});
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 = () => {
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
};
});