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
  • 常见问题
  • 参与贡献

ClickSelect 点击选中

上一篇
BrushSelect 框选
下一篇
CollapseExpand 展开/收起元素

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: 280, y: 60, fill: '#E4504D', labelText: 'degree: 0' } },
{ id: 'node-2-1', style: { x: 330, y: 140, fill: '#FFC40C', labelText: 'degree: 1' } },
{ id: 'node-2-2', style: { x: 230, y: 140, fill: '#FFC40C', labelText: 'degree: 1' } },
{ id: 'node-3-1', style: { x: 380, y: 220, fill: '#0f0', labelText: 'degree: 2' } },
{ id: 'node-3-2', style: { x: 180, y: 220, fill: '#0f0', labelText: 'degree: 2' } },
{
id: 'degree引导',
style: {
x: 525,
y: 110,
fill: null,
labelText: '这里可以修改degree ->',
labelFontWeight: 700,
labelFontSize: 10,
},
},
],
edges: [
{ source: 'node-1', target: 'node-2-1' },
{ source: 'node-1', target: 'node-2-2' },
{ source: 'node-2-1', target: 'node-3-1' },
{ source: 'node-2-2', target: 'node-3-2' },
],
},
node: {
style: { label: true, labelFill: '#666', labelFontSize: 14, labelPlacement: 'bottom' },
state: {
custom: { fill: '#ffa940' },
},
},
edge: {
stroke: '#8b9baf',
state: {
custom: { stroke: '#ffa940' },
},
},
behaviors: [
{
type: 'click-select',
key: 'click-select',
},
],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
key: 'click-select',
type: 'click-select',
animation: true,
enable: true,
multiple: false,
trigger: 'shift+click',
state: 'selected',
unselectedState: undefined,
degree: 0,
};
const optionFolder = gui.addFolder('Click Select Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'animation');
optionFolder.add(options, 'enable');
optionFolder.add(options, 'degree', 0, 2, 1);
optionFolder.add(options, 'state', ['active', 'selected', 'custom']);
optionFolder.add(options, 'unselectedState', [undefined, 'inactive']);
const trigger = optionFolder
.add(options, 'trigger', {
'shift+click': ['shift'],
'meta+click': ['Meta'],
})
.hide();
optionFolder.add(options, 'multiple').onChange((v) => trigger.show(v));
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'click-select',
[property]: value,
});
graph.render();
});
},
);

基本用法

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

1. 快速配置(静态)

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

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

2. 对象配置(推荐)

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

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'click-select',
key: 'click-select-1',
degree: 2, // 选中扩散范围
state: 'active', // 选中的状态
neighborState: 'neighborActive', // 相邻节点附着状态
unselectedState: 'inactive', // 未选中节点状态
},
],
});

配置项

配置项说明类型默认值必选
type交互类型名称。此交互已内置,你可以通过 type: 'click-select' 来使用它。click-select | stringclick-select✓
animation是否在元素状态切换时启用动画效果booleantrue
degree控制了高亮扩散范围,示例number | (event:Event) => number0
enable是否启用点击元素的功能,支持通过函数的方式动态控制是否启用,示例boolean | ((event: Event) => boolean)true
multiple是否允许多选booleanfalse
state当元素被选中时应用的状态string | selected | active| inactive| disabled| highlightselected
neighborState当有元素选中时,其相邻 n 度关系的元素应用的状态。n 的值由属性 degree 控制,例如 degree 为 1 时表示直接相邻的元素,示例string | selected | active| inactive| disabled| highlightselected
unselectedState当有元素被选中时,除了选中元素及其受影响的邻居元素外,其他所有元素应用的状态,示例string | selected | active| inactive| disabled| highlight
onClick点击元素时的回调(event: Event) => void
trigger按下该快捷键配合鼠标点击进行多选,按键参考: MDN Key Valuesstring[] | (Control | Shift| Alt | ......)[]['shift']

degree

控制了高亮扩散范围

  • 对于节点来说,0 表示只选中当前节点,1 表示选中当前节点及其直接相邻的节点和边,以此类推。
  • 对于边来说,0 表示只选中当前边,1 表示选中当前边及其直接相邻的节点,以此类推。

如下示例,当 degree: 0 仅高亮红色点; 当 degree: 1 高亮红色和橙色点。

createGraph(
{
data: {
nodes: [
{ id: 'node-1', style: { x: 280, y: 60, fill: '#E4504D', labelText: 'degree: 0' } },
{ id: 'node-2-1', style: { x: 330, y: 140, fill: '#FFC40C', labelText: 'degree: 1' } },
{ id: 'node-2-2', style: { x: 230, y: 140, fill: '#FFC40C', labelText: 'degree: 1' } },
{ id: 'node-3-1', style: { x: 380, y: 220, fill: '#0f0', labelText: 'degree: 2' } },
{ id: 'node-3-2', style: { x: 180, y: 220, fill: '#0f0', labelText: 'degree: 2' } },
{
id: 'degree引导',
style: {
x: 525,
y: 110,
fill: null,
labelText: '这里可以修改degree ->',
labelFontWeight: 700,
labelFontSize: 10,
},
},
],
edges: [
{ source: 'node-1', target: 'node-2-1' },
{ source: 'node-1', target: 'node-2-2' },
{ source: 'node-2-1', target: 'node-3-1' },
{ source: 'node-2-2', target: 'node-3-2' },
],
},
node: {
style: { label: true, labelFill: '#666', labelFontSize: 14, labelPlacement: 'bottom' },
state: {
custom: { fill: '#ffa940' },
},
},
edge: {
stroke: '#8b9baf',
state: {
custom: { stroke: '#ffa940' },
},
},
behaviors: [
{
type: 'click-select',
key: 'click-select',
},
],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
key: 'click-select',
type: 'click-select',
animation: true,
enable: true,
multiple: false,
trigger: 'shift+click',
state: 'selected',
unselectedState: undefined,
degree: 0,
};
const optionFolder = gui.addFolder('Click Select Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'animation');
optionFolder.add(options, 'enable');
optionFolder.add(options, 'degree', 0, 2, 1);
optionFolder.add(options, 'state', ['active', 'selected', 'custom']);
optionFolder.add(options, 'unselectedState', [undefined, 'inactive']);
const trigger = optionFolder
.add(options, 'trigger', {
'shift+click': ['shift'],
'meta+click': ['Meta'],
})
.hide();
optionFolder.add(options, 'multiple').onChange((v) => trigger.show(v));
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'click-select',
[property]: value,
});
graph.render();
});
},
);

