g6/packages/site/examples/net/layoutMechanism/demo/dataChange.js

627 lines
8.8 KiB
JavaScript
Raw Normal View History

2023-02-02 10:31:36 +08:00
import G6 from '@antv/g6';
const data = {
nodes: [
{
id: '0',
2023-08-28 20:39:59 +08:00
data: {
label: '0',
},
2023-02-02 10:31:36 +08:00
},
{
id: '1',
2023-08-28 20:39:59 +08:00
data: {
label: '1',
},
2023-02-02 10:31:36 +08:00
},
{
id: '2',
2023-08-28 20:39:59 +08:00
data: {
label: '2',
},
2023-02-02 10:31:36 +08:00
},
{
id: '3',
2023-08-28 20:39:59 +08:00
data: {
label: '3',
},
2023-02-02 10:31:36 +08:00
},
{
id: '4',
2023-08-28 20:39:59 +08:00
data: {
label: '4',
},
2023-02-02 10:31:36 +08:00
},
{
id: '5',
2023-08-28 20:39:59 +08:00
data: {
label: '5',
},
2023-02-02 10:31:36 +08:00
},
{
id: '6',
2023-08-28 20:39:59 +08:00
data: {
label: '6',
},
2023-02-02 10:31:36 +08:00
},
{
id: '7',
2023-08-28 20:39:59 +08:00
data: {
label: '7',
},
2023-02-02 10:31:36 +08:00
},
{
id: '8',
2023-08-28 20:39:59 +08:00
data: {
label: '8',
},
2023-02-02 10:31:36 +08:00
},
{
id: '9',
2023-08-28 20:39:59 +08:00
data: {
label: '9',
},
2023-02-02 10:31:36 +08:00
},
{
id: '10',
2023-08-28 20:39:59 +08:00
data: {
label: '10',
},
2023-02-02 10:31:36 +08:00
},
{
id: '11',
2023-08-28 20:39:59 +08:00
data: {
label: '11',
},
2023-02-02 10:31:36 +08:00
},
{
id: '12',
2023-08-28 20:39:59 +08:00
data: {
label: '12',
},
2023-02-02 10:31:36 +08:00
},
{
id: '13',
2023-08-28 20:39:59 +08:00
data: {
label: '13',
},
2023-02-02 10:31:36 +08:00
},
{
id: '14',
2023-08-28 20:39:59 +08:00
data: {
label: '14',
},
2023-02-02 10:31:36 +08:00
},
{
id: '15',
2023-08-28 20:39:59 +08:00
data: {
label: '15',
},
2023-02-02 10:31:36 +08:00
},
{
id: '16',
2023-08-28 20:39:59 +08:00
data: {
label: '16',
},
2023-02-02 10:31:36 +08:00
},
{
id: '17',
2023-08-28 20:39:59 +08:00
data: {
label: '17',
},
2023-02-02 10:31:36 +08:00
},
{
id: '18',
2023-08-28 20:39:59 +08:00
data: {
label: '18',
},
2023-02-02 10:31:36 +08:00
},
{
id: '19',
2023-08-28 20:39:59 +08:00
data: {
label: '19',
},
2023-02-02 10:31:36 +08:00
},
{
id: '20',
2023-08-28 20:39:59 +08:00
data: {
label: '20',
},
2023-02-02 10:31:36 +08:00
},
{
id: '21',
2023-08-28 20:39:59 +08:00
data: {
label: '21',
},
2023-02-02 10:31:36 +08:00
},
{
id: '22',
2023-08-28 20:39:59 +08:00
data: {
label: '22',
},
2023-02-02 10:31:36 +08:00
},
{
id: '23',
2023-08-28 20:39:59 +08:00
data: {
label: '23',
},
2023-02-02 10:31:36 +08:00
},
{
id: '24',
2023-08-28 20:39:59 +08:00
data: {
label: '24',
},
2023-02-02 10:31:36 +08:00
},
{
id: '25',
2023-08-28 20:39:59 +08:00
data: {
label: '25',
},
2023-02-02 10:31:36 +08:00
},
{
id: '26',
2023-08-28 20:39:59 +08:00
data: {
label: '26',
},
2023-02-02 10:31:36 +08:00
},
{
id: '27',
2023-08-28 20:39:59 +08:00
data: {
label: '27',
},
2023-02-02 10:31:36 +08:00
},
{
id: '28',
2023-08-28 20:39:59 +08:00
data: {
label: '28',
},
2023-02-02 10:31:36 +08:00
},
{
id: '29',
2023-08-28 20:39:59 +08:00
data: {
label: '29',
},
2023-02-02 10:31:36 +08:00
},
{
id: '30',
2023-08-28 20:39:59 +08:00
data: {
label: '30',
},
2023-02-02 10:31:36 +08:00
},
{
id: '31',
2023-08-28 20:39:59 +08:00
data: {
label: '31',
},
2023-02-02 10:31:36 +08:00
},
{
id: '32',
2023-08-28 20:39:59 +08:00
data: {
label: '32',
},
2023-02-02 10:31:36 +08:00
},
{
id: '33',
2023-08-28 20:39:59 +08:00
data: {
label: '33',
},
2023-02-02 10:31:36 +08:00
},
],
edges: [
{
2023-08-28 20:39:59 +08:00
id: 'edge-866',
2023-02-02 10:31:36 +08:00
source: '0',
target: '1',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-435',
2023-02-02 10:31:36 +08:00
source: '0',
target: '2',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-743',
2023-02-02 10:31:36 +08:00
source: '0',
target: '3',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-208',
2023-02-02 10:31:36 +08:00
source: '0',
target: '4',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-773',
2023-02-02 10:31:36 +08:00
source: '0',
target: '5',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-481',
2023-02-02 10:31:36 +08:00
source: '0',
target: '7',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-355',
2023-02-02 10:31:36 +08:00
source: '0',
target: '8',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-430',
2023-02-02 10:31:36 +08:00
source: '0',
target: '9',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-938',
2023-02-02 10:31:36 +08:00
source: '0',
target: '10',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-554',
2023-02-02 10:31:36 +08:00
source: '0',
target: '11',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-820',
2023-02-02 10:31:36 +08:00
source: '0',
target: '13',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-904',
2023-02-02 10:31:36 +08:00
source: '0',
target: '14',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-223',
2023-02-02 10:31:36 +08:00
source: '0',
target: '15',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-230',
2023-02-02 10:31:36 +08:00
source: '0',
target: '16',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-441',
2023-02-02 10:31:36 +08:00
source: '2',
target: '3',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-808',
2023-02-02 10:31:36 +08:00
source: '4',
target: '5',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-990',
2023-02-02 10:31:36 +08:00
source: '4',
target: '6',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-495',
2023-02-02 10:31:36 +08:00
source: '5',
target: '6',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-723',
2023-02-02 10:31:36 +08:00
source: '7',
target: '13',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-438',
2023-02-02 10:31:36 +08:00
source: '8',
target: '14',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-196',
2023-02-02 10:31:36 +08:00
source: '9',
target: '10',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-642',
2023-02-02 10:31:36 +08:00
source: '10',
target: '22',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-355',
2023-02-02 10:31:36 +08:00
source: '10',
target: '14',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-596',
2023-02-02 10:31:36 +08:00
source: '10',
target: '12',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-59',
2023-02-02 10:31:36 +08:00
source: '10',
target: '24',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-274',
2023-02-02 10:31:36 +08:00
source: '10',
target: '21',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-730',
2023-02-02 10:31:36 +08:00
source: '10',
target: '20',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-964',
2023-02-02 10:31:36 +08:00
source: '11',
target: '24',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-555',
2023-02-02 10:31:36 +08:00
source: '11',
target: '22',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-406',
2023-02-02 10:31:36 +08:00
source: '11',
target: '14',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-259',
2023-02-02 10:31:36 +08:00
source: '12',
target: '13',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-952',
2023-02-02 10:31:36 +08:00
source: '16',
target: '17',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-569',
2023-02-02 10:31:36 +08:00
source: '16',
target: '18',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-246',
2023-02-02 10:31:36 +08:00
source: '16',
target: '21',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-540',
2023-02-02 10:31:36 +08:00
source: '16',
target: '22',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-747',
2023-02-02 10:31:36 +08:00
source: '17',
target: '18',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-800',
2023-02-02 10:31:36 +08:00
source: '17',
target: '20',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-484',
2023-02-02 10:31:36 +08:00
source: '18',
target: '19',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-418',
2023-02-02 10:31:36 +08:00
source: '19',
target: '20',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-714',
2023-02-02 10:31:36 +08:00
source: '19',
target: '33',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-600',
2023-02-02 10:31:36 +08:00
source: '19',
target: '22',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-717',
2023-02-02 10:31:36 +08:00
source: '19',
target: '23',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-306',
2023-02-02 10:31:36 +08:00
source: '20',
target: '21',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-599',
2023-02-02 10:31:36 +08:00
source: '21',
target: '22',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-413',
2023-02-02 10:31:36 +08:00
source: '22',
target: '24',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-281',
2023-02-02 10:31:36 +08:00
source: '22',
target: '25',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-182',
2023-02-02 10:31:36 +08:00
source: '22',
target: '26',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-285',
2023-02-02 10:31:36 +08:00
source: '22',
target: '23',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-553',
2023-02-02 10:31:36 +08:00
source: '22',
target: '28',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-55',
2023-02-02 10:31:36 +08:00
source: '22',
target: '30',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-823',
2023-02-02 10:31:36 +08:00
source: '22',
target: '31',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-996',
2023-02-02 10:31:36 +08:00
source: '22',
target: '32',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-323',
2023-02-02 10:31:36 +08:00
source: '22',
target: '33',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-462',
2023-02-02 10:31:36 +08:00
source: '23',
target: '28',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-476',
2023-02-02 10:31:36 +08:00
source: '23',
target: '27',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-446',
2023-02-02 10:31:36 +08:00
source: '23',
target: '29',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-273',
2023-02-02 10:31:36 +08:00
source: '23',
target: '30',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-564',
2023-02-02 10:31:36 +08:00
source: '23',
target: '31',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-623',
2023-02-02 10:31:36 +08:00
source: '23',
target: '33',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-714',
2023-02-02 10:31:36 +08:00
source: '32',
target: '33',
},
],
};
const data2 = {
nodes: [
{
id: 'b0',
2023-08-28 20:39:59 +08:00
data: {
label: '0',
},
2023-02-02 10:31:36 +08:00
},
{
id: 'b1',
2023-08-28 20:39:59 +08:00
data: {
label: '1',
},
2023-02-02 10:31:36 +08:00
},
{
id: 'b2',
2023-08-28 20:39:59 +08:00
data: {
label: '2',
},
2023-02-02 10:31:36 +08:00
},
{
id: 'b3',
2023-08-28 20:39:59 +08:00
data: {
label: '3',
},
2023-02-02 10:31:36 +08:00
},
{
id: 'b4',
2023-08-28 20:39:59 +08:00
data: {
label: '4',
},
2023-02-02 10:31:36 +08:00
},
{
id: 'b5',
2023-08-28 20:39:59 +08:00
data: {
label: '5',
},
2023-02-02 10:31:36 +08:00
},
],
edges: [
{
2023-08-28 20:39:59 +08:00
id: 'edge-842',
2023-02-02 10:31:36 +08:00
source: 'b0',
target: 'b1',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-862',
2023-02-02 10:31:36 +08:00
source: 'b0',
target: 'b2',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-408',
2023-02-02 10:31:36 +08:00
source: 'b0',
target: 'b3',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-215',
2023-02-02 10:31:36 +08:00
source: 'b0',
target: 'b4',
},
{
2023-08-28 20:39:59 +08:00
id: 'edge-379',
2023-02-02 10:31:36 +08:00
source: 'b0',
target: 'b5',
},
],
};
const container = document.getElementById('container');
const width = container.scrollWidth;
const height = container.scrollHeight || 500;
const graph = new G6.Graph({
2023-08-28 20:39:59 +08:00
container,
2023-02-02 10:31:36 +08:00
width,
height,
modes: {
2023-08-28 20:39:59 +08:00
default: ['drag-canvas', 'drag-node', 'zoom-canvas', 'click-select'],
2023-02-02 10:31:36 +08:00
},
layout: {
type: 'force',
2023-08-28 20:39:59 +08:00
animated: true,
linkDistance: 100,
preventOverlap: true,
2023-02-02 10:31:36 +08:00
},
2023-08-28 20:39:59 +08:00
edge: {
keyShape: {
endArrow: true,
2023-02-02 10:31:36 +08:00
},
},
2023-08-28 20:39:59 +08:00
data,
});
let currentData = data;
const btn = document.createElement('a');
btn.style.position = 'absolute';
btn.innerHTML = '👉 Change Data';
btn.style.fontSize = '20px';
btn.style.backgroundColor = 'rgba(255, 255, 255, 0.8)';
btn.style.border = '2px solid #873bf4';
btn.style.padding = '4px 8px';
container.appendChild(btn);
btn.addEventListener('click', async () => {
currentData = currentData === data2 ? data : data2;
graph.changeData(currentData);
2023-02-02 10:31:36 +08:00
});
if (typeof window !== 'undefined')
window.onresize = () => {
2023-08-28 20:39:59 +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 20:39:59 +08:00
graph.setSize([container.scrollWidth, container.scrollHeight]);
2023-02-02 10:31:36 +08:00
};