mirror of
https://gitee.com/antv/g6.git
synced 2024-12-14 01:21:33 +08:00
d891fbe173
* fix: fix incomplete animation field parsing --------- Co-authored-by: yvonneyx <banxuan.zyx@antgroup.com>
93 lines
2.2 KiB
JavaScript
93 lines
2.2 KiB
JavaScript
import { Graph, Extensions, extend } from '@antv/g6';
|
|
|
|
const ExtGraph = extend(Graph, {
|
|
layouts: {
|
|
fruchterman: Extensions.FruchtermanLayout,
|
|
},
|
|
});
|
|
|
|
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 graph = new ExtGraph({
|
|
container: 'container',
|
|
width,
|
|
height,
|
|
transforms: [
|
|
{
|
|
type: 'transform-v4-data',
|
|
activeLifecycle: ['read'],
|
|
},
|
|
],
|
|
node: {
|
|
labelShape: {
|
|
text: {
|
|
fields: ['id'],
|
|
formatter: (node) => node.id,
|
|
},
|
|
},
|
|
labelBackgroundShape: {},
|
|
animates: {
|
|
update: [
|
|
{
|
|
fields: ['opacity'],
|
|
states: ['selected', 'active'],
|
|
shapeId: 'haloShape',
|
|
},
|
|
{
|
|
fields: ['lineWidth'],
|
|
states: ['selected', 'active'],
|
|
shapeId: 'keyShape',
|
|
},
|
|
],
|
|
},
|
|
},
|
|
layout: {
|
|
type: 'fruchterman',
|
|
speed: 50,
|
|
gravity: 1,
|
|
animated: true,
|
|
maxIteration: 500,
|
|
},
|
|
modes: {
|
|
default: ['zoom-canvas', 'drag-canvas', 'drag-node', 'click-select'],
|
|
},
|
|
data,
|
|
});
|
|
|
|
/******** 拖拽固定节点的逻辑 *********/
|
|
graph.on('node:dragstart', function (e) {
|
|
graph.stopLayout();
|
|
});
|
|
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,
|
|
},
|
|
});
|
|
}
|
|
/*********************************/
|
|
|
|
if (typeof window !== 'undefined')
|
|
window.onresize = () => {
|
|
if (!graph || graph.destroyed) return;
|
|
if (!container || !container.scrollWidth || !container.scrollHeight) return;
|
|
graph.setSize([container.scrollWidth, container.scrollHeight]);
|
|
};
|
|
});
|