配置指南

标签与图标

节点标签、边标签和节点图标配置

G6Lite 用 Canvas 2D 在 WebGL 画布上方绘制文本标签与图标。GPU 负责海量节点和边,Canvas 2D 负责文本排版。

节点标签

node: {
  style: {
    size: 20,
    label: (node) => (node.data?.name as string) ?? node.id,
    labelPlacement: 'bottom',
    labelFontSize: 12,
    labelColor: '#333',
  },
}
字段说明
label文本;false 表示不显示
labelFontSize字号
labelFontFamily字体
labelFontWeight字重
labelColor文本颜色
labelPlacementcenter / top / bottom / left / right
labelOffsetX / labelOffsetY偏移
labelBackground标签底色
labelBackgroundPadding底色内边距
labelMaxWidth最大宽度,超出截断

节点图标

node: {
  style: {
    size: 28,
    icon: (node) => iconMap[node.data?.type as string],
    iconWidth: 16,
    iconHeight: 16,
  },
}
字段说明
icon图标内容,如图片地址或文本符号
iconWidth图标宽度
iconHeight图标高度

边标签

edge: {
  style: {
    label: (_source, _target, edge) => (edge.data?.label as string) ?? '',
    labelPlacement: 'center',
    labelAutoRotate: true,
  },
}
字段说明
label边标签文本
labelFontSize字号
labelFontFamily字体
labelColor文本颜色
labelPlacementstart / center / end
labelBackground标签底色
labelBackgroundPadding底色内边距
labelAutoRotate是否随边方向旋转

全局标签策略

通过 Graph 顶层 label 配置控制节点与边标签的缩放、低 zoom 隐藏和碰撞策略:

const graph = new Graph({
  label: {
    scaleWithZoom: true,
    hideOnLowZoom: true,
    hideOnCollision: true,
    nodeMinZoom: 0.5,
    edgeMinZoom: 0.5,
  },
});
字段默认值说明
scaleWithZoomtrue节点标签尺寸是否随 camera zoom 缩放
hideOnLowZoomtrue是否启用节点与边标签的最小 zoom 隐藏
hideOnCollisiontrue是否隐藏与先绘制标签发生碰撞的标签
nodeMinZoom0.3节点标签最小展示 zoom
edgeMinZoom0.5边标签最小展示 zoom

hideOnLowZoom: false 时会忽略两个数值阈值。运行时可通过 graph.updateOptions({ label: ... }) 更新策略,不会重新执行布局。

性能提示

  • 大规模图不建议全量显示所有标签。
  • 可以让 label 函数根据缩放层级、选中状态或业务过滤条件返回 false。
  • 优先在高亮、选中或聚焦场景显示局部标签。