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...

连接桩布局算法是一个函数具有如下签名的函数,返回连接桩相对于节点的相对位置。例如,某节点在画布的位置是 { x: 30, y: 40 },如果返回的某个连接桩的位置是 { x: 2, y: 4 },那么该连接桩渲染到画布后的位置是 { x: 32, y: 44 }。

type Definition<T> = (
portsPositionArgs: T[], // 连接桩中指定的布局算法参数
elemBBox: Rectangle, // 节点的包围盒
groupPositionArgs: T, // group 中定义的默认布局算法参数
) => Result[]
interface Result {
position: Point.PointLike // 相对于节点的位置
angle?: number // 旋转角度
}

需要注意的是,配置连接桩 ports 时,我们只能通过 groups 选项来配置布局算法,而在 items 中可以提供可选的布局算法参数 args 来影响布局结果。

graph.addNode(
...,
ports: {
// 连接桩分组
groups: {
group1: {
position: {
name: 'xxx', // 布局算法名称
args: { }, // 布局算法的默认参数
},
},
},
// 连接桩定义
items: [
{
groups: 'group1',
args: { }, // 覆盖 group1 中指定的默认参数
},
],
},
)

下面我们一起来看看如何使用内置的连接桩布局算法,以及如何自定并注册自定义布局算法。

内置布局

absolute

绝对定位,通过 args 指定连接桩的位置。

interface AbsoluteArgs {
x?: string | number
y?: string | number
angle?: number
}
名称类型必选默认值描述
xstring | number0连接桩在 X 轴相对位置。
ystring | number0连接桩在 Y 轴相对位置。
anglenumber0连接桩旋转角度。

当 x 和 y 为百分比字符串或位于 [0, 1] 之间时,表示在宽度和高度方向的百分比偏移量,否则表示绝对偏移量。

graph.addNode({
ports: {
groups: {
group1: {
position: {
name: 'absolute',
args: { x: 0, y: 0 },
},
},
},
items: [
{
group: 'group1',
args: {
x: '60%',
y: 32,
angle: 45,
},
},
],
},
})
Args
x
0.6
y
32
angle
45

left, right, top, bottom

连接桩沿矩形指定边线均匀布局,left、right、top 和 bottom 四个布局对矩形形状的节点非常友好,可以通过 args 来设置偏移量和旋转角度。

interface SideArgs {
dx?: number
dy?: number
angle?: number
x?: number
y?: number
}
名称类型必选默认值描述
strictbooleanfalse是否严格等分均匀分布。
dxnumber0沿 X 轴方向的偏移量。
dynumber0沿 Y 轴方向的偏移量。
anglenumber0连接桩的旋转角度。
xnumber-用指定的 X 坐标覆盖计算结果中的 X 坐标。
ynumber-用指定的 Y 坐标覆盖计算结果中的 Y 坐标。
graph.addNode({
ports: {
groups: {
group1: {
position: 'left',
},
},
items: [
{
group: 'group1',
args: {
dx: 2,
},
},
],
},
})
Args
position
strict
dx
0
dy
0
angle
45

line

连接桩沿线段均匀分布。

interface LineArgs {
start?: Point.PointLike
end?: Point.PointLike
dx?: number
dy?: number
angle?: number
x?: number
y?: number
}
名称类型必选默认值描述
startPoint.PointLike线段起点。
endPoint.PointLike线段终点。
strictbooleanfalse是否严格等分均匀分布。
dxnumber0沿 X 轴方向的偏移量。
dynumber0沿 Y 轴方向的偏移量。
anglenumber0连接桩的旋转角度。
xnumber-用指定的 X 坐标覆盖计算结果中的 X 坐标。
ynumber-用指定的 Y 坐标覆盖计算结果中的 Y 坐标。
graph.addNode({
ports: {
groups: {
group1: {
position: {
name: 'line',
args: {
start: { x: 10, y: 10 },
end: { x: 210, y: 10 },
},
},
},
},
items: [
{
group: 'group1',
args: {
dx: 2,
},
},
],
},
})
Args
strict
dx
0
dy
0
angle
45

ellipse

沿圆弧分布的连接桩,从 start 指定的角度开始,以 step 为步长均匀分布。

interface EllipseArgs {
start?: number
step?: number
compensateRotate?: boolean
dr?: number
dx?: number
dy?: number
angle?: number
x?: number
y?: number
}
名称类型必选默认值描述
startnumber起始角度。
stepnumber20步长。
compensateRotatenumberfalse是否沿圆弧修正连接桩的旋转角度。
drnumber0沿半径方向的偏移量。
dxnumber0沿 X 轴方向的偏移量。
dynumber0沿 Y 轴方向的偏移量。
anglenumber0连接桩的旋转角度。
xnumber-用指定的 X 坐标覆盖计算结果中的 X 坐标。
ynumber-用指定的 Y 坐标覆盖计算结果中的 Y 坐标。
const node = graph.addNode({
ports: {
groups: {
group1: {
position: {
name: 'ellipse',
args: {
start: 45,
},
},
},
},
},
})
Array.from({ length: 10 }).forEach((_, index) => {
node.addPort({
id: `${index}`,
group: 'group1',
attrs: { text: { text: index } },
})
})
Args
start
45
step
20
dr
0
dx
0
dy
0
angle
45

ellipseSpread

沿椭圆均匀分布的连接桩,从 start 指定的角度开始均匀分布。

interface EllipseSpreadArgs {
start?: number
compensateRotate?: boolean
dr?: number
dx?: number
dy?: number
angle?: number
x?: number
y?: number
}
名称类型必选默认值描述
startnumber起始角度。
compensateRotatenumberfalse是否沿圆弧修正连接桩的旋转角度。
drnumber0沿半径方向的偏移量。
dxnumber0沿 X 轴方向的偏移量。
dynumber0沿 Y 轴方向的偏移量。
anglenumber0连接桩的旋转角度。
xnumber-用指定的 X 坐标覆盖计算结果中的 X 坐标。
ynumber-用指定的 Y 坐标覆盖计算结果中的 Y 坐标。
const node = graph.addNode({
ports: {
groups: {
group1: {
position: {
name: 'ellipseSpread',
args: {
start: 45,
},
},
},
},
},
})
Array.from({ length: 36 }).forEach(function (_, index) {
ellipse.addPort({
group: 'group1',
id: `${index}`,
attrs: { text: { text: index } },
})
})
Args
start
45
dr
0
dx
0
dy
0
angle
45

自定义连接桩布局

连接桩布局算法是一个函数具有如下签名的函数,返回每个连接桩相对于节点的相对位置。例如,某节点在画布的位置是 { x: 30, y: 40 },如果返回的某个连接桩的位置是 { x: 2, y: 4 },那么该连接桩渲染到画布后的位置是 { x: 32, y: 44 }。

type Definition<T> = (
portsPositionArgs: T[], // 连接桩中指定的布局算法参数
elemBBox: Rectangle, // 节点的包围盒
groupPositionArgs: T, // group 中定义的默认布局算法参数
) => Result[]
interface Result {
position: Point.PointLike // 相对于节点的位置
angle?: number // 旋转角度
}

所以我们可以按照上面规则来创建自定义布局算法,例如,实现一个正弦分布的布局算法:

function sin(portsPositionArgs, elemBBox) {
return portsPositionArgs.map((_, index) => {
const step = -Math.PI / 8
const y = Math.sin(index * step) * 50
return {
position: {
x: index * 12,
y: y + elemBBox.height,
},
angle: 0,
}
})
}

布局算法实现后,需要注册到系统,注册后就可以像内置布局算法那样来使用。

Graph.registerPortLayout('sin', sin)

注册以后,我们就可以像内置布局算法那样来使用:

const rect = graph.addNode({
ports: {
groups: {
sin: {
position: {
name: 'sin',
args: {
start: 45,
},
},
attrs: {
rect: {
fill: '#fe854f',
width: 11,
},
text: {
fill: '#fe854f',
},
circle: {
fill: '#fe854f',
r: 5,
magnet: true,
},
},
},
},
},
})
Array.from({ length: 24 }).forEach(() => {
rect.addPort({ group: 'sin' })
})