Edges
Previous
Nodes
Next
Connection Pile
Loading...
Both nodes and edges share a common base class Cell. In addition to inheriting properties from Cell
, edges support the following options.
Property Name | Type | Default Value | Description |
---|---|---|---|
source | TerminalData | - | Source node or starting point. |
target | TerminalData | - | Target node or endpoint. |
vertices | Point.PointLike[] | - | Path points. |
router | RouterData | - | Router. |
connector | ConnectorData | - | Connector. |
labels | Label[] | - | Labels. |
defaultLabel | Label | Default Label | Default label. |
graph.addEdge({shape: 'edge',source: 'node1',target: 'node2',})
Let's take a look at how to use the configurations mentioned above.
The source and target nodes (points) of the edge.
graph.addEdge({source: rect1, // Source nodetarget: rect2, // Target node})graph.addEdge({source: 'rect1', // Source node IDtarget: 'rect2', // Target node ID})graph.addEdge({source: { cell: rect1, port: 'out-port-1' }, // Source node and connection port IDtarget: { cell: 'rect2', port: 'in-port-1' }, // Target node ID and connection port ID})graph.addEdge({source: 'rect1', // Source node IDtarget: { x: 100, y: 120 }, // Target point})
Path points. The edge starts from the starting point, passes through the path points in order, and finally reaches the endpoint.
graph.addEdge({source: rect1,target: rect2,vertices: [{ x: 100, y: 200 },{ x: 300, y: 120 },],})
The router
will further process the vertices
, adding additional points if necessary, and then return the processed points. For example, after processing with orth routing, each link segment of the edge will be horizontal or vertical.
graph.addEdge({source: rect1,target: rect2,vertices: [{ x: 100, y: 200 },{ x: 300, y: 120 },],// If there are no args parameters, it can be simplified to router: 'orth'router: {name: 'orth',args: {},},})
X6 provides the following routing options by default. Click the links below to see how each routing option is used.
Additionally, we can register custom routers. For more details, please refer to the Custom Router tutorial.
The connector
processes the points returned by the router
into the pathData needed for rendering the edge. For example, the rounded
connector will round the corners between the lines.
graph.addEdge({source: rect1,target: rect2,vertices: [{ x: 100, y: 200 },{ x: 300, y: 120 },],router: 'orth',// If there are no args parameters, it can be simplified to connector: 'rounded'connector: {name: 'rounded',args: {},},})
X6 provides the following connector options by default. Click the links below to see how each connector is used.
Additionally, we can register custom connectors. For more details, please refer to the Custom Connector tutorial.
Used to set label text, position, style, etc. Supports multiple labels in array form, and each item specified in labels
will be used after being merged with the defaultLabel.
const edge = graph.addEdge({source: rect1,target: rect2,labels: [{attrs: {label: {text: 'edge',},},},],})// Orconst edge = graph.addEdge({source: rect1,target: rect2,labels: ['edge'], // Multiple labels can be set through labels, and when only setting label text, this syntax can be simplified})// Orconst edge = graph.addEdge({source: rect1,target: rect2,label: 'edge', // A single label can be set through label, and when only setting label text, this syntax can be simplified})
In addition to setting text, you can also create complex shapes on the edge using Label, which we will detail in the API.
Default label. The default label can simplify the label configuration items, and each item specified in labels
will be used after being merged with defaultLabel
.
We define two special properties, sourceMarker
and targetMarker
, to customize the starting and ending arrows of the edge. For example, for Shape.Edge
, we can specify the starting and ending arrows using the line
selector.
X6 provides the following built-in arrows. When using them, you only need to specify the arrow name and parameters (optional).
graph.addEdge({shape: 'edge',source: [100, 100],target: [500, 500],attrs: {line: {sourceMarker: 'block', // Solid arrowtargetMarker: {name: 'ellipse', // Ellipserx: 10, // X radius of the ellipse arrowry: 6, // Y radius of the ellipse arrow},},},})
By default, X6 edges come with a classic
arrow. If you want to remove it, you can set targetMarker
to null
.
We can also render arrows using SVG elements specified by tagName
. For example, below we use the <path>
element to render the arrow, which inherits the edge's fill color fill
and border color stroke
by default.
graph.addEdge({shape: 'edge',source: [100, 100],target: [500, 500],attrs: {line: {sourceMarker: {tagName: 'path',d: 'M 20 -10 0 0 20 10 Z',},targetMarker: {tagName: 'path',fill: 'yellow', // Use custom fill colorstroke: 'green', // Use custom border colorstrokeWidth: 2,d: 'M 20 -10 0 0 20 10 Z',},},},})
Our starting and ending arrows use the same d
attribute because we automatically calculate the arrow direction. In simple terms, when defining the arrow, we only need to define an arrow that points towards the origin.
For more examples and customization tips for arrows, please refer to the API.
Like nodes, we can customize the shape and style of edges using markup
and attrs
, and we can also register custom edges for reuse. The default edge Shape.Edge
in X6 defines two selectors: line
(representing the path element) and wrap
(representing a transparent path element for interaction). We can define the style of the edge as shown below.
Similar to nodes, after rendering is complete, we can modify all properties of edges through the API. We commonly use the following two methods:
Let's take a look at the prop
of the default edge provided by X6.
const edge = graph.addEdge({source: [200, 140],target: [500, 140],label: 'edge',})console.log(edge.prop())// Output{"shape": "edge","attrs": {"lines": {"connection": true,"strokeLinejoin": "round"},"wrap": {"strokeWidth": 10},"line": {"stroke": "#333","strokeWidth": 2,"targetMarker": "classic"}},"id": "9d5e4f54-1ed3-429e-8d8c-a1526cff2cd8","source": {"x": 200,"y": 140},"target": {"x": 500,"y": 140},"labels": [{"attrs": {"label": {"text": "edge"}}}],"zIndex": 1}
From the output above, we can see that prop
is a new configuration after processing, and its values can be updated through methods. After updating, the edge will immediately refresh to the latest state. To modify the edge's attrs
more conveniently, X6 provides the attr
method.
edge.prop('target', { x: 300, y: 300 }) // Modify the endpointedge.attr('line/stroke', '#ccc') // Modify the edge color, equivalent to edge.prop('attrs/line/stroke', '#ccc')