logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.47
  • Introduction
  • Data
  • Getting Started
    • Quick Start
    • Installation
    • Integration
      • react
      • vue
      • angular
    • Step-by-step guide
  • Graph
    • Extensions En
    • Graph
    • Options
    • extension
  • Element
    • Element Overview
    • Element State
    • Node
      • Node Overview
      • Build-in Node
        • Common Node Configurations
        • Diamond
        • Donut
        • Ellipse
        • Hexagon
        • Html
        • Image
        • Rect
        • Star
        • Triangle
        • Circle
      • Custom Node
      • Define Nodes with React
    • Edge
      • Edge Overview
      • Build-in Edge
        • Common Edge Configurations
        • Cubic Bezier Curve
        • CubicHorizontal Bezier Curve
        • CubicVertical Bezier Curve
        • Line
        • Polyline
        • Quadratic Bezier Curve
      • Custom Edge
    • Combo
      • Combo Overview
      • Build-in Combo
        • Circle
        • Combo Configuration Options
        • Rect
      • Custom Combo
    • Shape
      • Shape and KeyShape
      • Atomic Shapes and Their Properties
      • Design and Implementation of Composite Shape
  • Layout
    • Layout Overview
    • Build-in Layout
      • 3D Force-Directed Layout
      • AntvDagre Layout
      • Circular Layout
      • ComboCombined Layout
      • Common Layout Configuration Options
      • CompactBox
      • Concentric Layout
      • D3 Force-Directed Layout
      • Dagre Layout
      • Dendrogram Layout
      • Fishbone Layout
      • Force Force-directed Layout
      • ForceAtlas2 Force-directed Layout
      • Fruchterman Force-directed Layout
      • Grid Layout
      • Indented Tree
      • MDS High-dimensional Data Dimensionality Reduction Layout
      • Mindmap Tree
      • Radial Layout
      • Random Layout
      • Snake Layout
    • Custom Layout
  • Behavior
    • Behavior Overview
    • Build-in Behavior
      • AutoAdaptLabel
      • BrushSelect
      • ClickSelect
      • CollapseExpand
      • CreateEdge
      • DragCanvas
      • DragElement
      • DragElementForce
      • FixElementSize
      • FocusElement
      • HoverActivate
      • LassoSelect
      • OptimizeViewportTransform
      • ScrollCanvas
      • ZoomCanvas
    • Custom Behavior
  • Plugin
    • Plugin Overview
    • Build-in Plugin
      • Background
      • BubbleSets
      • Contextmenu
      • EdgeBundling
      • EdgeFilterLens
      • Fisheye
      • Fullscreen
      • GridLine
      • History
      • Hull
      • Legend
      • Minimap
      • Snapline
      • Timebar
      • Toolbar
      • Tooltip
      • Watermark
    • Custom Plugin
  • Transform
    • Data Transformation Overview
    • Build-in Transform
      • MapNodeSize
      • PlaceRadialLabels
      • ProcessParallelEdges
    • Custom Transform
  • Theme
    • Theme Overview
    • Custom Theme
    • Palette
    • Custom Palette
  • Animation
    • Animation Overview
    • Custom Animation
  • Further Reading
    • Event
    • renderer
    • coordinate
    • download-image
    • Using Iconfont
    • Use 3D
    • Bundle Project
  • What's new
    • Feature
    • Upgrade To 5.0
  • FAQ
  • contribute

Legend

Previous
Hull
Next
Minimap

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview

The Legend plugin is used to display classification information of elements in the graph, supporting the display of classification information for nodes, edges, and combos. Through the legend, users can quickly perceive the classification information of related elements in the graph and quickly locate elements by clicking on the corresponding legend items, improving user browsing efficiency.

Usage Scenarios

This plugin is mainly used for:

  • Quickly classifying elements through the legend
  • Quickly highlighting and locating corresponding elements through the legend

Basic Usage

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,
// Other configurations...
plugins: [
{
type: 'legend', // Plugin type is legend
nodeField: 'cluster', // Array field name for node grouping
edgeField: 'cluster', // Array field name for edge grouping
},
],
});

Configuration Options

