logo

X6

  • Tutorials
  • API
  • Examples
  • Q&A
  • Change Log
  • XFlow
  • Productsantv logo arrow
  • 2.x
  • Graph
    • Graph
    • Grid
    • Background
    • Panning
    • Mousewheel
    • Viewport Transformation
    • Coordinate Systems
  • Element
    • Cell
    • Node
    • Edge
    • Labels
    • Arrow
    • Element Attributes
    • Interaction
  • MVC
    • Model
    • View
  • Extension
    • Node Tools
    • Edge Tools
    • Routing
    • Connector
    • Node Anchor
    • Edge Anchor
    • Connection Point
    • Port Layout Algorithm
    • Port Label Layout
    • Attributes
    • Highlighter
    • Filter

Edge Tools

Previous
Node Tools
Next
Routing

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Since edges are typically narrow lines or curves, interacting with them can be inconvenient. To address this issue, we render a transparent <path> shape that matches the edge's shape but is wider, allowing users to interact with the edge more easily. Additionally, we can add small tools to edges to enhance their interactivity, such as vertex tools that allow path points to be moved, and segment tools that enable the movement of segments within the edge.

Scenario 1: Adding specified tools.

// Add tools when creating an edge
graph.addEdge({
source,
target,
tools: [
{ name: 'vertices' },
{
name: 'button-remove',
args: { distance: 20 },
},
],
})
// Add tools after creating an edge
edge.addTools([
{ name: 'vertices' },
{
name: 'button-remove',
args: { distance: 20 },
},
])

Scenario 2: Dynamically adding/removing tools with the mouse.

graph.on('edge:mouseenter', ({ cell }) => {
cell.addTools([
{ name: 'vertices' },
{
name: 'button-remove',
args: { distance: 20 },
},
])
})
graph.on('edge:mouseleave', ({ cell }) => {
if (cell.hasTool('button-remove')) {
cell.removeTool('button-remove')
}
})

In X6, the following tools for edges are provided by default:

  • vertices Vertex tool, renders a small dot at the position of the path point. Dragging the dot modifies the position of the path point, double-clicking the dot deletes the path point, and clicking on the edge adds a path point.
  • segments Segment tool. Renders a tool bar at the center of each segment of the edge, allowing the tool bar to be dragged to adjust the positions of the path points at both ends of the segment.
  • boundary Renders a rectangle surrounding the edge based on its bounding box. Note that this tool only renders a rectangle without any interactivity.
  • button Renders a button at a specified position, supporting custom click interactions for the button.
  • button-remove Renders a delete button at a specified position, which deletes the corresponding edge when clicked.
  • source-arrowhead and target-arrowhead Renders a shape (default is an arrow) at the start or end of the edge, allowing the shape to be dragged to modify the start or end of the edge.
  • edge-editor Provides text editing functionality on the edge.

Built-in Tools

vertices

The path point tool renders a small dot at the position of the path point. You can drag the dot to modify the path point's position, double-click the dot to delete the path point, and click on the edge to add a new path point. The configuration is as follows:

Parameter NameTypeDefault ValueDescription
attrsKeyValueobjectAttributes of the small dot.
snapRadiusnumber20The snap radius during the movement of the path point. When the path point is within the radius of a neighboring path point, it will snap to that point.
addablebooleantrueWhether new path points can be added by clicking the mouse on the edge.
removablebooleantrueWhether path points can be removed by double-clicking.
removeRedundanciesbooleantrueWhether to automatically remove redundant path points.
stopPropagationbooleantrueWhether to prevent mouse events on the tool from bubbling up to the edge view. If prevented, mouse interactions with the tool will not trigger the edge's mousedown, mousemove, and mouseup events.
modifiersstring | ModifierKey[]-Keys that must be pressed to add path points, which can resolve the overlap of interactions for adding path points and clicking on edges.

The default value (default style) for attrs is:

{
r: 6,
fill: '#333',
stroke: '#fff',
cursor: 'move',
'stroke-width': 2,
}

The tool is used as follows:

// Add the small tool when creating an edge
const edge1 = graph.addEdge({
...,
tools: [
{
name: 'vertices',
args: {
attrs: { fill: '#666' },
},
},
]
})

segments

The line segment tool renders a tool bar at the center of each line segment of the edge, allowing you to drag the tool bar to adjust the positions of the path points at both ends of the segment. The configuration is as follows:

Parameter NameTypeDefault ValueDescription
attrsobjectobjectAttributes of the element.
precisionnumber0.5The tool is rendered only when the coordinate difference of the X or Y axis of the two endpoints of the segment is less than precision. The default 0.5 means the tool is rendered only for vertical and horizontal segments.
thresholdnumber40The tool is rendered only when the segment length exceeds threshold.
snapRadiusnumber10The snap radius during the adjustment of the segment.
removeRedundanciesbooleantrueWhether to automatically remove redundant path points.
stopPropagationbooleantrueWhether to prevent mouse events on the tool from bubbling up to the edge view. If prevented, mouse interactions with the tool will not trigger the edge's mousedown, mousemove, and mouseup events.

The default value (default style) for attrs is:

{
width: 20,
height: 8,
x: -10,
y: -4,
rx: 4,
ry: 4,
fill: '#333',
stroke: '#fff',
'stroke-width': 2,
}

The tool is used as follows:

graph.addEdge({
...,
tools: [{
name: 'segments',
args: {
snapRadius: 20,
attrs: {
fill: '#444',
},
},
}]
})

boundary

Renders a rectangle surrounding the edge based on its bounding box. Note that this tool only renders a rectangle and does not provide any interaction. The configuration is as follows:

