logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.47
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 内置节点
        • 节点通用配置项
        • Diamond 菱形
        • Donut 甜甜圈
        • Ellipse 椭圆形
        • Hexagon 六边形
        • Html HTML
        • Image 图片
        • Rect 矩形
        • Star 五角形
        • Triangle 三角形
        • Circle 圆形
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 内置边
        • 边通用配置项
        • Cubic 三次贝塞尔曲线
        • CubicHorizontal 水平三次贝塞尔曲线
        • CubicVertical 垂直三次贝塞尔曲线
        • Line 直线
        • Polyline 折线
        • Quadratic 二次贝塞尔曲线
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 内置组合
        • 组合配置项
        • Circle 圆形
        • Rect 矩形
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 内置布局
      • 布局通用配置项
      • AntvDagre 布局
      • Circular 环形布局
      • ComboCombined 复合布局
      • CompactBox 紧凑树
      • Concentric 同心圆布局
      • D3Force 力导向布局
      • D3Force3D 3D 力导向布局
      • Dagre 布局
      • Dendrogram 生态树
      • Fishbone 鱼骨布局
      • Force 力导向布局
      • ForceAtlas2 力导向布局
      • Fruchterman 力导向布局
      • Grid 网格布局
      • Indented 缩进树
      • MDS 高维数据降维布局
      • Mindmap 脑图树
      • Radial 径向布局
      • Random 随机布局
      • Snake 蛇形布局
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 内置交互
      • AutoAdaptLabel 标签自适应显示
      • BrushSelect 框选
      • ClickSelect 点击选中
      • CollapseExpand 展开/收起元素
      • CreateEdge 创建边
      • DragCanvas 拖拽画布
      • DragElement 拖拽元素
      • DragElementForce 力导向拖拽元素
      • FixElementSize 缩放画布时固定元素大小
      • FocusElement 聚焦元素
      • HoverActivate 悬停激活
      • LassoSelect 套索选择
      • OptimizeViewportTransform 优化视口变换
      • ScrollCanvas 滚动画布
      • ZoomCanvas 缩放画布
    • 自定义交互
  • 插件 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
  • 常见问题
  • 参与贡献

BrushSelect 框选

上一篇
AutoAdaptLabel 标签自适应显示
下一篇
ClickSelect 点击选中

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

概述

鼠标点击拖一个框笼罩元素,框选范围内的元素会被选中。

使用场景

这一交互主要用于:

  • 快速选中一批元素
  • 快速取消选中一批元素

在线体验

createGraph(
{
data: {
nodes: [
{ id: 'node-1', style: { x: 200, y: 100 } },
{ id: 'node-2', style: { x: 360, y: 100 } },
{ id: 'node-3', style: { x: 280, y: 220 } },
],
edges: [
{ source: 'node-1', target: 'node-2' },
{ source: 'node-1', target: 'node-3' },
{ source: 'node-2', target: 'node-3' },
],
},
node: {
style: { fill: '#7e3feb' },
state: {
custom: { fill: '#ffa940' },
},
},
edge: {
stroke: '#8b9baf',
state: {
custom: { stroke: '#ffa940' },
},
},
behaviors: [
{
type: 'brush-select',
key: 'brush-select',
},
],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
key: 'brush-select',
type: 'brush-select',
animation: false,
enable: true,
enableElements: ['node', 'edge', 'combo'],
immediately: false,
mode: 'default',
state: 'selected',
trigger: 'shift+drag',
};
const optionFolder = gui.addFolder('BrushSelect Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'animation');
optionFolder.add(options, 'enable');
optionFolder.add(options, 'enableElements', [
['node', 'edge', 'combo'],
['node', 'edge'],
['node', 'combo'],
['combo', 'edge'],
['node'],
['edge'],
['combo'],
]);
optionFolder.add(options, 'trigger', {
'shift+drag': ['shift'],
drag: [],
});
optionFolder.add(options, 'state', ['active', 'selected', 'custom']);
optionFolder.add(options, 'mode', ['union', 'intersect', 'diff', 'default']).onChange((e) => {
immediately.show(e === 'default');
});
const immediately = optionFolder.add(options, 'immediately');
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'brush-select',
[property]: value,
});
graph.render();
});
},
);

基本用法

在图配置中添加这一交互:

1. 快速配置(静态)

使用字符串形式直接声明,这种方式简洁但仅支持默认配置,且配置后不可动态修改:

const graph = new Graph({
// 其他配置...
behaviors: ['brush-select'],
});

2. 对象配置(推荐)

使用对象形式进行配置,支持自定义参数,且可以在运行时动态更新配置:

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'brush-select',
key: 'brush-select-1',
immediately: true, // 可以看到框框笼罩过去时,元素立即被框选了
trigger: ['shift', 'alt', 'control'], // 配合多种按键进行框选
},
],
});

配置项

配置项说明类型默认值必选
type交互类型名称。此插件已内置,你可以通过 type: 'brush-select' 来使用它。brush-select | stringbrush-select✓
animation是否启用动画booleanfalse
enable是否启用框选功能boolean | ((event: Event) => boolean)true
enableElements可框选的元素类型( node | edge | combo )[][node, combo, edge]
immediately是否及时框选, 仅在 框选模式 mode 为 default 时生效,示例booleanfalse
mode框选的选择模式,示例union | intersect | diff | defaultdefault
onSelect框选元素状态回调(states:Record<string,string|string[]>) =>Record<string,string|string[]>
state被选中时切换到该状态string | selected | active | inactive | disabled | highlightselected
style指定框选时的框样式,配置项见下文
trigger按下该快捷键配合鼠标点击进行框选 按键参考: MDN Key Values 。若设为空数组时则表示鼠标点击进行框选,不需要按下其他按键配合
⚠️ 注意,trigger 设置为 ['drag'] 时会导致 drag-canvas 行为失效。两者不可同时配置。
string[] | (Control | Shift| Alt | ......)[][shift]

