logo

X6

  • 文档
  • API
  • 图表示例
  • 常见问题
  • 更新日志
  • XFlow
  • 所有产品antv logo arrow
  • 2.x
  • 画布
    • 画布
    • 网格
    • 背景
    • 画布平移
    • 画布缩放
    • 视口变换
    • 坐标系
  • 元素
    • 元素
    • 节点
    • 边
    • 标签
    • 箭头
    • 元素属性
    • 交互
  • MVC
    • 模型
    • 视图
  • 扩展
    • 节点工具
    • 边上工具
    • 路由
    • 连接器
    • 节点的锚点
    • 边的锚点
    • 连接点
    • 连接桩布局
    • 连接桩标签布局
    • 属性
    • 高亮器
    • 滤镜

连接器

上一篇
路由
下一篇
节点的锚点

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

连接器将起点、路由返回的点、终点加工为 <path> 元素的 d 属性,决定了边渲染到画布后的样式。X6 中内置了以下几种连接器。

连接器说明
normal简单连接器,用直线连接起点、路由点和终点。
smooth平滑连接器,用三次贝塞尔曲线线连接起点、路由点和终点。
rounded圆角连接器,用直线连接起点、路由点和终点,并在线段连接处用圆弧链接(倒圆角)。
jumpover跳线连接器,用直线连接起点、路由点和终点,并在边与边的交叉处用跳线符号链接。

可以为某条边设置路由:

const edge = graph.addEdge({
source,
target,
connector: {
name: 'rounded',
args: {
radius: 20,
},
},
})

当没有连接器参数时,可以简化为:

const edge = graph.addEdge({
source,
target,
connector: 'rounded',
})

也可以调用 edge.setConnector 来设置连接器:

edge.setConnector('rounded', { radius: 20 })

在创建画布时,可以通过 connecting 选项来设置全局默认连接器(默认为 'normal'):

new Graph({
connecting: {
connector: {
name: 'rounded',
args: {
radius: 20,
},
},
},
})

当路由没有参数时,也可以简化为:

new Graph({
connecting: {
connector: 'rounded',
},
})

下面我们一起来看看如何使用内置连接器,以及如何自定并注册自定义连接器。

内置连接器

normal

系统的默认连接器,将起点、路由点、终点通过直线按顺序连接。

支持的参数如下表:

参数名参数类型是否必选默认值参数说明
rawboolean否false是否返回一个 Path 对象,默认值为 false 返回序列化后的字符串。

smooth

平滑连接器,通过三次贝塞尔链接起点、路由点和终点。

支持的参数如下表:

参数名参数类型是否必选默认值参数说明
rawboolean否false是否返回一个 Path 对象,默认值为 false 返回序列化后的字符串。
directionH | V否-保持水平连接或者保持垂直连接,不设置会根据起点和终点位置动态计算。

例如:

graph.addEdge({
source: rect1,
target: rect2,
vertices: [
{ x: 100, y: 200 },
{ x: 300, y: 120 },
],
connector: 'smooth',
})

rounded

圆角连接器,将起点、路由点、终点通过直线按顺序连接,并在线段连接处通过圆弧连接(倒圆角)。

支持的参数如下表:

参数名参数类型是否必选默认值参数说明
radiusnumber否10倒角半径。
rawboolean否false是否返回一个 Path 对象,默认值为 false 返回序列化后的字符串。

例如:

graph.addEdge({
source: rect1,
target: rect2,
vertices: [
{ x: 100, y: 200 },
{ x: 300, y: 120 },
],
connector: {
name: 'rounded',
args: {
radius: 10,
},
},
})
Args
radius
10

jumpover

跳线连接器,用直线连接起点、路由点和终点,并在边与边的交叉处用跳线符号链接。

支持的参数如下表:

参数名参数类型是否必选默认值参数说明
type'arc' | 'gap' | 'cubic'否'arc'跳线类型。
sizenumber否5跳线大小。
radiusnumber否0倒角半径。
rawboolean否false是否返回一个 Path 对象,默认值为 false 返回序列化后的字符串。
Args
type
size
5
radius
0

自定义连接器

连接器是一个具有如下签名的函数,返回 Path 对象或序列化后的字符串。

export type Definition<T> = (
this: EdgeView, // 边的视图
sourcePoint: Point.PointLike, // 起点
targetPoint: Point.PointLike, // 终点
routePoints: Point.PointLike[], // 路由返回的点
args: T, // 参数
edgeView: EdgeView, // 边的视图
) => Path | string
参数名参数类型参数说明
thisEdgeView边的视图。
sourcePointPoint.PointLike起点。
targetPointPoint.PointLike终点。
routePointsPoint.PointLike[]路由返回的点。
argsT连接器参数。
edgeViewEdgeView边的视图。

我来定义一个 wobble 连接器:

export interface WobbleArgs {
spread?: number
raw?: boolean
}
function wobble(
sourcePoint: Point.PointLike,
targetPoint: Point.PointLike,
vertices: Point.PointLike[],
args: WobbleArgs,
) {
const spread = args.spread || 20
const points = [...vertices, targetPoint].map((p) => Point.create(p))
let prev = Point.create(sourcePoint)
const path = new Path(Path.createSegment('M', prev))
for (let i = 0, n = points.length; i < n; i += 1) {
const next = points[i]
const distance = prev.distance(next)
let d = spread
while (d < distance) {
const current = prev.clone().move(next, -d)
current.translate(
Math.floor(7 * Math.random()) - 3,
Math.floor(7 * Math.random()) - 3,
)
path.appendSegment(Path.createSegment('L', current))
d += spread
}
path.appendSegment(Path.createSegment('L', next))
prev = next
}
return args.raw ? path : path.serialize()
}

然后注册连接器:

Graph.registerConnector('wobble', wobble)

注册以后我们就可以通过连接器名称来使用:

edge.setConnector('wobble', { spread: 16 })