Loading...
Routing further processes the edge's waypoints vertices, adding additional points when necessary, and then returns the processed points (excluding the start and end points of the edge). For example, after orth
routing, each segment of the edge is a horizontal or vertical orthogonal segment.
X6 has the following built-in routing options.
Routing Name | Description |
---|---|
normal | Default routing, returns the waypoints as they are. |
orth | Orthogonal routing, composed of horizontal or vertical orthogonal segments. |
oneSide | Restricted orthogonal routing, composed of three restricted horizontal or vertical orthogonal segments. |
manhattan | Smart orthogonal routing, composed of horizontal or vertical orthogonal segments that automatically avoid other nodes (obstacles) on the path. |
metro | Smart subway line routing, composed of horizontal or vertical orthogonal segments and diagonal segments, similar to a subway map, and automatically avoids other nodes (obstacles) on the path. |
er | Entity-relationship routing, composed of zigzag diagonal segments. |
When using, you can set the routing for an edge:
const edge = graph.addEdge({source,target,router: {name: 'oneSide',args: {side: 'right',},},})
When the router has no parameters, it can also be simplified to:
const edge = graph.addEdge({source,target,router: 'oneSide',})
You can also call the method to set the routing:
edge.setRouter('oneSide', { side: 'right' })
When creating a canvas, you can set a global default routing through the connecting
option (the default routing for the canvas is 'normal'
):
new Graph({connecting: {router: {name: 'oneSide',args: {side: 'right',},},},})
When the router has no parameters, it can also be simplified to:
new Graph({connecting: {router: 'orth',},})
Now let's take a look at how to use the built-in routing and how to define and register custom routing.
The system's default routing, which returns the input vertices
path points as is.
Orthogonal routing, which adds extra points along the path to ensure that each line segment of the edge is horizontally or vertically orthogonal.
The supported parameters are as follows:
Parameter Name | Parameter Type | Required | Default Value | Description |
---|---|---|---|---|
padding | SideOptions | No | 20 | Sets the minimum distance from the anchor point to the corner. |
SideOptions
is defined as follows:
export type SideOptions =| number| {vertical?: numberhorizontal?: numberleft?: numbertop?: numberright?: numberbottom?: number}
For example:
graph.addEdge({source,target,vertices: [{ x: 100, y: 200 },{ x: 300, y: 120 },],router: {name: 'orth',args: {padding: {left: 50,},},},})
The oneSide
routing is a restricted version of the orthogonal routing orth
, which generates a strict three-segment route: starting from the side
side of the starting node, passing through the middle segment, and ending at the side
side of the target node. It is important to note that when using this routing, do not specify vertices
at the same time, as it will result in poor routing performance.
The supported parameters are as follows:
Parameter Name | Parameter Type | Required | Default Value | Description |
---|---|---|---|---|
side | 'left' | 'right' | 'top' | 'bottom' | No | 'bottom' | The starting/ending direction of the route, default is 'bottom' . |
padding | SideOptions | No | 20 | Sets the minimum distance from the anchor point to the corner. |
For example:
graph.addEdge({source,target,router: {name: 'oneSide',args: { side: 'right' },},})
The Manhattan routing 'manhattan'
is an intelligent version of the orthogonal routing 'orth'
, consisting of horizontal or vertical orthogonal line segments that automatically avoid other nodes (obstacles) along the path.
We provide a rich set of options for this routing algorithm:
Parameter Name | Parameter Type | Required | Default Value | Description |
---|---|---|---|---|
step | number | No | 10 | The step length of the routing algorithm; smaller values increase computation. It is recommended to use the canvas grid size. |
excludeTerminals | ('source' | 'target')[] | No | [] | Ignore starting or ending nodes; ignored nodes will not be considered as obstacles. |
excludeShapes | string[] | No | [] | Ignore specified shape nodes; ignored nodes will not be considered as obstacles. |
excludeNodes | (Node | string)[] | No | [] | Nodes to ignore; ignored nodes will not be considered as obstacles. |
startDirections | string[] | No | ['top', 'right', 'bottom', 'left'] | Supported directions to start routing. |
endDirections | string[] | No | ['top', 'right', 'bottom', 'left'] | Supported directions to end routing. |
padding | SideOptions | No | - | Sets the minimum distance from the anchor point to the corner. |
fallbackRouter | Router | No | Registry.Router.presets.orth | In scenarios where obstacles cannot be avoided, downgrade to the specified routing. |
For example:
graph.addEdge({source,target,router: {name: 'manhattan',args: {startDirections: ['top'],endDirections: ['bottom'],},},})
The characteristic of the manhattan routing is to automatically avoid obstacles in the path. If an unavoidable situation arises, it will automatically downgrade to the orth routing. In this case, to help developers identify the issue, a warning will be logged in the console: Unable to execute manhattan algorithm, use orth instead.
The metro routing metro
is a variant of the Manhattan routing manhattan
, consisting of horizontal or vertical orthogonal line segments and diagonal segments, similar to a subway map, and automatically avoids other nodes (obstacles) along the path. Its options are the same as manhattan, but the default value of maxDirectionChange
is 45
, indicating that the maximum slope angle of the routing line segment is 45
degrees.
For example:
graph.addEdge({source,target,router: {name: 'metro',args: {startDirections: ['top'],endDirections: ['bottom'],},},})
The entity-relationship routing er
consists of zigzag diagonal segments, commonly used to represent connections between entities in an ER diagram.
The supported parameters are as follows:
Parameter Name | Parameter Type | Required | Default Value | Description |
---|---|---|---|---|
offset | number | 'center' | No | 32 | The distance between the first and last points of the route and the nodes. When set to 'center' , the center of the node is used as the route point coordinate. |
min | number | No | 16 | The minimum distance between the first and last points of the route and the nodes. |
direction | 'T' | 'B' | 'L' | 'R' | 'H' | 'V' | No | - | The routing direction; if omitted, the optimal direction will be automatically selected. |
For example:
graph.addEdge({source,target,router: {name: 'er',args: {offset: 24,},},})
In addition to built-in routing, we can also create custom routing according to certain rules, for example, implementing random routing:
// Routing parametersinterface RandomRouterArgs {bounces?: number}function randomRouter(vertices: Point.PointLike[],args: RandomRouterArgs,view: EdgeView,) {const bounces = args.bounces || 20const points = vertices.map((p) => Point.create(p))for (var i = 0; i < bounces; i++) {const sourceCorner = view.sourceBBox.getCenter()const targetCorner = view.targetBBox.getCenter()const randomPoint = Point.random(sourceCorner.x,targetCorner.x,sourceCorner.y,targetCorner.y,)points.push(randomPoint)}return points}Graph.registerRouter('random', randomRouter)edge.setRouter('random', { bounces: 3 })