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

悬停激活 HoverActivate

上一篇
聚焦元素 FocusElement
下一篇
套索选择 LassoSelect

资源

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

概述

HoverActivate 是 G6 中用于实现元素悬停激活效果的内置交互,当鼠标悬停在节点或边上时,会自动触发高亮、显示等视觉反馈。该交互是图可视化中增强数据探索的重要手段,有助于用户快速聚焦目标元素并获取相关信息。

使用场景

这一交互主要用于:

  • 在复杂关系图中快速定位关注元素
  • 通过悬停信息展示节点额外信息
  • 分析节点间关联关系时,通过激活边凸显连接路径

在线体验

createGraph(
{
data: {
nodes: [
{ id: 'node0', size: 50, label: '0', style: { x: 326, y: 268 } },
{ id: 'node1', size: 30, label: '1', style: { x: 280, y: 384 } },
{ id: 'node2', size: 30, label: '2', style: { x: 234, y: 167 } },
{ id: 'node3', size: 30, label: '3', style: { x: 391, y: 368 } },
{ id: 'node4', size: 30, label: '4', style: { x: 444, y: 209 } },
{ id: 'node5', size: 30, label: '5', style: { x: 378, y: 157 } },
{ id: 'node6', size: 15, label: '6', style: { x: 229, y: 400 } },
{ id: 'node7', size: 15, label: '7', style: { x: 281, y: 440 } },
{ id: 'node8', size: 15, label: '8', style: { x: 188, y: 119 } },
{ id: 'node9', size: 15, label: '9', style: { x: 287, y: 157 } },
{ id: 'node10', size: 15, label: '10', style: { x: 185, y: 200 } },
{ id: 'node11', size: 15, label: '11', style: { x: 238, y: 110 } },
{ id: 'node12', size: 15, label: '12', style: { x: 239, y: 221 } },
{ id: 'node13', size: 15, label: '13', style: { x: 176, y: 160 } },
{ id: 'node14', size: 15, label: '14', style: { x: 389, y: 423 } },
{ id: 'node15', size: 15, label: '15', style: { x: 441, y: 341 } },
{ id: 'node16', size: 15, label: '16', style: { x: 442, y: 398 } },
],
edges: [
{ source: 'node0', target: 'node1', label: '0-1' },
{ source: 'node0', target: 'node2', label: '0-2' },
{ source: 'node0', target: 'node3', label: '0-3' },
{ source: 'node0', target: 'node4', label: '0-4' },
{ source: 'node0', target: 'node5', label: '0-5' },
{ source: 'node1', target: 'node6', label: '1-6' },
{ source: 'node1', target: 'node7', label: '1-7' },
{ source: 'node2', target: 'node8', label: '2-8' },
{ source: 'node2', target: 'node9', label: '2-9' },
{ source: 'node2', target: 'node10', label: '2-10' },
{ source: 'node2', target: 'node11', label: '2-11' },
{ source: 'node2', target: 'node12', label: '2-12' },
{ source: 'node2', target: 'node13', label: '2-13' },
{ source: 'node3', target: 'node14', label: '3-14' },
{ source: 'node3', target: 'node15', label: '3-15' },
{ source: 'node3', target: 'node16', label: '3-16' },
],
},
behaviors: ['zoom-canvas', 'drag-canvas', { key: 'hover-activate', type: 'hover-activate' }],
autoFit: 'center',
},
{ width: 600, height: 300 },
(gui, graph) => {
const options = {
key: 'hover-activate',
type: 'hover-activate',
animation: true,
enable: true,
degree: 1,
direction: 'both',
};
const optionFolder = gui.addFolder('Hover Activate Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'animation');
optionFolder.add(options, 'enable');
optionFolder.add(options, 'degree', 0, 10, 1);
optionFolder.add(options, 'direction', {
both: ['both'],
in: ['in'],
out: ['out'],
});
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'hover-activate',
[property]: value,
});
graph.render();
});
},
);

基本用法

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

1. 快速配置(静态)

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

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

2. 对象配置(推荐)

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

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'hover-activate',
key: 'hover-activate-1', // 为交互指定标识符,方便动态更新
},
],
});

配置项

配置项说明类型默认值必选
type交互类型名称stringhover-activate✓
animation是否开启动画效果booleantrue
enable是否开启悬浮元素功能boolean | ((event: IPointerEvent) => boolean)true
degree激活元素的n度关系number | ((event: IPointerEvent) => number);0
direction指定边方向both | in | outboth
state激活元素的状态stringactive
inactiveState不激活元素的状态string-
onHover当元素被悬停时的回调(event: IPointerEvent) => void-
onHoverEnd当悬停结束时的回调(event: IPointerEvent) => void-

enable

enable 用于控制是否开启元素的悬浮高亮,可接收一个函数来动态控制

例如:只有节点开启悬浮高亮

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'hover-activate',
enable: (e) => {
if (e.targetType === 'node') {
return true;
}
return false;
},
},
],
});

代码示例

基础悬浮用法

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

节点触发高亮

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'hover-activate',
enable: (e) => {
if (e.targetType === 'node') {
return true;
}
return false;
},
},
],
});

流程图移入节点 下一步节点高亮

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'hover-activate',
degree: 1,
direction: 'out',
enable: (e) => {
if (e.targetType === 'node') {
return true;
}
return false;
},
},
],
});

实际案例

import { Graph } from '@antv/g6';
const format = (data) => {
const { nodes, edges } = data;
return {
nodes: nodes.map(({ id, ...node }) => ({ id, data: node })),
edges: edges.map(({ id, source, target, ...edge }) => ({ id, source, target, data: edge })),
};
};
fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/xiaomi.json')
.then((res) => res.json())
.then((data) => {
const graph = new Graph({
container: 'container',
autoFit: 'view',
data: format(data),
behaviors: ['hover-activate'],
layout: {
type: 'force',
preventOverlap: true,
nodeSize: 24,
},
animation: false,
});
graph.render();
});