enable

是否启用点击元素的功能

可以通过函数的方式动态控制是否启用,例如只有节点被选中时才启用。

{
//⚠️ 注意,这里需要同时设置节点和画布,否则用户点击画布时将不会监听到事件
enable: (event) => ['node', 'canvas'].includes(event.targetType);
}

createGraph(
{
data: {
nodes: [
{ id: 'node1', style: { x: 100, y: 60 } },
{ id: 'node2', style: { x: 200, y: 60 } },
{ id: 'node3', style: { x: 300, y: 60 } },
],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node2', target: 'node3' },
],
},
node: {
style: {
fill: '#E4504D',
},
state: {
active: {
fill: '#0f0',
},
neighborActive: {
fill: '#FFC40C',
},
},
},
behaviors: [
{
type: 'click-select',
degree: 1,
state: 'active',
neighborState: 'neighborActive',
enable: (event) => ['node', 'canvas'].includes(event.targetType),
},
],
},
{ width: 400, height: 200 },
);

同理,如果只希望边能被选中:

{
enable: (event) => ['edge', 'canvas'].includes(event.targetType);
}

neighborState

当有元素选中时,其相邻 n 度关系的元素应用的状态。n 的值由属性 degree 控制,例如 degree 为 1 时表示直接相邻的元素

const graph = new Graph({
behaviors: [
{
type: 'click-select',
degree: 1,
// 被直接点击的节点附着的状态
state: 'active',
// 相邻的节点附着的状态
neighborState: 'neighborActive',
},
],
});

createGraph(
{
layout: {
type: 'grid',
},
data: {
nodes: [{ id: 'node1' }, { id: 'node2' }, { id: 'node3' }, { id: 'node4' }, { id: 'node5' }],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node2', target: 'node3' },
{ source: 'node3', target: 'node4' },
{ source: 'node4', target: 'node5' },
],
},
node: {
style: {
fill: '#E4504D',
},
state: {
active: {
fill: '#0f0',
},
neighborActive: {
fill: '#FFC40C',
halo: true,
},
},
},
behaviors: [
{
type: 'click-select',
degree: 1,
state: 'active',
neighborState: 'neighborActive',
},
],
},
{ width: 400, height: 200 },
);

unselectedState

当有元素被选中时,除了被选中元素和扩散的邻居元素外,其他所有元素应用的状态。

内置状态: selected active inactive disabled highlight

const graph = new Graph({
behaviors: [
{
type: 'click-select',
degree: 1,
unselectedState: 'inactive',
},
],
});

createGraph(
{
layout: {
type: 'grid',
},
data: {
nodes: [{ id: 'node1' }, { id: 'node2' }, { id: 'node3' }, { id: 'node4' }, { id: 'node5' }],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node2', target: 'node3' },
{ source: 'node3', target: 'node4' },
{ source: 'node4', target: 'node5' },
],
},
node: {
style: {
fill: '#E4504D',
},
state: {
active: {
fill: '#0f0',
},
neighborActive: {
fill: '#FFC40C',
},
},
},
behaviors: [
{
type: 'click-select',
degree: 1,
state: 'active',
neighborState: 'neighborActive',
unselectedState: 'inactive',
},
],
},
{ width: 400, height: 200 },
);

示例

点击选中节点及其直接相连的节点

点击节点 会从 默认状态 切换为 active
相邻节点 会从 默认状态 切换为 neighborActive

const graph = new Graph({
node: {
style: {
fill: '#E4504D',
},
state: {
// 选中节点状态
active: {
fill: '#0f0',
},
// 相邻节点状态
neighborActive: {
fill: '#FFC40C',
},
},
},
behaviors: [
{
type: 'click-select',
degree: 1,
state: 'active',
// 相邻节点附着状态
neighborState: 'neighborActive',
// 未选中节点状态
unselectedState: 'inactive',
},
],
});

createGraph(
{
layout: {
type: 'grid',
},
data: {
nodes: [{ id: 'node1' }, { id: 'node2' }, { id: 'node3' }, { id: 'node4' }, { id: 'node5' }],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node2', target: 'node3' },
{ source: 'node3', target: 'node4' },
{ source: 'node4', target: 'node5' },
],
},
node: {
style: {
fill: '#E4504D',
},
state: {
active: {
fill: '#0f0',
},
neighborActive: {
fill: '#FFC40C',
},
},
},
behaviors: [
{
type: 'click-select',
degree: 1,
state: 'active',
neighborState: 'neighborActive',
unselectedState: 'inactive',
},
],
},
{ width: 400, height: 200 },
);

实际案例