logo

X6

  • 文档
  • API
  • 图表示例
  • 常见问题
  • 更新日志
  • XFlow
  • 所有产品antv logo arrow
  • 2.x
  • 简介
  • 快速上手
  • 基础
    • 画布
    • 节点
    • 边
    • 连接桩
    • 交互
    • 事件
    • 数据
  • 进阶
    • 连接点
    • 工具
    • 群组
    • React 节点
    • Vue 节点
    • Angular 节点
    • HTML 节点
  • 插件
    • 图形变换
    • 对齐线
    • 复制粘贴
    • 快捷键
    • 撤销重做
    • 框选
    • 滚动画布
    • 小地图
    • Dnd
    • Stencil
    • 导出
  • 升级到 2.x 版本
  • 开发者工具

框选

上一篇
撤销重做
下一篇
滚动画布

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

在本章节中主要介绍框选插件相关的知识,通过阅读你可以了解到

  • 如何开启选择交互

使用

我们提供了一个独立的插件包 @antv/x6-plugin-selection 来使用框选功能。

# npm
$ npm install @antv/x6-plugin-selection --save
# yarn
$ yarn add @antv/x6-plugin-selection

然后我们在代码中这样使用:

import { Selection } from '@antv/x6-plugin-selection'
const graph = new Graph({
background: {
color: '#F2F7FA',
},
})
graph.use(
new Selection({
enabled: true,
}),
)

演示

  • 点击选中节点。
  • 启用多选,按住 Ctrl/Command 后点击节点多选。
  • 启用移动,拖动选框移动节点。
  • 启用框选,在画布空白位置按下鼠标左键,拖动选框来框选节点。
  • 启用严格框选模式(strict),观察对框选的影响。
  • 选择与框选配合使用的修饰键,如 alt 键,按住 alt 键并画布空白位置按下鼠标左键,拖动选框来框选节点。
  • 应用自定义过滤器(排除 circle 节点),圆形节点不能被选中。
  • 应用自定义附加内容(显示选中节点个数),选择两个及以上的节点,触发显示自定义内容。
Selection Settings
Enable Multiple Select
Selection Movable
Enable Rubberband
Is Strict Contains
Modifier Key

配置

属性名类型默认值必选描述
classNamestring-附加样式名,用于定制样式
multiplebooleantrue是否启用点击多选,启用后按住 ctrl 或 command 键点击节点实现多选
multipleSelectionModifiersModifierKey['ctrl', 'meta']用于设置上面点击多选配套的修饰键
rubberbandbooleanfalse是否启用框选节点功能
modifiersModifierKey-用于设置上面框选配套的修饰键
strictbooleanfalse选框是否需要完全包围节点时才选中节点
movablebooleantrue拖动选框时框选的节点是否一起移动
contentstring-设置附加显示的内容
filterFilter-节点过滤器
showNodeSelectionBoxbooleanfalse是否显示节点的选择框
showEdgeSelectionBoxbooleanfalse是否显示边的选择框
pointerEventsnode | autoauto如果打开 showNodeSelectionBox 时,会在节点上方盖一层元素,导致节点的事件无法响应,此时可以配置 pointerEvents: none 来解决,默认值是 auto
eventTypesSelectionEventType[]['leftMouseDown', 'mouseWheelDown']用于设置框选的触发事件类型

Filter 的类型定义如下:

type Filter = string[] | { id: string }[] | (this: Graph, cell: Cell) => boolean
  • string[]: 节点 shape 数组,指定的节点/边 shape 才能被选中
  • ({ id: string })[]: 节点 ID 数组,指定的节点/边才能被选中
  • (this: Graph, cell: Cell) => boolean: 返回 true 的节点/边才能被选中

ModifierKey 的类型定义如下:

type ModifierKey = string | ('alt' | 'ctrl' | 'meta' | 'shift')[] | null

X6 中修饰键包括 alt、ctrl、meta、shift 四个,设置修饰键后需要点击鼠标并按下修饰键才能触发相应的行为。修饰键在某些场景下非常有用,比如同时开始框选和拖拽画布时,而框选和拖拽画布的触发时机都是鼠标左键在画布空白位置按下,这时可以为框选和拖拽画布设置不一样的修饰键,达到同时开启又不冲突的效果。支持配置单个(如 alt)或多个(如 ['alt', 'ctrl'])修饰键,通过数组形式配置的多个修饰键是或关系,比如刚刚配置的修饰键表示按下 alt 或 ctrl,如果需要更加灵活的配置,可以使用如下这些形式:

  • alt 表示按下 alt。
  • [alt, ctrl], 表示按下 alt 或 ctrl。
  • alt|ctrl 表示按下 alt 或 ctrl。
  • alt&ctrl 表示同时按下 alt 和 ctrl。
  • alt|ctrl&shift 表示同时按下 alt 和 shift 或者同时按下 ctrl 和 shift。

SelectionEventType 的类型定义如下:

type SelectionEventType = 'leftMouseDown' | 'mouseWheelDown';

触发框选的交互方式。支持2种形式或者他们之间的组合:

  • leftMouseDown: 按下鼠标左键移动进行拖拽
  • mouseWheelDown: 按下鼠标滚轮进行拖拽

API

graph.select(...)

