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

标签自适应显示 AutoAdaptLabel

上一篇
缩放画布 ZoomCanvas
下一篇
框选 BrushSelect

资源

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: 'node1', style: { x: 200, y: 100, labelText: '短标签' } },
{ id: 'node2', style: { x: 360, y: 100, labelText: '中等长度的标签' } },
{ id: 'node3', style: { x: 280, y: 220, labelText: '这是一个非常非常长的标签,需要自适应显示' } },
],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node1', target: 'node3' },
{ source: 'node2', target: 'node3' },
],
},
node: {
style: { label: true, fill: '#7e3feb', labelFill: '#666', labelFontSize: 14, labelPlacement: 'bottom' },
state: {
custom: { fill: '#ffa940' },
},
},
edge: {
stroke: '#8b9baf',
state: {
custom: { stroke: '#ffa940' },
},
},
behaviors: ['zoom-canvas', 'drag-canvas', { key: 'auto-adapt-label', type: 'auto-adapt-label' }],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
},
{ width: 600, height: 400 },
(gui, graph) => {
const options = {
key: 'auto-adapt-label',
type: 'auto-adapt-label',
animation: true,
enable: true,
throttle: 100,
padding: 0,
};
const optionFolder = gui.addFolder('CollapseExpand Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'animation');
optionFolder.add(options, 'enable');
optionFolder.add(options, 'throttle', 0, 900, 100);
optionFolder.add(options, 'padding', 0, 20, 1);
optionFolder.onChange(({ property, value }) => {
graph.updateBehavior({
key: 'auto-adapt-label',
[property]: value,
});
graph.render();
});
},
);

基本用法

在图配置中添加这一交互

1. 快速配置(静态)

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

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

2. 对象配置(推荐)

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

const graph = new Graph({
// 其他配置...
behaviors: [
{
type: 'auto-adapt-label',
throttle: 200, // 节流时间
padding: 10, // 检测重叠时的额外间距
},
],
});

配置项

配置项说明类型默认值必选
type交互类型名称stringauto-adapt-label√
enable是否启用该交互boolean | ((event: Event) => boolean)true
throttle标签更新节流时间(ms)number100
padding标签检测重叠时的额外间距number | number[]0
sort自定义排序函数,根据元素的重要性从高到低排序,重要性越高的元素其标签显示优先级越高。一般情况下 combo > node > edge(a: ElementDatum, b: ElementDatum) => -1 | 0 | 1
sortNode根据节点的重要性从高到低排序,重要性越高的节点其标签显示优先级越高。内置几种中心性算法,也可以自定义排序函数。需要注意,如果设置了 sort,则 sortNode 不会生效NodeCentralityOptions | (nodeA: NodeData, nodeB: NodeData => -1 | 0 | 1)type: 'degree'
sortEdge根据边的重要性从高到低排序,重要性越高的边其标签显示优先级越高。默认按照数据先后进行排序。需要注意,如果设置了 sort,则 sortEdge 不会生效(edgeA: EdgeData, edgeB: EdgeData) => -1 | 0 | 1
sortCombo根据群组的重要性从高到低排序,重要性越高的群组其标签显示优先级越高。默认按照数据先后进行排序。需要注意,如果设置了 sort,则 sortCombo 不会生效(comboA: ComboData, comboB: ComboData) => -1 | 0 | 1

NodeCentralityOptions

节点中心性的度量方法

  • 'degree':度中心性,通过节点的度数(连接的边的数量)来衡量其重要性。度中心性高的节点通常具有较多的直接连接,在网络中可能扮演着重要的角色
  • 'betweenness':介数中心性,通过节点在所有最短路径中出现的次数来衡量其重要性。介数中心性高的节点通常在网络中起到桥梁作用,控制着信息的流动
  • 'closeness':接近中心性,通过节点到其他所有节点的最短路径长度总和的倒数来衡量其重要性。接近中心性高的节点通常能够更快地到达网络中的其他节点
  • 'eigenvector':特征向量中心性,通过节点与其他中心节点的连接程度来衡量其重要性。特征向量中心性高的节点通常连接着其他重要节点
  • 'pagerank':PageRank 中心性,通过节点被其他节点引用的次数来衡量其重要性,常用于有向图。PageRank 中心性高的节点通常在网络中具有较高的影响力,类似于网页排名算法
type NodeCentralityOptions =
| { type: 'degree'; direction?: 'in' | 'out' | 'both' }
| { type: 'betweenness'; directed?: boolean; weightPropertyName?: string }
| { type: 'closeness'; directed?: boolean; weightPropertyName?: string }
| { type: 'eigenvector'; directed?: boolean }
| { type: 'pagerank'; epsilon?: number; linkProb?: number };

实际案例

import { Graph } from '@antv/g6';
const data = {
nodes: [
{ id: 'node1', style: { x: 200, y: 100, labelText: '短标签' } },
{ id: 'node2', style: { x: 360, y: 100, labelText: '中等长度的标签' } },
{ id: 'node3', style: { x: 280, y: 220, labelText: '这是一个非常非常长的标签,需要自适应显示' } },
],
edges: [
{ source: 'node1', target: 'node2' },
{ source: 'node1', target: 'node3' },
{ source: 'node2', target: 'node3' },
],
};
const graph = new Graph({
container: 'container',
data,
behaviors: [
'zoom-canvas',
'drag-canvas',
{
key: 'auto-adapt-label',
type: 'auto-adapt-label',
padding: 0,
throttle: 200,
},
],
plugins: [{ type: 'grid-line', size: 30 }],
animation: true,
});
graph.render();