Parameter NameTypeDefault ValueDescription
tagNamestringrectThe type of shape to render.
paddingnumber10Margin.
attrsKeyValueobjectShape attributes.
useCellGeometrybooleantrueWhether to use geometric calculations to compute the element's bounding box. Enabling this will improve performance, but if there are accuracy issues, set it to false.

The default value (default style) for attrs is:

{
fill: 'none',
stroke: '#333',
'stroke-width': 0.5,
'stroke-dasharray': '5, 5',
'pointer-events': 'none',
}

The tool is used as follows:

graph.addEdge({
...,
tools: [
{
name: 'boundary',
args: {
padding: 5,
attrs: {
fill: '#7c68fc',
stroke: '#333',
'stroke-width': 0.5,
'fill-opacity': 0.2,
},
},
},
]
})

button

Renders a button at a specified position, supporting custom click interactions. The configuration is as follows:

Parameter NameTypeDefault ValueDescription
distancenumber | stringundefinedDistance or ratio from the start point.
offsetnumber | Point.PointLike0Offset based on distance.
rotatebooleanundefinedWhether to rotate with the edge.
markupMarkup.JSONMarkupundefinedMarkup definition for rendering the button.
onClick(args: {e: Dom.MouseDownEvent, cell: Cell, view: CellView }) => voidundefinedCallback function for button click.

The usage is as follows:

graph.addEdge({
...,
tools: [
{
name: 'button',
args: {
distance: -40,
onClick({ view }: any) {
//
},
},
},
],
})

button-remove

Renders a delete button at a specified position, which deletes the corresponding edge when clicked. It is a special case of the button tool above, so it supports all configurations of button. The usage is as follows:

graph.addEdge({
...,
tools: [
{
name: 'button-remove',
args: { distance: -40 },
},
]
})

source-arrowhead and target-arrowhead

Renders a shape (default is an arrow) at the start or end of the edge, allowing you to drag the shape to modify the start or end of the edge. The configuration is as follows:

Parameter NameTypeDefault ValueDescription
tagNamestringpathThe type of shape to render.
attrsAttr.SimpleAttrsobjectAttributes of the shape.

The default value for source-arrowhead attributes is:

{
d: 'M 10 -8 -10 0 10 8 Z',
fill: '#333',
stroke: '#fff',
'stroke-width': 2,
cursor: 'move',
}

The default value for target-arrowhead attributes is:

{
d: 'M -10 -8 10 0 -10 8 Z',
fill: '#333',
stroke: '#fff',
'stroke-width': 2,
cursor: 'move',
}

The tool is used as follows:

graph.on('edge:mouseenter', ({ cell }) => {
cell.addTools([
'source-arrowhead',
{
name: 'target-arrowhead',
args: {
attrs: {
fill: 'red',
},
},
},
])
})
graph.on('edge:mouseleave', ({ cell }) => {
cell.removeTools()
})

edge-editor

Provides text editing functionality on the edge. The configuration is as follows:

Parameter NameTypeDefault ValueDescription
labelAddablebooleantrueWhether to create a new label by clicking on a non-text position.
attrs/fontSizestring14Font size for editing text.
attrs/colorstring#000Font color for editing text.
attrs/fontFamilystringArial, helvetica, sans-serifFont for editing text.
attrs/backgroundColorstring#fffBackground color for the editing area.
getTextstring | (this: CellView, args: {cell: Cell}) => string-Method to get the original text; needs to be customized in custom markup scenarios.
setTextstring | (this: CellView, args: {cell: Cell, value: string}) => void-Method to set new text; needs to be customized in custom markup scenarios.

Note

Note that after version 2.8.0, there is no need to dynamically add tools in the double-click event, so event parameters do not need to be passed.

// Usage before version 2.8.0
graph.on('node:dblclick', ({ node, e }) => {
edge.addTools({
name: 'edge-editor',
args: {
event: e,
},
})
})
// Usage after version 2.8.0
edge.addTools({
name: 'edge-editor',
})

It is also important to note that if a custom markup is defined in the edge, it is often necessary to customize the getText and setText methods to correctly get and set the edited text. Both configurations support function and string forms; the function is straightforward, while the string is the property path of the text to get or set. Generally, it is recommended to use the string form so that the graph data can be fully serialized (since functions cannot be serialized), otherwise, issues may arise with the text editing functionality after rendering the canvas. For example:

edge.addTools({
name: 'edge-editor',
args: {
getText: 'a/b',
setText: 'c/d',
},
})

The above configuration indicates:

  • Get edited text: edge.attr('a/b')
  • Set edited text: edge.attr('c/d', value)

Custom Tools

Method One

Inherit from ToolItem to implement a tool class, which is more complex and requires understanding of the ToolItem class. You can refer to the source code of the built-in tools above; this will not be elaborated here.

Graph.registerEdgeTool('button', Button)

Method Two

Inherit from an already registered tool and modify the configuration based on that. We provide a static method define on the ToolItem base class to quickly implement inheritance and modify configurations.

import { Vertices } from '@antv/x6/es/registry/tool/vertices'
const RedVertices = Vertices.define<Vertices.Options>({
attrs: {
fill: 'red',
},
})
Graph.registerEdgeTool('red-vertices', RedVertices, true)

Additionally, we provide a quick way to inherit and specify default options for the Graph.registerEdgeTool method:

Graph.registerEdgeTool('red-vertices', {
inherit: 'vertices', // Base class name, using the name of the already registered tool.
attrs: {
// Other options, as default options for the inherited class.
fill: 'red',
},
})

Using this method, we can quickly define and register a circular endpoint circle-target-arrowhead:

Graph.registerEdgeTool('circle-target-arrowhead', {
inherit: 'target-arrowhead',
tagName: 'circle',
attrs: {
r: 18,
fill: '#31d0c6',
'fill-opacity': 0.3,
stroke: '#fe854f',
'stroke-width': 4,
cursor: 'move',
},
})