select(cells: Cell | string | (Cell | string)[]): this

选中指定的节点/边。需要注意的是,该方法不会取消选中当前选中的节点/边,而是将指定的节点/边追加到选区中。如果同时需要取消选中当前选中的节点/边,请使用 resetSelection(...) 方法。

graph.unselect(...)

unselect(cells: Cell | string | (Cell | string)[]): this

取消选中指定的节点/边。

graph.isSelected(...)

isSelected(cell: Cell | string): boolean

返回指定的节点/边是否被选中。

graph.resetSelection(...)

resetSelection(cells?: Cell | string | (Cell | string)[]): this

先清空选区,然后选中提供的节点/边。

graph.getSelectedCells()

getSelectedCells(): Cell[]

获取选中的节点/边。

graph.cleanSelection()

cleanSelection(): this

清空选区。

graph.isSelectionEmpty()

cleanSelection(): boolean

返回选区是否为空。

graph.isSelectionEnabled()

isSelectionEnabled(): boolean

是否启用选择能力。

graph.enableSelection()

enableSelection(): this

启用选择能力。

graph.disableSelection()

disableSelection(): this

禁用选择能力。

graph.toggleSelection(...)

toggleSelection(enabled?: boolean): this

切换选择的启用状态。参数如下:

名称类型必选默认值描述
enabledboolean-是否启用选择能力,缺省时切换选择的启用状态。

graph.isMultipleSelection()

isMultipleSelection(): boolean

是否启用了多选。

graph.enableMultipleSelection()

enableMultipleSelection(): this

启用多选。

graph.disableMultipleSelection()

disableMultipleSelection(): this

禁用多选。

graph.toggleMultipleSelection(...)

toggleMultipleSelection(multiple?: boolean): this

切换多选的启用状态。参数如下:

名称类型必选默认值描述
multipleboolean-是否启用多选,缺省时切换多选的启用状态。

graph.isSelectionMovable()

isSelectionMovable(): boolean

返回选中的节点/边是否可以被移动。

graph.enableSelectionMovable()

enableSelectionMovable(): this

启用选中的节点/边的移动。

graph.disableSelectionMovable()

disableSelectionMovable(): this

禁用选中节点/边的移动。

graph.toggleSelectionMovable(...)

toggleSelectionMovable(enabled?: boolean): this

切换选中节点/边是否可以被移动。参数如下:

名称类型必选默认值描述
enabledboolean-是否启用选中的节点/边的移动,缺省时切换启用状态。

graph.isRubberbandEnabled()

isRubberbandEnabled(): boolean

返回是否启用了框选。

graph.enableRubberband()

enableRubberband(): this

启用框选。

graph.disableRubberband()

disableRubberband(): this

禁用框选。

graph.toggleRubberband(...)

toggleRubberband(enabled?: boolean): this

切换框选的启用状态。参数如下:

名称类型必选默认值描述
enabledboolean-是否启用框选,缺省时切换启用状态。

graph.isStrictRubberband()

isStrictRubberband(): boolean

返回是否启用了严格框选。启用严格框选后,只有节点/边被选框完全包围时才会选中节点/边。

graph.enableStrictRubberband()

enableStrictRubberband(): this

启用严格框选。启用严格框选后,只有节点/边被选框完全包围时才会选中节点/边。

graph.disableStrictRubberband()

disableStrictRubberband(): this

禁用严格框选。禁用严格框选后,只需要选框与节点/边的包围盒相交即可选中节点/边。

graph.toggleStrictRubberband(...)

toggleStrictRubberband(enabled?: boolean): this

切换严格框选的启用状态。参数如下:

名称类型必选默认值描述
enabledboolean-是否启用严格框选,缺省时切换启用状态。

graph.setSelectionFilter(...)

setSelectionFilter(
filter?:
| null
| (string | { id: string })[]
| ((this: Graph, cell: Cell) => boolean)
): this

设置选择的过滤条件,满足过滤条件的节点/边才能被选中。

graph.setRubberbandModifiers(...)

setRubberbandModifiers(modifiers?: string | ModifierKey[] | null): this

设置框选的修饰键,只有同时按下修饰键时才能触发框选。

graph.setSelectionDisplayContent(...)

setSelectionDisplayContent(
content?:
| null
| false
| string
| ((this: Graph, selection: Selection, contentElement: HTMLElement) => string)
): this

设置选中节点/边的附加显示内容。

事件

事件名称参数类型描述
cell:selected{ cell: Cell; options: Model.SetOptions }节点/边被选中时触发
node:selected{ node: Node; options: Model.SetOptions }节点被选中时触发
edge:selected{ edge: Edge; options: Model.SetOptions }边被选中时触发
cell:unselected{ cell: Cell; options: Model.SetOptions }节点/边被取消选中时触发
node:unselected{ node: Node; options: Model.SetOptions }节点被取消选中时触发
edge:unselected{ edge: Edge; options: Model.SetOptions }边被取消选中时触发
selection:changed{added: Cell[]; removed: Cell[]; selected: Cell[]; options: Model.SetOptions}选中的节点/边发生改变(增删)时触发
graph.on('node:selected', ({ node }) => {
console.log(node)
})
// 我们也可以在插件实例上监听事件
selection.on('node:selected', ({ node }) => {
console.log(node)
})