-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
254 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
packages/site/examples/layout/force-directed/demo/bubbles.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
packages/site/examples/layout/force-directed/demo/drag-fixed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/site/examples/layout/force-directed/demo/prevent-overlap.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |