logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.49
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 节点通用配置项
      • 圆形节点 Circle
      • 菱形节点 Diamond
      • 甜甜圈节点 Donut
      • 椭圆形节点 Ellipse
      • 六边形节点 Hexagon
      • HTML节点 Html
      • 图片节点 Image
      • 矩形节点 Rect
      • 五角形节点 Star
      • 三角形节点 Triangle
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 边通用配置项
      • 三次贝塞尔曲线边 Cubic
      • 水平三次贝塞尔曲线边 CubicHorizontal
      • 垂直三次贝塞尔曲线边 CubicVertical
      • 直线边 Line
      • 二次贝塞尔曲线边 Quadratic
      • 折线边 Polyline
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 组合通用配置项
      • 圆形组合 Circle
      • 矩形组合 Rect
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 布局通用配置项
    • AntV Dagre 布局 AntvDagre
    • 环形布局 Circular
    • 复合布局 ComboCombined
    • 紧凑树布局 CompactBox
    • 同心圆布局 Concentric
    • 3D D3力导向布局 D3Force3D
    • D3力导向布局 D3Force
    • Dagre 布局
    • 生态树 Dendrogram
    • 鱼骨布局 Fishbone
    • ForceAtlas2 力导向布局
    • Force 力导向布局
    • Fruchterman 力导向布局
    • 网格布局 Grid
    • 缩进树 Indented
    • 高维数据降维布局 MDS
    • 脑图树 Mindmap
    • 径向布局 Radial
    • 随机布局 Random
    • 蛇形布局 Snake
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 缩放画布 ZoomCanvas
    • 标签自适应显示 AutoAdaptLabel
    • 框选 BrushSelect
    • 点击选中 ClickSelect
    • 展开/收起元素 CollapseExpand
    • 创建边 CreateEdge
    • 拖拽画布 DragCanvas
    • 拖拽元素 DragElement
    • 力导向拖拽元素 DragElementForce
    • 缩放画布时固定元素大小 FixElementSize
    • 聚焦元素 FocusElement
    • 悬停激活 HoverActivate
    • 套索选择 LassoSelect
    • 优化视口变换 OptimizeViewportTransform
    • 滚动画布 ScrollCanvas
    • 自定义交互
  • 插件 Plugin
    • 插件总览
    • 背景 Background
    • 气泡集 BubbleSets
    • 上下文菜单 Contextmenu
    • 边绑定 EdgeBundling
    • 边过滤镜 EdgeFilterLens
    • 鱼眼放大镜 Fisheye
    • 全屏展示 Fullscreen
    • 网格线 GridLine
    • 历史记录 History
    • 轮廓包围 Hull
    • 图例 Legend
    • 小地图 Minimap
    • 对齐线 Snapline
    • 时间条 Timebar
    • 工具栏 Toolbar
    • 提示框 Tooltip
    • 水印 Watermark
    • 自定义插件
  • 数据处理 Transform
    • 数据处理总览
    • 动态调整节点大小 MapNodeSize
    • 径向标签 PlaceRadialLabels
    • 平行边 ProcessParallelEdges
    • 自定义数据处理
  • 主题 Theme
    • 主题总览
    • 自定义主题
    • 色板
    • 自定义色板
  • 动画 Animation
    • 动画总览
    • 自定义动画
  • 扩展阅读
    • 事件
    • 渲染器
    • 坐标系
    • 下载图片
    • 使用 iconfont
    • 使用 3D
    • 项目打包
  • 版本特性
    • 新版本特性
    • 升级到 5.0
  • 常见问题
  • 参与贡献

折线边 Polyline

上一篇
二次贝塞尔曲线边 Quadratic
下一篇
自定义边

资源

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

概述

折线是由多条直线段组成的边,适合在复杂布局中绕过障碍物连接节点。

使用场景:

  • 适用于复杂布局的图,如电路图、管道图。

  • 当需要绕过其他节点或障碍物时使用。

在线体验

createGraph(
{
data: {
nodes: [
{
id: 'node1',
style: { x: 150, y: 150 },
},
{
id: 'node2',
style: {
x: 400,
y: 150,
labelText: 'Drag Me!',
labelPadding: [1, 5],
labelBackground: true,
labelBackgroundRadius: 10,
labelBackgroundFill: '#99add1',
},
},
],
edges: [
{
id: 'edge1',
source: 'node1',
target: 'node2',
text: 'polyline',
},
],
},
node: {
style: {
fill: '#f8f8f8',
stroke: '#8b9baf',
lineWidth: 1,
},
},
edge: {
type: 'polyline',
style: {
stroke: '#7e3feb',
lineWidth: 2,
labelText: (d) => d.text,
labelBackground: true,
labelBackgroundFill: '#f9f0ff',
labelBackgroundOpacity: 1,
labelBackgroundLineWidth: 2,
labelBackgroundStroke: '#7e3feb',
labelPadding: [1, 10],
labelBackgroundRadius: 4,
router: { type: 'orth' },
},
},
behaviors: ['drag-canvas', 'drag-element'],
plugins: [{ type: 'grid-line', size: 30 }],
},
{ width: 600, height: 300 },
(gui, graph) => {
gui.add({ type: 'polyline' }, 'type').disable();
let index = 3;
const options = {
radius: 0,
router: {
type: 'orth',
},
random: () => {
const x = Math.floor(Math.random() * 600);
const y = Math.floor(Math.random() * 300);
graph.addNodeData([
{
id: `node-${index}`,
style: {
size: 5,
fill: '#7e3feb',
x,
y,
},
},
]);
index++;
graph.updateEdgeData((prev) => {
const targetEdgeData = prev.find((edge) => edge.id === 'edge1');
const controlPoints = [...(targetEdgeData.style.controlPoints || [])];
controlPoints.push([x, y]);
return [{ ...targetEdgeData, style: { ...targetEdgeData.style, controlPoints } }];
});
graph.render();
},
};
const optionFolder = gui.addFolder('polyline.style');
optionFolder.add(options, 'radius', 0, 100, 1);
optionFolder.add(options, 'router');
optionFolder.add(options, 'random').name('Add random node as control points');
optionFolder.onChange(({ property, value }) => {
if (property === 'random') return;
graph.updateEdgeData([{ id: 'edge1', style: { [property]: value } }]);
graph.render();
});
},
);

设置 edge.type 为 polyline 以使用折线。

样式配置

如果元素有其特定的属性,我们将在下面列出。对于所有的通用样式属性,见 BaseEdge

属性描述类型默认值必选
controlPoints控制点数组,用于定义折线的转折点Point[][]
radius转折点圆角半径number0
router是否启用路由false | OrthRouter | ShortestPathRouterfalse

OrthRouter

属性描述类型默认值
type正交路由,通过在路径上添加额外的控制点,使得边的每一段都保持水平或垂直'orth'-
padding节点连接点与转角的最小距离Padding0

ShortestPathRouter

属性描述类型默认值
type最短路径路由,是正交路由 'orth' 的智能版本。该路由由水平或垂直的正交线段组成。采用 A* 算法计算最短路径,并支持自动避开路径上的其他节点(障碍)'shortest-path'-
offset节点锚点与转角的最小距离Padding0
gridSizegrid 格子大小number0
maxAllowedDirectionChange支持的最大旋转角度(弧度)number0
startDirections节点的可能起始方向Direction[]0
endDirections节点的可能结束方向Direction[]0
directionMap指定可移动的方向{ [key in Direction]: { stepX: number; stepY: number } }0
penalties表示在路径搜索过程中某些路径的额外代价。key 为弧度值,value 为代价{ [key: string]: number }0
distFunc指定计算两点之间距离的函数(p1: Point, p2: Point) => number0
maximumLoops最大迭代次数number0
enableObstacleAvoidance是否开启避障booleanfalse

Direction

type Direction = 'left' | 'right' | 'top' | 'bottom';

Point

type Point = [number, number] | [number, number, number] | Float32Array;

Padding

type Padding = number | [number, number] | [number, number, number, number];

示例

内置折线边效果

import { Graph } from '@antv/g6';
const data = {
nodes: [
{ id: 'node-1', style: { x: 200, y: 200 } },
{ id: 'node-2', style: { x: 350, y: 120 } },
],
edges: [
{
id: 'edge-1',
source: 'node-1',
target: 'node-2',
controlPoints: [[300, 190]],
},
],
};
const graph = new Graph({
container: 'container',
data,
edge: {
type: 'polyline',
style: {
controlPoints: (d) => d.controlPoints,
},
},
behaviors: [{ type: 'drag-element' }],
});
graph.render();