logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.48
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 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
  • 常见问题
  • 参与贡献

Legend 图例

上一篇
Hull 轮廓包围
下一篇
Minimap 小地图

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

概述

图例(Legend)插件用于展示图中元素的分类信息,支持节点、边、组合的分类信息展示。通过图例,用户可以快速感知到图中相关元素的分类信息,也可以通过点击对应图例项来快速定位到元素,提高用户的浏览效率。

使用场景

这一插件主要用于:

  • 通过图例快速对元素进行分类
  • 通过图例快速高亮定位到对应元素

基本用法

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,
// 其他配置...
plugins: [
{
type: 'legend', // 插件类型为 legend
nodeField: 'cluster', // 用于节点分组的数组字段名称
edgeField: 'cluster', // 用于边分组的数组字段名称
},
],
});

配置项

属性描述类型默认值必选
type插件类型stringlegend✓
key插件唯一标识符,用于后续更新string-
trigger图例项触发对应项高亮的方式:
- hover:鼠标移入图例项时触发
- click:鼠标点击图例项时触发
hover | clickhover
position图例在画布中的相对位置,可选值CardinalPlacementbottom
container图例挂载的容器,无则挂载到 Graph 所在容器HTMLElement | string-
className图例画布类名,传入外置容器时不生效string-
containerStyle图例的容器样式,传入外置容器时不生效CSSStyleDeclaration-
nodeField节点分类标识string | (item: ElementDatum) => string-
edgeField边分类标识string | (item: ElementDatum) => string-
comboField组合分类标识string | (item: ElementDatum) => string-
orientation图例项的布局方向:
- horizontal:水平方向
- vertical:垂直方向
horizontal | vertical'horizontal'
layout布局方式:
- flex:弹性布局
- grid:网格布局
flex | gridflex
showTitle是否显示标题booleanfalse
titleText标题内容string""
x图例在画布中的相对的横向位置,优先级高于positionnumber-
y图例在画布中的相对的纵向位置,优先级高于positionnumber-
width图例的宽度number240
height图例的高度number160
itemSpacing图例项的文本和对应标记之间的间距number4
rowPadding图例中每行之间的间距number10
colPadding图例中每列之间的间距number10
itemMarkerSize图例项标记的大小number16
itemLabelFontSize图例项文本的字体大小number16
gridCol图例项在宽度允许情况下的最大列数number-
gridRow图例项在高度允许情况下的最大行数number-

CardinalPlacement

position 属性支持以下值:

  • 'top-left':左上角
  • 'top-right':右上角
  • 'bottom-left':左下角
  • 'bottom-right':右下角
  • 'left-top':左侧靠上
  • 'left-bottom':左侧靠下
  • 'right-top':右侧靠上
  • 'right-bottom':右侧靠下

代码示例

基础图例

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({
// 其他配置...
plugins: [
{
type: 'legend', // 插件类型为 legend
nodeField: 'cluster', // 用于节点分组的数组字段名称
edgeField: 'cluster', // 用于边分组的数组字段名称
},
],
});

自定义图例位置

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,
// 其他配置...
plugins: [
{
type: 'legend',
nodeField: 'cluster',
edgeField: 'cluster',
// 可以通过 position 快捷的来指定位置
// position: "top-left",
// 也可以通过x,y来更加灵活的控制图例的位置
x: 20,
y: 20,
},
],
});

自定义图例项布局

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,
// 其他配置...
plugins: [
{
type: 'legend',
nodeField: 'cluster',
edgeField: 'cluster',
layout: 'flex',
// 控制只显示一行
gridRow: 1,
// 控制一行显示10列,当列宽不足时会显示翻页按钮
gridCol: 10,
},
],
});

常见问题

1. 设置了 orientation 无效?

orientation主要控制布局的方向,具体展示一行多列还是一列多行,主要通过 gridRow 以及gridCol来控制,例如想要看起来像是竖向的图例项,则可以通过这样配置:

plugins: [
{
type: 'legend',
nodeField: 'cluster',
edgeField: 'cluster',
layout: "flex",
// 控制一行显示1列
gridCol:1,
// 控制显示最多20行
gridRow: 20,
},
],

这样就变成了只有一列的图例,符合视觉上的竖向排列。

2. 如何动态更新工具栏?

可以使用 updatePlugin 方法动态更新工具栏:

const graph = new Graph({
data,
// 其他配置...
plugins: [
{
type: 'legend',
key: 'my-legend',
nodeField: 'cluster',
edgeField: 'cluster',
},
],
});
// 更新图例位置
graph.updatePlugin({
key: 'my-legend',
position: 'bottom-right',
});

实际案例