Skip to content

Commit

Permalink
docs: force demos
Browse files Browse the repository at this point in the history
  • Loading branch information
yvonneyx committed Jan 3, 2025
1 parent 6e9f064 commit f96d610
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 6 deletions.
15 changes: 14 additions & 1 deletion packages/g6/src/behaviors/drag-element-force.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ import { DragElement } from './drag-element';
*
* <en/> Call d3-force layout to drag element behavior options
*/
export interface DragElementForceOptions extends Omit<DragElementOptions, 'animation' | 'dropEffect' | 'shadow'> {}
export interface DragElementForceOptions extends Omit<DragElementOptions, 'animation' | 'dropEffect' | 'shadow'> {
/**
* <zh/> 在拖拽结束后,节点是否保持固定位置
* - `true`: 在拖拽结束后,节点的位置将保持固定,不受布局算法的影响
* - `false`: 在拖拽结束后,节点的位置将继续受到布局算法的影响
*
* <en/> Whether the node remains in a fixed position after dragging ends
* - `true`: After dragging ends, the node's position will remain fixed and will not be affected by the layout algorithm
* - `false`: After dragging ends, the node's position will continue to be affected by the layout algorithm
*/
fixed?: boolean;
}

/**
* <zh/> 调用力导布局拖拽元素的交互
Expand Down Expand Up @@ -101,6 +112,8 @@ export class DragElementForce extends DragElement {
const layout = this.forceLayoutInstance;
if (layout) getLayoutProperty(layout, 'simulation').alphaTarget(0);

if (this.options.fixed) return;

this.context.graph.getNodeData(this.target).forEach((element) => {
if (layout) invokeLayoutMethod(layout, 'setFixedPosition', idOf(element), [null, null, null]);
});
Expand Down
129 changes: 129 additions & 0 deletions packages/site/examples/layout/force-directed/demo/bubbles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { Graph, NodeEvent } from '@antv/g6';

const data = {
nodes: [
{
id: '0',
label: '0',
value: 10,
cluster: 'a',
description: 'this is node 0, \nand the value of it is 10',
},
{
id: '1',
label: '1',
value: 20,
cluster: 'b',
description: 'this is node 1, \nand the value of it is 20',
},
{
id: '2',
label: '2',
value: 5,
cluster: 'a',
description: 'this is node 2, \nand the value of it is 5',
},
{
id: '3',
label: '3',
value: 10,
cluster: 'a',
description: 'this is node 3, \nand the value of it is 10',
},
{
id: '4',
label: '4',
value: 12,
cluster: 'c',
subCluster: 'sb',
description: 'this is node 4, \nand the value of it is 12',
},
{
id: '5',
label: '5',
value: 18,
cluster: 'c',
subCluster: 'sa',
description: 'this is node 5, \nand the value of it is 18',
},
{
id: '6',
label: '6',
value: 3,
cluster: 'c',
subCluster: 'sa',
description: 'this is node 6, \nand the value of it is 3',
},
{
id: '7',
label: '7',
value: 7,
cluster: 'b',
subCluster: 'sa',
description: 'this is node 7, \nand the value of it is 7',
},
{
id: '8',
label: '8',
value: 21,
cluster: 'd',
subCluster: 'sb',
description: 'this is node 8, \nand the value of it is 21',
},
{
id: '9',
label: '9',
value: 9,
cluster: 'd',
subCluster: 'sb',
description: 'this is node 9, \nand the value of it is 9',
},
],
edges: [],
};

const oriSize = {};

const nodes = data.nodes;
// randomize the node size
nodes.forEach((node) => {
node.size = Math.random() * 30 + 16;
oriSize[node.id] = node.size;
});

const graph = new Graph({
container: 'container',
data,
node: {
style: {
size: (d) => d.size,
labelText: (d) => (d.size === 200 ? d.description : d.id),
labelPlacement: 'middle',
labelFill: '#fff',
},
palette: {
field: (d) => d.cluster,
},
},
layout: {
type: 'd3-force',
collide: {
radius: (d) => d.size / 2,
strength: 0.7,
},
manyBody: {
strength: 30,
},
},
behaviors: ['drag-element-force'],
});

graph.on(NodeEvent.CLICK, async (e) => {
const nodeId = e.target.id;
const data = graph.getNodeData(nodeId);
const size = data.size === oriSize[nodeId] ? 200 : oriSize[nodeId];
graph.updateNodeData([{ id: nodeId, size }]);
await graph.layout();
});

graph.render();
49 changes: 49 additions & 0 deletions packages/site/examples/layout/force-directed/demo/drag-fixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Graph } from '@antv/g6';

const data = {
nodes: new Array(10).fill(0).map((_, i) => ({ id: `${i}`, label: `${i}` })),
edges: [
{ source: '0', target: '1' },
{ source: '0', target: '2' },
{ source: '0', target: '3' },
{ source: '0', target: '4' },
{ source: '0', target: '5' },
{ source: '0', target: '7' },
{ source: '0', target: '8' },
{ source: '0', target: '9' },
{ source: '2', target: '3' },
{ source: '4', target: '5' },
{ source: '4', target: '6' },
{ source: '5', target: '6' },
],
};

const graph = new Graph({
container: 'container',
data,
node: {
style: {
labelText: (d) => d.label,
labelPlacement: 'middle',
labelFill: '#fff',
},
},
layout: {
type: 'd3-force',
link: {
distance: 100,
strength: 2
},
collide: {
radius: 40,
},
},
behaviors: [
{
type: 'drag-element-force',
fixed: true,
},
],
});

graph.render();
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const graph = new Graph({
},
},
},
behaviors: ['zoom-canvas', 'drag-canvas', 'drag-element-force'],
behaviors: ['drag-element-force'],
});

graph.render();
32 changes: 28 additions & 4 deletions packages/site/examples/layout/force-directed/demo/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*-HgiS6CyuuEAAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "d3-force.js",
"title": {
"zh": "D3 力导向布局",
"en": "D3 Force Layout"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*-_sFS5IRGGcAAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "functional-params.js",
"title": {
Expand All @@ -29,12 +37,28 @@
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*78tVRrG7zU8AAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "d3-force.js",
"filename": "prevent-overlap.js",
"title": {
"zh": "D3 力导向布局",
"en": "D3 Force Layout"
"zh": "力导向布局防止节点重叠",
"en": "Prevent Overlap in d3-force Layout"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*-_sFS5IRGGcAAAAAAAAAAAAADmJ7AQ/original"
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*J802QozikwsAAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "drag-fixed.js",
"title": {
"zh": "固定被做拽节点",
"en": "Drag Fixed Nodes"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*QlS5T6Wy2TUAAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "bubbles.js",
"title": {
"zh": "力导向气泡图",
"en": "Force Directed Bubble Chart"
},
"screenshot": "https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*iMR5QqrGp0cAAAAAAAAAAAAADmJ7AQ/original"
},
{
"filename": "3d-force.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Graph } from '@antv/g6';

fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/relations.json')
.then((res) => res.json())
.then((data) => {
const nodes = data.nodes;
// randomize the node size
nodes.forEach((node) => {
node.size = Math.random() * 30 + 5;
});

const graph = new Graph({
container: 'container',
autoFit: 'center',
data,
node: {
style: {
size: (d) => d.size,
lineWidth: 1,
},
},
layout: {
type: 'd3-force',
collide: {
// Prevent nodes from overlapping by specifying a collision radius for each node.
radius: (d) => d.size / 2,
},
},
behaviors: ['drag-element-force'],
});

graph.render();
});

0 comments on commit f96d610

Please sign in to comment.