PropertyDescriptionTypeDefault ValueRequired
typePlugin typestringlegend✓
keyUnique identifier for the plugin, used for subsequent updatesstring-
triggerHow the legend item triggers the corresponding item highlight:
- hover: Triggered when the mouse enters the legend item
- click: Triggered when the mouse clicks the legend item
hover | clickhover
positionRelative position of the legend on the canvas, optional valuesCardinalPlacementbottom
containerContainer to which the legend is mounted, if not provided, it is mounted to the container where the Graph is locatedHTMLElement | string-
classNameLegend canvas class name, not effective when an external container is passedstring-
containerStyleStyle of the legend container, not effective when an external container is passedCSSStyleDeclaration-
nodeFieldNode classification identifierstring | (item: ElementDatum) => string-
edgeFieldEdge classification identifierstring | (item: ElementDatum) => string-
comboFieldCombo classification identifierstring | (item: ElementDatum) => string-
orientationLayout direction of legend items:
- horizontal: Horizontal direction
- vertical: Vertical direction
horizontal | vertical'horizontal'
layoutLayout method:
- flex: Flexible layout
- grid: Grid layout
flex | gridflex
showTitleWhether to display the titlebooleanfalse
titleTextTitle contentstring""
xRelative horizontal position of the legend on the canvas, higher priority than positionnumber-
yRelative vertical position of the legend on the canvas, higher priority than positionnumber-
widthWidth of the legendnumber240
heightHeight of the legendnumber160
itemSpacingSpacing between the text of the legend item and the corresponding markernumber4
rowPaddingSpacing between each row in the legendnumber10
colPaddingSpacing between each column in the legendnumber10
itemMarkerSizeSize of the legend item markernumber16
itemLabelFontSizeFont size of the legend item textnumber16
gridColMaximum number of columns allowed for legend items when width permitsnumber-
gridRowMaximum number of rows allowed for legend items when height permitsnumber-

CardinalPlacement

The position property supports the following values:

  • 'top-left': Top left corner
  • 'top-right': Top right corner
  • 'bottom-left': Bottom left corner
  • 'bottom-right': Bottom right corner
  • 'left-top': Left side near the top
  • 'left-bottom': Left side near the bottom
  • 'right-top': Right side near the top
  • 'right-bottom': Right side near the bottom

Code Examples

Basic 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({
// Other configurations...
plugins: [
{
type: 'legend', // Plugin type is legend
nodeField: 'cluster', // Array field name for node grouping
edgeField: 'cluster', // Array field name for edge grouping
},
],
});

Custom Legend Position

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,
// Other configurations...
plugins: [
{
type: 'legend',
nodeField: 'cluster',
edgeField: 'cluster',
// You can quickly specify the position through position
// position: "top-left",
// Or you can more flexibly control the position of the legend through x, y
x: 20,
y: 20,
},
],
});

Custom Legend Item Layout

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,
// Other configurations...
plugins: [
{
type: 'legend',
nodeField: 'cluster',
edgeField: 'cluster',
layout: 'flex',
// Control to display only one row
gridRow: 1,
// Control to display 10 columns in one row, a page button will be displayed when the column width is insufficient
gridCol: 10,
},
],
});

Common Issues

1. Setting orientation is ineffective?

orientation mainly controls the direction of the layout, and the specific display of multiple columns in one row or multiple rows in one column is mainly controlled by gridRow and gridCol. For example, if you want it to look like a vertical legend item, you can configure it like this:

plugins: [
{
type: 'legend',
nodeField: 'cluster',
edgeField: 'cluster',
layout: "flex",
// Control to display 1 column in one row
gridCol:1,
// Control to display up to 20 rows
gridRow: 20,
},
],

This way, it becomes a legend with only one column, conforming to the visual vertical arrangement.

2. How to dynamically update the toolbar?

You can use the updatePlugin method to dynamically update the toolbar:

const graph = new Graph({
data,
// Other configurations...
plugins: [
{
type: 'legend',
key: 'my-legend',
nodeField: 'cluster',
edgeField: 'cluster',
},
],
});
// Update legend position
graph.updatePlugin({
key: 'my-legend',
position: 'bottom-right',
});

Practical Cases