docs: 📚️ add elk demo (#1578)

This commit is contained in:
vector
2021-11-20 15:06:06 +08:00
committed by GitHub
parent 8806256b44
commit b991b78d0e
9 changed files with 2359 additions and 6 deletions

View File

@ -10,12 +10,13 @@
},
"dependencies": {
"@antv/x6": "^1.28.1",
"@antv/x6-vector": "^1.2.4",
"@antv/x6-react-components": "^1.1.14",
"@antv/x6-react-shape": "^1.5.2",
"@antv/x6-vector": "^1.2.4",
"antd": "^4.4.2",
"classnames": "^2.2.6",
"dagre": "^0.8.5",
"elkjs": "^0.7.1",
"react": "^16.12.0",
"react-dom": "^16.8.6",
"react-helmet": "^6.0.0"

View File

@ -0,0 +1,197 @@
import React from 'react'
import { Graph, Cell } from '@antv/x6'
import ELK, { ElkNode, ElkExtendedEdge, ElkEdge } from 'elkjs/lib/elk-api.js'
import elkWorker from 'elkjs/lib/elk-worker.js'
import elkdata from './elkdata.json'
import '../index.less'
Graph.registerNode(
'elk-node',
{
inherit: 'rect',
attrs: {
body: {
fill: '#EFF4FF',
stroke: '#5F95FF',
strokeWidth: 1,
},
label: {
refX: 0,
refY: -4,
textAnchor: 'start',
textVerticalAnchor: 'bottom',
fontSize: 10,
},
},
ports: {
groups: {
port: {
position: {
name: 'absolute',
},
attrs: {
portBody: {
magnet: 'passive',
fill: '#5F95FF',
refWidth: '100%',
refHeight: '100%',
},
},
markup: [
{
tagName: 'rect',
selector: 'portBody',
},
],
},
},
},
},
true,
)
Graph.registerEdge(
'elk-edge',
{
inherit: 'edge',
attrs: {
line: {
stroke: '#A2B1C3',
strokeWidth: 1,
targetMarker: {
name: 'block',
width: 4,
height: 4,
},
},
},
},
true,
)
interface Position {
x: number
y: number
}
export default class Example extends React.Component {
private container: HTMLDivElement
private portIdToNodeIdMap: Record<string, string> = {}
private cells: Cell[] = []
private graph: Graph
componentDidMount() {
this.graph = new Graph({
container: this.container,
width: 1000,
height: 600,
interacting: false,
})
const elk = new ELK({
workerFactory: (url: string) => new elkWorker.Worker(url),
})
elk.layout(elkdata as any).then((res) => {
this.addChildren(res.children || [])
this.addEdges(res.edges || [])
this.graph.resetCells(this.cells)
this.graph.zoomToFit({ padding: 10, maxScale: 1 })
})
}
addChildren = (children: ElkNode[], pos?: Position) => {
console.log(children)
children.forEach((child) => {
const position = {
x: (child.x || 0) + (pos ? pos.x : 0),
y: (child.y || 0) + (pos ? pos.y : 0),
}
let label: string = ''
if (typeof child.labels === 'string') {
label = child.labels
} else if (Array.isArray(child.labels) && child.labels[0]) {
label = child.labels[0].text
}
const node = this.graph.createNode({
shape: 'elk-node',
id: child.id,
position,
label,
size: {
width: child.width || 0,
height: child.height || 0,
},
ports: {
items: (child.ports || []).map((item) => {
this.portIdToNodeIdMap[item.id] = child.id
return {
id: item.id,
group: 'port',
args: {
x: item.x,
y: item.y,
},
size: { width: item.width || 0, height: item.height || 0 },
}
}),
},
})
this.cells.push(node)
if (child.children) {
this.addChildren(child.children, position)
}
if (child.edges) {
this.addEdges(child.edges, position)
}
})
}
addEdges = (edges: ElkEdge[], pos?: Position) => {
edges.forEach((edge: ElkExtendedEdge) => {
const { bendPoints = [] } = edge.sections[0]
if (pos) {
bendPoints.map((bendPoint: Position) => {
bendPoint.x += pos.x
bendPoint.y += pos.y
})
}
const sourcePortId = edge.sources[0]
const targetPortId = edge.targets[0]
const sourceNodeId = this.portIdToNodeIdMap[sourcePortId]
const targetNodeId = this.portIdToNodeIdMap[targetPortId]
this.cells.push(
this.graph.createEdge({
shape: 'elk-edge',
source: {
cell: sourceNodeId,
port: sourcePortId,
},
target: {
cell: targetNodeId,
port: targetPortId,
},
vertices: bendPoints,
}),
)
})
}
refContainer = (container: HTMLDivElement) => {
this.container = container
}
render() {
return (
<div className="x6-graph-wrap">
<div ref={this.refContainer} className="x6-graph"></div>
</div>
)
}
}

View File

@ -0,0 +1,980 @@
{
"id": "n0",
"children": [
{
"id": "n1",
"labels": "DatagramReader",
"ports": [
{
"id": "p1",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "0"
}
},
{
"id": "p2",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
},
{
"id": "p3",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
},
{
"id": "p4",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "3"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
},
{
"id": "n2",
"labels": [
{
"text": "Ramp",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g837978",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g739884",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g539420",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
},
{
"id": "n3",
"labels": [
{
"text": "QueueControl",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 100.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g572291",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g653563",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g722216",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "2"
}
},
{
"id": "p4_g748499",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "3"
}
},
{
"id": "p5",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "4"
}
},
{
"id": "p6",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "5"
}
},
{
"id": "p7",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "6"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
},
{
"id": "n4",
"labels": [
{
"text": "Display",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 35.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g255145",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 35.0,
"height": 27.0
},
{
"id": "n5",
"labels": [
{
"text": "Interface - fast",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 10.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g820682",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g506569",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
}
],
"children": [
{
"id": "n6",
"labels": [
{
"text": "Sleep",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g582202",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g036217",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
},
{
"id": "p3_g791687",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "SOUTH",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 50.0,
"height": 35.0
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"edges": [
{
"id": "e15",
"sources": ["p1_g820682"],
"targets": ["p1_g582202"]
},
{
"id": "e16",
"sources": ["p2_g036217"],
"targets": ["p2_g506569"]
}
]
},
{
"id": "n7",
"labels": [
{
"text": "Interface - slow",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 100.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g261431",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g518944",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
}
],
"children": [
{
"id": "n8",
"labels": [
{
"text": "Sleep",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g587373",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g014361",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
},
{
"id": "p3_g749816",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "SOUTH",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 50.0,
"height": 35.0
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"edges": [
{
"id": "e17",
"sources": ["p1_g261431"],
"targets": ["p1_g587373"]
},
{
"id": "e18",
"sources": ["p2_g014361"],
"targets": ["p2_g518944"]
}
]
},
{
"id": "n9",
"labels": [
{
"text": "Counter - q1",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 55.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g650325",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g732355",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g158827",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 35.0
},
{
"id": "n10",
"labels": [
{
"text": "Counter - q2",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 55.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g006148",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g008338",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g606176",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 35.0
},
{
"id": "n11",
"labels": [
{
"text": "channel1",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g044769",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"children": [
{
"id": "n12",
"labels": [
{
"text": "Counter",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 75.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g924853",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g825798",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g627665",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 35.0
},
{
"id": "n13",
"labels": [
{
"text": "RecordAssembler",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 100.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g710134",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g774554",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g935869",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 10.0,
"height": 40.0
},
{
"id": "n14",
"labels": [
{
"text": "Display",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g849516",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 35.0,
"height": 27.0
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"edges": [
{
"id": "e19",
"sources": ["p1_g044769"],
"targets": ["p2_g825798"]
},
{
"id": "e20",
"sources": ["p3_g627665"],
"targets": ["p1_g710134"]
},
{
"id": "e21",
"sources": ["p1_g044769"],
"targets": ["p2_g774554"]
},
{
"id": "e22",
"sources": ["p3_g935869"],
"targets": ["p1_g849516"]
}
]
},
{
"id": "n15",
"labels": [
{
"text": "channel2",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g598580",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"children": [
{
"id": "n16",
"labels": [
{
"text": "Counter",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 75.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g439211",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g586159",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g324437",
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 35.0
},
{
"id": "n17",
"labels": [
{
"text": "RecordAssembler",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 100.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g816526",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g800534",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g930851",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 10.0,
"height": 40.0
},
{
"id": "n18",
"labels": [
{
"text": "Display",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g183964",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 35.0,
"height": 27.0
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"edges": [
{
"id": "e23",
"sources": ["p1_g598580"],
"targets": ["p2_g586159"]
},
{
"id": "e24",
"sources": ["p3_g324437"],
"targets": ["p1_g816526"]
},
{
"id": "e25",
"sources": ["p1_g598580"],
"targets": ["p2_g800534"]
},
{
"id": "e26",
"sources": ["p3_g930851"],
"targets": ["p1_g183964"]
}
]
},
{
"id": "n19",
"labels": [
{
"text": "MonitorValue - q1 length",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 150.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g445341",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
},
{
"id": "n20",
"labels": [
{
"text": "MonitorValue - q2 length",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 150.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g930680",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
}
],
"edges": [
{
"id": "e0",
"sources": ["p3"],
"targets": ["p2_g739884"]
},
{
"id": "e1",
"sources": ["p3_g539420"],
"targets": ["p2_g653563"]
},
{
"id": "e2",
"sources": ["p5"],
"targets": ["p1_g255145"]
},
{
"id": "e3",
"sources": ["p6"],
"targets": ["p1_g820682"]
},
{
"id": "e4",
"sources": ["p7"],
"targets": ["p1_g261431"]
},
{
"id": "e5",
"sources": ["p6"],
"targets": ["p2_g732355"]
},
{
"id": "e6",
"sources": ["p2_g506569"],
"targets": ["p1_g650325"]
},
{
"id": "e7",
"sources": ["p7"],
"targets": ["p2_g008338"]
},
{
"id": "e8",
"sources": ["p2_g518944"],
"targets": ["p1_g006148"]
},
{
"id": "e9",
"sources": ["p2_g506569"],
"targets": ["p1_g044769"]
},
{
"id": "e10",
"sources": ["p2_g518944"],
"targets": ["p1_g598580"]
},
{
"id": "e11",
"sources": ["p3_g158827"],
"targets": ["p1_g445341"]
},
{
"id": "e12",
"sources": ["p1_g445341"],
"targets": ["p3_g722216"]
},
{
"id": "e13",
"sources": ["p3_g606176"],
"targets": ["p1_g930680"]
},
{
"id": "e14",
"sources": ["p1_g930680"],
"targets": ["p1_g572291"]
}
]
}

View File

@ -0,0 +1,980 @@
{
"id": "n0",
"children": [
{
"id": "n1",
"labels": "DatagramReader",
"ports": [
{
"id": "p1",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "0"
}
},
{
"id": "p2",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
},
{
"id": "p3",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
},
{
"id": "p4",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "3"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
},
{
"id": "n2",
"labels": [
{
"text": "Ramp",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g837978",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g739884",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g539420",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
},
{
"id": "n3",
"labels": [
{
"text": "QueueControl",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 100.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g572291",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g653563",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g722216",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "2"
}
},
{
"id": "p4_g748499",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "3"
}
},
{
"id": "p5",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "4"
}
},
{
"id": "p6",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "5"
}
},
{
"id": "p7",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "6"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
},
{
"id": "n4",
"labels": [
{
"text": "Display",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 35.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g255145",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 35.0,
"height": 27.0
},
{
"id": "n5",
"labels": [
{
"text": "Interface - fast",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 10.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g820682",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g506569",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
}
],
"children": [
{
"id": "n6",
"labels": [
{
"text": "Sleep",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g582202",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g036217",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
},
{
"id": "p3_g791687",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "SOUTH",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 50.0,
"height": 35.0
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"edges": [
{
"id": "e15",
"sources": ["p1_g820682"],
"targets": ["p1_g582202"]
},
{
"id": "e16",
"sources": ["p2_g036217"],
"targets": ["p2_g506569"]
}
]
},
{
"id": "n7",
"labels": [
{
"text": "Interface - slow",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 100.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g261431",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g518944",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
}
],
"children": [
{
"id": "n8",
"labels": [
{
"text": "Sleep",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g587373",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g014361",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "1"
}
},
{
"id": "p3_g749816",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "SOUTH",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 50.0,
"height": 35.0
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"edges": [
{
"id": "e17",
"sources": ["p1_g261431"],
"targets": ["p1_g587373"]
},
{
"id": "e18",
"sources": ["p2_g014361"],
"targets": ["p2_g518944"]
}
]
},
{
"id": "n9",
"labels": [
{
"text": "Counter - q1",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 55.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g650325",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g732355",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g158827",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 35.0
},
{
"id": "n10",
"labels": [
{
"text": "Counter - q2",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 55.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g006148",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g008338",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g606176",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 35.0
},
{
"id": "n11",
"labels": [
{
"text": "channel1",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g044769",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"children": [
{
"id": "n12",
"labels": [
{
"text": "Counter",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 75.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g924853",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g825798",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g627665",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 35.0
},
{
"id": "n13",
"labels": [
{
"text": "RecordAssembler",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 100.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g710134",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g774554",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g935869",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 10.0,
"height": 40.0
},
{
"id": "n14",
"labels": [
{
"text": "Display",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g849516",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 35.0,
"height": 27.0
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"edges": [
{
"id": "e19",
"sources": ["p1_g044769"],
"targets": ["p2_g825798"]
},
{
"id": "e20",
"sources": ["p3_g627665"],
"targets": ["p1_g710134"]
},
{
"id": "e21",
"sources": ["p1_g044769"],
"targets": ["p2_g774554"]
},
{
"id": "e22",
"sources": ["p3_g935869"],
"targets": ["p1_g849516"]
}
]
},
{
"id": "n15",
"labels": [
{
"text": "channel2",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g598580",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"children": [
{
"id": "n16",
"labels": [
{
"text": "Counter",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 75.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g439211",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g586159",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g324437",
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 35.0
},
{
"id": "n17",
"labels": [
{
"text": "RecordAssembler",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 100.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g816526",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
},
{
"id": "p2_g800534",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "1"
}
},
{
"id": "p3_g930851",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "EAST",
"port.index": "2"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 10.0,
"height": 40.0
},
{
"id": "n18",
"labels": [
{
"text": "Display",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 50.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g183964",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 35.0,
"height": 27.0
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"edges": [
{
"id": "e23",
"sources": ["p1_g598580"],
"targets": ["p2_g586159"]
},
{
"id": "e24",
"sources": ["p3_g324437"],
"targets": ["p1_g816526"]
},
{
"id": "e25",
"sources": ["p1_g598580"],
"targets": ["p2_g800534"]
},
{
"id": "e26",
"sources": ["p3_g930851"],
"targets": ["p1_g183964"]
}
]
},
{
"id": "n19",
"labels": [
{
"text": "MonitorValue - q1 length",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 150.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g445341",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
},
{
"id": "n20",
"labels": [
{
"text": "MonitorValue - q2 length",
"layoutOptions": {
"nodeLabels.placement": "[H_LEFT, V_TOP, OUTSIDE]"
},
"width": 150.0,
"height": 12.0
}
],
"ports": [
{
"id": "p1_g930680",
"width": 7,
"height": 7,
"layoutOptions": {
"port.side": "WEST",
"port.index": "0"
}
}
],
"layoutOptions": {
"portConstraints": "FIXED_ORDER"
},
"width": 55.0,
"height": 40.0
}
],
"edges": [
{
"id": "e0",
"sources": ["p3"],
"targets": ["p2_g739884"]
},
{
"id": "e1",
"sources": ["p3_g539420"],
"targets": ["p2_g653563"]
},
{
"id": "e2",
"sources": ["p5"],
"targets": ["p1_g255145"]
},
{
"id": "e3",
"sources": ["p6"],
"targets": ["p1_g820682"]
},
{
"id": "e4",
"sources": ["p7"],
"targets": ["p1_g261431"]
},
{
"id": "e5",
"sources": ["p6"],
"targets": ["p2_g732355"]
},
{
"id": "e6",
"sources": ["p2_g506569"],
"targets": ["p1_g650325"]
},
{
"id": "e7",
"sources": ["p7"],
"targets": ["p2_g008338"]
},
{
"id": "e8",
"sources": ["p2_g518944"],
"targets": ["p1_g006148"]
},
{
"id": "e9",
"sources": ["p2_g506569"],
"targets": ["p1_g044769"]
},
{
"id": "e10",
"sources": ["p2_g518944"],
"targets": ["p1_g598580"]
},
{
"id": "e11",
"sources": ["p3_g158827"],
"targets": ["p1_g445341"]
},
{
"id": "e12",
"sources": ["p1_g445341"],
"targets": ["p3_g722216"]
},
{
"id": "e13",
"sources": ["p3_g606176"],
"targets": ["p1_g930680"]
},
{
"id": "e14",
"sources": ["p1_g930680"],
"targets": ["p1_g572291"]
}
]
}

View File

@ -0,0 +1,181 @@
import { Graph, Cell } from '@antv/x6'
////import ELK, { ElkNode, ElkExtendedEdge, ElkEdge } from 'elkjs/lib/elk-api.js';
import ELK, { ElkNode, ElkEdge, ElkExtendedEdge } from 'elkjs'
Graph.registerNode(
'elk-node',
{
inherit: 'rect',
attrs: {
body: {
fill: '#EFF4FF',
stroke: '#5F95FF',
strokeWidth: 1,
},
label: {
refX: 0,
refY: -4,
textAnchor: 'start',
textVerticalAnchor: 'bottom',
fontSize: 10,
},
},
ports: {
groups: {
port: {
position: {
name: 'absolute',
},
attrs: {
portBody: {
magnet: 'passive',
fill: '#5F95FF',
refWidth: '100%',
refHeight: '100%',
},
},
markup: [
{
tagName: 'rect',
selector: 'portBody',
},
],
},
},
},
},
true,
)
Graph.registerEdge(
'elk-edge',
{
inherit: 'edge',
attrs: {
line: {
stroke: '#A2B1C3',
strokeWidth: 1,
targetMarker: {
name: 'block',
width: 4,
height: 4,
},
},
},
},
true,
)
interface Position {
x: number
y: number
}
const portIdToNodeIdMap: Record<string, string> = {}
const cells: Cell[] = []
const graph = new Graph({
container: document.getElementById('container')!,
interacting: false,
})
const elk = new ELK()
const addChildren = (children: ElkNode[], pos?: Position) => {
children.forEach((child) => {
const position = {
x: (child.x || 0) + (pos ? pos.x : 0),
y: (child.y || 0) + (pos ? pos.y : 0),
}
let label: string = ''
if (typeof child.labels === 'string') {
label = child.labels
} else if (Array.isArray(child.labels) && child.labels[0]) {
label = child.labels[0].text
}
const node = graph.createNode({
shape: 'elk-node',
id: child.id,
position,
label,
size: {
width: child.width || 0,
height: child.height || 0,
},
ports: {
items: (child.ports || []).map((item) => {
portIdToNodeIdMap[item.id] = child.id
return {
id: item.id,
group: 'port',
args: {
x: item.x,
y: item.y,
},
size: {
width: item.width || 0,
height: item.height || 0,
},
}
}),
},
})
cells.push(node)
if (child.children) {
addChildren(child.children, position)
}
if (child.edges) {
addEdges(child.edges, position)
}
})
}
const addEdges = (edges: ElkEdge[], pos?: Position) => {
edges.forEach((edge: ElkExtendedEdge) => {
const { bendPoints = [] } = edge.sections[0]
if (pos) {
bendPoints.map((bendPoint: Position) => {
bendPoint.x += pos.x
bendPoint.y += pos.y
})
}
const sourcePortId = edge.sources[0]
const targetPortId = edge.targets[0]
const sourceNodeId = portIdToNodeIdMap[sourcePortId]
const targetNodeId = portIdToNodeIdMap[targetPortId]
cells.push(
graph.createEdge({
shape: 'elk-edge',
source: {
cell: sourceNodeId,
port: sourcePortId,
},
target: {
cell: targetNodeId,
port: targetPortId,
},
vertices: bendPoints,
}),
)
})
}
fetch('../data/elkdata.json')
.then((response) => response.json())
.then((data) => {
elk.layout(data).then((res) => {
addChildren(res.children || [])
addEdges(res.edges || [])
graph.resetCells(cells)
graph.zoomToFit({
padding: 10,
maxScale: 1,
})
})
})

View File

@ -48,6 +48,14 @@
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_43231b/afts/img/A*mUVrSJMkP1UAAAAAAAAAAAAAARQnAQ"
},
{
"filename": "elk.ts",
"title": {
"zh": "ELK",
"en": "ELK"
},
"screenshot": "https://gw.alipayobjects.com/mdn/rms_43231b/afts/img/A*Z3ebTKy0w9cAAAAAAAAAAAAAARQnAQ"
},
{
"filename": "validate-connection.ts",
"title": {

View File

@ -10,4 +10,4 @@ window.x6 = require('@antv/x6')
window.x6ReactShape = require('@antv/x6-react-shape')
window.layout = require('@antv/layout')
window.hierarchy = require('@antv/hierarchy')
window.elkjs = require('elkjs/lib/elk.bundled.js')

View File

@ -16,13 +16,14 @@
],
"dependencies": {
"@antv/gatsby-theme-antv": "^1.1.13",
"@antv/hierarchy": "^0.6.6",
"@antv/layout": "^0.1.9",
"@antv/x6": "^1.28.1",
"@antv/x6-react-components": "^1.1.15",
"@antv/x6-react-shape": "^1.5.2",
"@antv/layout": "^0.1.9",
"@antv/hierarchy": "^0.6.6",
"antd": "^4.4.2",
"dagre": "^0.8.5",
"elkjs": "^0.7.1",
"express": "^4.17.1",
"gatsby": "^2.24.2",
"gatsby-plugin-static-folders": "^1.0.1",

View File

@ -11419,6 +11419,11 @@ elegant-spinner@^1.0.1:
resolved "https://registry.npmjs.org/elegant-spinner/-/elegant-spinner-1.0.1.tgz#db043521c95d7e303fd8f345bedc3349cfb0729e"
integrity sha1-2wQ1IcldfjA/2PNFvtwzSc+wcp4=
elkjs@^0.7.1:
version "0.7.1"
resolved "https://registry.npmjs.org/elkjs/-/elkjs-0.7.1.tgz#4751c5e918a4988139baf7f214e010aea22de969"
integrity sha512-lD86RWdh480/UuRoHhRcnv2IMkIcK6yMDEuT8TPBIbO3db4HfnVF+1lgYdQi99Ck0yb+lg5Eb46JCHI5uOsmAw==
elliptic@^6.5.3:
version "6.5.4"
resolved "https://registry.npmjs.org/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb"
@ -21340,14 +21345,14 @@ moment@2.x, moment@^2.15.1, moment@^2.22.1, moment@^2.24.0, moment@^2.25.3, mome
resolved "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz#b2be769fa31940be9eeea6469c075e35006fa3d3"
integrity sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==
monaco-editor-webpack-plugin@2.0.0, monaco-editor-webpack-plugin@^2.0.0:
monaco-editor-webpack-plugin@^2.0.0:
version "2.0.0"
resolved "https://registry.nlark.com/monaco-editor-webpack-plugin/download/monaco-editor-webpack-plugin-2.0.0.tgz#196052f23678285deca7a7baf4d7aebe9d1ff9e1"
integrity sha1-GWBS8jZ4KF3sp6e69Neuvp0f+eE=
dependencies:
loader-utils "^2.0.0"
monaco-editor@*, monaco-editor@0.21.0, monaco-editor@^0.21.0:
monaco-editor@*, monaco-editor@^0.21.0:
version "0.21.0"
resolved "https://registry.nlark.com/monaco-editor/download/monaco-editor-0.21.0.tgz?cache=0&sync_timestamp=1623961471145&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fmonaco-editor%2Fdownload%2Fmonaco-editor-0.21.0.tgz#35d964cbeff8b035d574bb113e9491331810d5f1"
integrity sha1-Ndlky+/4sDXVdLsRPpSRMxgQ1fE=