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

上下文菜单 Contextmenu

上一篇
气泡集 BubbleSets
下一篇
边绑定 EdgeBundling

资源

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

概述

上下文菜单(Contextmenu),也被称为右键菜单,是当用户在某个特定区域上点击后出现的一个菜单。支持在点击前后,触发自定义事件。通过上下文菜单,可以将一些具体元素的操作集成在其中,方便对某一项来进行需要时的单独控制。

使用场景

这一插件主要用于:

  • 元素的各种交互:查看节点、查看边、删除节点等等交互操作

基本用法

以下是一个简单的 Contextmenu 插件初始化示例:

const graph = new Graph({
plugins: [
{
type: 'contextmenu',
// 只在节点上开启右键菜单,默认全部元素都开启
enable: (e) => e.targetType === 'node',
getItems: () => {
return [{ name: '查看详情', value: 'detail' }];
},
onClick: (value) => {
if (value === 'detail') console.log('展示节点详情');
},
},
],
});

配置项

属性描述类型默认值必选
className给菜单的 DOM 追加的类名stringg6-contextmenu
trigger如何触发右键菜单:contextmenu 表示右键触发,click 表示点击触发click | contextmenucontextmenu
offset菜单显式 X、Y 方向的偏移量[number, number][4, 4]
onClick当菜单被点击后,触发的回调方法,示例(value: string, target: HTMLElement, current: Element) => void-
getItems返回菜单的项目列表,支持 Promise 类型的返回值。是 getContent 的快捷配置(event: IElementEvent) => Item[] | Promise<Item[]>-
getContent返回菜单的内容,支持 Promise 类型的返回值,也可以使用 getItems 进行快捷配置(event: IElementEvent) => HTMLElement | string | Promise<HTMLElement | string>-
loadingContent当 getContent 返回一个 Promise 时,使用的菜单内容HTMLElement | string-
enable是否可用,通过参数判断是否支持右键菜单,默认是全部可用boolean | (event: IElementEvent) => booleantrue

Item

每个菜单项目 (Item) 包含以下属性:

属性描述类型必选
name菜单项显示的名字string✓
value菜单项对应的值string✓

onClick

点击菜单项后会触发该函数,函数有三个参数:

  • value: 对应菜单项的 value
  • target: 对应菜单项容器的 dom 节点
  • current: 对应触发菜单项的元素,例如是节点则可以通过 current 来获取到节点的信息(id)等,或者来对元素进行修改

代码示例

基础右键菜单

const data = {
nodes: [
{ id: 'node-1', type: 'circle', data: { cluster: 'node-type1' } },
{ id: 'node-2', type: 'rect', data: { cluster: 'node-type2' } },
],
edges: [{ source: 'node-1', target: 'node-2', data: { cluster: 'edge-type1' } }],
};
const graph = new Graph({
data,
layout: { type: 'grid' },
plugins: [
{
type: 'contextmenu',
trigger: 'contextmenu', // 'click' or 'contextmenu'
onClick: (value, target, current) => {
alert('You have clicked the「' + v + '」item');
},
getItems: () => {
return [
{ name: '查看详情', value: 'detail' },
{ name: '删除', value: 'delete' },
];
},
},
],
});

边的右键菜单

const data = {
nodes: [
{ id: 'node-1', type: 'circle', data: { cluster: 'node-type1' } },
{ id: 'node-2', type: 'rect', data: { cluster: 'node-type2' } },
],
edges: [{ source: 'node-1', target: 'node-2', data: { cluster: 'edge-type1' } }],
};
const graph = new Graph({
data,
layout: { type: 'grid' },
plugins: [
{
type: 'contextmenu',
trigger: 'contextmenu',
getItems: () => {
return [{ name: '变更起点', value: 'change' }];
},
onClick: (value) => {
if (value === 'change') console.log('这里执行变更起点操作');
},
// 仅在边上开启右键菜单
enable: (e) => e.targetType === 'edge',
},
],
});

异步加载菜单项

const data = {
nodes: [
{ id: 'node-1', type: 'circle', data: { cluster: 'node-type1' } },
{ id: 'node-2', type: 'rect', data: { cluster: 'node-type2' } },
],
edges: [{ source: 'node-1', target: 'node-2', data: { cluster: 'edge-type1' } }],
};
const graph = new Graph({
data,
layout: { type: 'grid' },
plugins: [
{
type: 'contextmenu',
trigger: 'contextmenu',
getItems: async () => {
// 可以从服务器或其他异步源获取工具栏配置
const response = await fetch('/api/contextmenu-config');
const items = await response.json();
return items;
},
// 仅在边上开启右键菜单
enable: (e) => e.targetType === 'node',
},
],
});

动态控制菜单项

const data = {
nodes: [
{ id: 'node-1', type: 'circle', data: { cluster: 'node-type1' } },
{ id: 'node-2', type: 'rect', data: { cluster: 'node-type2' } },
],
edges: [{ source: 'node-1', target: 'node-2', data: { cluster: 'edge-type1' } }],
};
const graph = new Graph({
data,
layout: { type: 'grid' },
plugins: [
{
type: 'contextmenu',
trigger: 'contextmenu',
getItems: (e) => {
if (e.target.id === 'node-1') {
return [
{
name: '删除节点',
value: 'delete',
},
];
}
if (e.target.type === 'edge') {
return [
{
name: '移动边',
value: 'move',
},
];
}
return [];
},
},
],
});

实际案例

import { Graph } from '@antv/g6';
const data = {
nodes: [{ id: 'node-0' }, { id: 'node-1' }, { id: 'node-2' }, { id: 'node-3' }, { id: 'node-4' }, { id: 'node-5' }],
edges: [
{ source: 'node-0', target: 'node-1' },
{ source: 'node-0', target: 'node-2' },
{ source: 'node-0', target: 'node-3' },
{ source: 'node-0', target: 'node-4' },
{ source: 'node-1', target: 'node-0' },
{ source: 'node-2', target: 'node-0' },
{ source: 'node-3', target: 'node-0' },
{ source: 'node-4', target: 'node-0' },
{ source: 'node-5', target: 'node-0' },
],
};
const graph = new Graph({
container: 'container',
data,
layout: {
type: 'grid',
},
behaviors: ['zoom-canvas', 'drag-canvas', 'drag-element'],
plugins: [
{
type: 'contextmenu',
trigger: 'contextmenu', // 'click' or 'contextmenu'
onClick: (v) => {
alert('You have clicked the「' + v + '」item');
},
getItems: () => {
return [
{ name: '展开一度关系', value: 'spread' },
{ name: '查看详情', value: 'detail' },
];
},
enable: (e) => e.targetType === 'node',
},
],
});
graph.render();