immediately

是否及时框选, 仅在框选模式为 default 时生效

const graph = new Graph({
behaviors: [
{
type: 'brush-select',
key: 'brush-select',
immediately: true, // 可以看到框框笼罩过去时,元素立即被框选了
trigger: [], // 不需要配合其他按键,点击鼠标拖动即可框选
},
],
});

createGraph(
{
data: {
nodes: [
{ id: 'node-1', style: { x: 100, y: 50 } },
{ id: 'node-2', style: { x: 260, y: 50 } },
{ id: 'node-3', style: { x: 280, y: 100 } },
],
edges: [
{ source: 'node-1', target: 'node-2' },
{ source: 'node-1', target: 'node-3' },
{ source: 'node-2', target: 'node-3' },
],
},
node: {
style: { fill: '#7e3feb' },
},
edge: {
stroke: '#8b9baf',
},
behaviors: [
{
type: 'brush-select',
key: 'brush-select',
immediately: true, // 立即框选
trigger: [],
},
],
plugins: [{ type: 'grid-line', size: 30 }],
},
{ width: 400, height: 200 },
);

mode

内置支持四种框选的选择模式:

  • union:保持已选元素的当前状态,并添加指定的 state 状态。
  • intersect:如果已选元素已有指定的 state 状态,则保留;否则清除该状态。
  • diff:对已选元素的指定 state 状态进行取反操作。
  • default:清除已选元素的当前状态,并添加指定的 state 状态。
const graph = new Graph({
behaviors: [
{
type: 'brush-select',
key: 'brush-select',
mode: 'default', // 框选模式, 默认框选模式
},
],
});

createGraph(
{
data: {
nodes: [
{ id: 'node-1', style: { x: 200, y: 100 } },
{ id: 'node-2', style: { x: 360, y: 100 } },
{ id: 'node-3', style: { x: 280, y: 220 } },
],
edges: [
{ source: 'node-1', target: 'node-2' },
{ source: 'node-1', target: 'node-3' },
{ source: 'node-2', target: 'node-3' },
],
},
node: {
style: { fill: '#7e3feb' },
state: {
custom: { fill: '#ffa940' },
},
},
edge: {
stroke: '#8b9baf',
state: {
custom: { stroke: '#ffa940' },
},
},
behaviors: [
{
type: 'brush-select',
key: 'brush-select',
trigger: [],
immediately: true,
},
],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
key: 'brush-select',
type: 'brush-select',
animation: false,
enable: true,
enableElements: ['node', 'edge', 'combo'],
mode: 'default',
state: 'selected',
};
const optionFolder = gui.addFolder('BrushSelect Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'state', ['active', 'selected', 'custom']);
optionFolder.add(options, 'mode', ['union', 'intersect', 'diff', 'default']);
// .onChange((e) => {
// immediately.show(e === 'default');
// });
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'brush-select',
[property]: value,
});
graph.render();
});
},
);

style

属性描述类型默认值
cursor鼠标样式string
fill填充颜色string | Pattern | null#1677FF
fillOpacity填充透明度number | string0.1
isBillboard是否启用公告牌模式boolean
isSizeAttenuation是否启用大小衰减boolean
lineCap线段端点样式butt | round | square
lineDash虚线配置number | string | (string | number)[]
lineDashOffset虚线偏移量number
lineJoin线段连接处样式miter | round | bevel
lineWidth线宽度number | string1
opacity整体透明度number | string
radius矩形圆角半径number | string | number[]
shadowBlur阴影模糊程度number
shadowColor阴影颜色string
shadowOffsetX阴影 X 方向偏移number
shadowOffsetY阴影 Y 方向偏移number
stroke描边颜色string | Pattern | null#1677FF
strokeOpacity描边透明度number | string
visibility可见性visible | hidden
zIndex渲染层级number2

示例:

const graph = new Graph({
behaviors: [
{
type: 'brush-select',
key: 'brush-select',
style: {
width: 0,
height: 0,
lineWidth: 4,
lineDash: [2, 2], // 虚线外框
// rgb超级炫彩框框
fill: 'linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%)',
stroke: 'pink',
fillOpacity: 0.2,
zIndex: 2,
pointerEvents: 'none',
},
},
],
});

createGraph(
{
data: {
nodes: [
{ id: 'node-1', style: { x: 200, y: 100 } },
{ id: 'node-2', style: { x: 360, y: 100 } },
{ id: 'node-3', style: { x: 280, y: 220 } },
],
edges: [
{ source: 'node-1', target: 'node-2' },
{ source: 'node-1', target: 'node-3' },
{ source: 'node-2', target: 'node-3' },
],
},
node: {
style: { fill: '#7e3feb' },
},
edge: {
stroke: '#8b9baf',
},
behaviors: [
{
type: 'brush-select',
key: 'brush-select',
trigger: [],
immediately: true,
style: {
width: 0,
height: 0,
lineWidth: 4,
lineDash: [2, 2], // 虚线外框
// rgb超级炫彩框框
fill: 'linear-gradient(217deg, rgba(255,0,0,.8), rgba(255,0,0,0) 70.71%),linear-gradient(127deg, rgba(0,255,0,.8), rgba(0,255,0,0) 70.71%),linear-gradient(336deg, rgba(0,0,255,.8), rgba(0,0,255,0) 70.71%)',
stroke: 'pink',
fillOpacity: 0.2,
zIndex: 2,
pointerEvents: 'none',
},
},
],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
},
{ width: 600, height: 300 },
);

实际案例