Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export the layout methods. add: subgraph layout demo on site.
- Loading branch information
1 parent
b556ae7
commit 4310001
Showing
7 changed files
with
204 additions
and
2 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
173 changes: 173 additions & 0 deletions
173
packages/graphin-site/examples/layout/sublayout/demo/Sublayout.jsx
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,173 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
/* eslint-disable no-undef */ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Graphin, { Layout } from '@antv/graphin'; | ||
import { Button } from 'antd'; | ||
import '@antv/graphin/dist/index.css'; // 引入Graphin CSS | ||
|
||
const getSub = (data, refCurrent) => { | ||
// stop the simulation if the previous layout is force layout | ||
const { forceSimulation } = refCurrent; | ||
if (forceSimulation) { | ||
forceSimulation.stop(); | ||
} | ||
|
||
const { nodes, edges } = data; | ||
|
||
const subNodes1 = []; | ||
const subEdges1 = []; | ||
const subNodes2 = []; | ||
const subEdges2 = []; | ||
|
||
// split the nodes into two parts | ||
nodes.forEach((node, i) => { | ||
if (i < 10) { | ||
subNodes1.push(node); | ||
} else { | ||
subNodes2.push(node); | ||
} | ||
}); | ||
|
||
// find the edges for these two parts | ||
edges.forEach(edge => { | ||
let findSource = false; | ||
let findTarget = false; | ||
subNodes1.forEach(sn => { | ||
if (edge.source === sn.id) { | ||
findSource = true; | ||
} | ||
else if (edge.target === sn.id) { | ||
findTarget = true; | ||
} | ||
}); | ||
if (findSource && findTarget) { | ||
subEdges1.push(edge); | ||
return; | ||
} | ||
findSource = false; | ||
findTarget = false; | ||
subNodes2.forEach(sn => { | ||
if (edge.source === sn.id) { | ||
findSource = true; | ||
} | ||
else if (edge.target === sn.id) { | ||
findTarget = true; | ||
} | ||
}); | ||
if (findSource && findTarget) { | ||
subEdges2.push(edge); | ||
} | ||
}); | ||
|
||
// layout the part1 | ||
const node1 = Layout.Circle({nodes: subNodes1, edges: subEdges1}, { x: 100, y: 100, r: 80}); | ||
|
||
// layout the part2 | ||
const node2 = Layout.Radial({nodes: subNodes2, edges: subEdges2}, { center: [220, 220], unitRadius: 100}); | ||
|
||
// combine the two parts | ||
const newNodes = [...node1.nodes, ...node2.nodes]; | ||
|
||
return { | ||
data: { | ||
nodes: newNodes, | ||
edges: data.edges, | ||
}, | ||
layout: null, | ||
}; | ||
}; | ||
const App = () => { | ||
const testData = { | ||
nodes: [ | ||
{ id: "node-0" }, | ||
{ id: "node-1" }, | ||
{ id: "node-2" }, | ||
{ id: "node-3" }, | ||
{ id: "node-4" }, | ||
{ id: "node-5" }, | ||
{ id: "node-6" }, | ||
{ id: "node-7" }, | ||
{ id: "node-8" }, | ||
{ id: "node-9" }, | ||
{ id: "node-10" }, | ||
{ id: "node-11" }, | ||
{ id: "node-12" }, | ||
{ id: "node-13" }, | ||
{ id: "node-14" }, | ||
{ id: "node-15" }, | ||
{ id: "node-16" }, | ||
{ id: "node-17" }, | ||
{ id: "node-18" }, | ||
{ id: "node-19" } | ||
], | ||
edges: [ | ||
{ source: "node-10", target: "node-11" }, | ||
{ source: "node-10", target: "node-12" }, | ||
{ source: "node-10", target: "node-13" }, | ||
{ source: "node-10", target: "node-14" }, | ||
{ source: "node-10", target: "node-15" }, | ||
{ source: "node-11", target: "node-12" }, | ||
{ source: "node-11", target: "node-16" }, | ||
{ source: "node-11", target: "node-17" }, | ||
{ source: "node-11", target: "node-18" }, | ||
{ source: "node-18", target: "node-19" }, | ||
] | ||
} | ||
const nodes = []; | ||
const edges = []; | ||
testData.nodes.forEach(tnode => { | ||
tnode.label = tnode.id; | ||
tnode.type = 'company'; | ||
tnode.shape = 'CircleNode'; | ||
tnode.style = {}; | ||
const node = { | ||
id: tnode.id, | ||
data: tnode, | ||
shape: 'CircleNode', | ||
style: {} | ||
} | ||
nodes.push(node); | ||
}); | ||
testData.edges.forEach(tedge => { | ||
const edge = { | ||
source: tedge.source, | ||
target: tedge.target, | ||
data: tedge | ||
} | ||
edges.push(edge); | ||
}); | ||
const [state, setState] = React.useState({ | ||
data: { nodes, edges }, | ||
layout: { | ||
name: 'force', | ||
}, | ||
}); | ||
|
||
const { data, layout } = state; | ||
|
||
const graphRef = React.createRef(null); | ||
let refCurrent; | ||
React.useEffect(() => { | ||
refCurrent = graphRef.current; | ||
}, [state]); | ||
|
||
return ( | ||
<div> | ||
<Button | ||
type="primary" | ||
onClick={() => { | ||
const result = getSub(data, refCurrent); | ||
setState({ | ||
...result, | ||
}); | ||
}} | ||
> | ||
sub layout | ||
</Button> | ||
<Graphin data={data} layout={layout} ref={graphRef} /> | ||
</div> | ||
); | ||
}; | ||
|
||
ReactDOM.render(<App />, document.getElementById('container')); |
13 changes: 13 additions & 0 deletions
13
packages/graphin-site/examples/layout/sublayout/demo/meta.json
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,13 @@ | ||
{ | ||
"title": { | ||
"zh": "中文分类", | ||
"en": "Category" | ||
}, | ||
"demos": [ | ||
{ | ||
"filename": "Sublayout.jsx", | ||
"title": "子图布局", | ||
"screenshot": "https://gw.alipayobjects.com/mdn/rms_f8c6a0/afts/img/A*qpVWRbWiPpIAAAAAAAAAAABkARQnAQ" | ||
} | ||
] | ||
} |
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,4 @@ | ||
--- | ||
title: 子图布局 | ||
order: 1 | ||
--- |
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
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,10 @@ | ||
import Dagre from './g6/dagre'; | ||
import Radial from './g6/radial'; | ||
import Force from './force'; | ||
import Concentric from './basic/concentric'; | ||
import Circle from './basic/circle'; | ||
import Grid from './basic/grid'; | ||
import Random from './basic/random'; | ||
import Tweak from './basic/tweak'; | ||
|
||
export default { Dagre, Radial, Force, Concentric, Circle, Grid, Random, Tweak }; |