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

Toolbar

Previous
Timebar
Next
Tooltip

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 Toolbar is a plugin in G6 used to provide a collection of operation buttons, supporting common chart operations such as zoom in, zoom out, auto-fit, and reset. Through the toolbar, users can quickly access common functions of the chart, improving operational efficiency and user experience.

Use Cases

This plugin is mainly used for:

  • Quickly accessing common functions of the chart

Basic Usage

Below is a simple example of initializing the Toolbar plugin:

const graph = new Graph({
// Other configurations...
plugins: [
{
type: 'toolbar',
getItems: () => [
{ id: 'zoom-in', value: 'zoom-in' },
{ id: 'zoom-out', value: 'zoom-out' },
{ id: 'auto-fit', value: 'auto-fit' },
],
onClick: (value) => {
// Handle button click events
if (value === 'zoom-in') {
graph.zoomTo(1.1);
} else if (value === 'zoom-out') {
graph.zoomTo(0.9);
} else if (value === 'auto-fit') {
graph.fitView();
}
},
},
],
});

Configuration Options

PropertyDescriptionTypeDefault ValueRequired
typePlugin typestringtoolbar✓
keyUnique identifier for the plugin, used for updatesstring-
classNameAdditional CSS class name for the toolbar DOM elementstring-
positionToolbar position relative to the canvas, optional valuesstring'top-left'
styleCustom style for the toolbar DOM element, common valuesCSSStyleDeclaration-
getItemsReturns the list of toolbar items() => ToolbarItem[] | Promise<ToolbarItem[]>-✓
onClickCallback function after a toolbar item is clicked(value: string, target: Element) => void-

position

The position parameter 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, top
  • 'left-bottom': Left side, bottom
  • 'right-top': Right side, top
  • 'right-bottom': Right side, bottom

style Attributes

AttributeDescriptionTypeDefault Value
backgroundColorBackground colorstring#fff
borderBorderstring1px solid #e8e8e8
borderRadiusBorder radiusstring4px
heightHeightstringauto
marginMarginstring12px
opacityOpacitynumber0.9
paddingPaddingstring8px
widthWidthstringauto

ToolbarItem

Each toolbar item (ToolbarItem) includes the following attributes:

AttributeDescriptionTypeRequired
idIcon ID of the item, see Built-in Iconsstring✓
valueValue returned when the item is clickedstring✓

Built-in Icons

The Toolbar provides the following built-in icon IDs:

  • 'zoom-in': Zoom in
  • 'zoom-out': Zoom out
  • 'redo': Redo
  • 'undo': Undo
  • 'edit': Edit
  • 'delete': Delete
  • 'auto-fit': Auto-fit view
  • 'export': Export chart
  • 'reset': Reset view
  • 'request-fullscreen': Request fullscreen
  • 'exit-fullscreen': Exit fullscreen

Custom Icons

In addition to using built-in icons, you can also use custom icons by introducing third-party icon libraries (such as Alibaba iconfont):

// Introduce iconfont script
const iconFont = document.createElement('script');
iconFont.src = '//at.alicdn.com/t/font_8d5l8fzk5b87iudi.js'; // Replace with your iconfont script URL
document.head.appendChild(iconFont);
// Use custom icons in the toolbar
const graph = new Graph({
// Other configurations...
plugins: [
{
type: 'toolbar',
getItems: () => [
{ id: 'icon-xinjian', value: 'new' }, // Use icons from iconfont
{ id: 'icon-fenxiang', value: 'share' },
{ id: 'icon-chexiao', value: 'undo' },
],
onClick: (value) => {
// Handle click events
},
},
],
});

Note: Custom icon IDs usually start with icon- and need to correspond to the icon names in the introduced iconfont.

Code Examples

Basic Toolbar

const graph = new Graph({
// Other configurations...
plugins: [
{
type: 'toolbar',
position: 'top-right',
getItems: () => [
{ id: 'zoom-in', value: 'zoom-in' },
{ id: 'zoom-out', value: 'zoom-out' },
{ id: 'undo', value: 'undo' },
{ id: 'redo', value: 'redo' },
{ id: 'auto-fit', value: 'fit' },
],
onClick: (value) => {
// redo, undo need to be used with the history plugin
const history = graph.getPluginInstance('history');
switch (value) {
case 'zoom-in':
graph.zoomTo(1.1);
break;
case 'zoom-out':
graph.zoomTo(0.9);
break;
case 'undo':
history?.undo();
break;
case 'redo':
history?.redo();
break;
case 'fit':
graph.fitView();
break;
default:
break;
}
},
},
],
});

Custom Styles

const graph = new Graph({
// Other configurations...
plugins: [
{
type: 'toolbar',
className: 'my-custom-toolbar',
style: {
backgroundColor: '#f5f5f5',
padding: '8px',
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.15)',
borderRadius: '8px',
border: '1px solid #e8e8e8',
opacity: '0.9',
marginTop: '12px',
marginLeft: '12px',
},
getItems: () => [
{ id: 'zoom-in', value: 'zoom-in' },
{ id: 'zoom-out', value: 'zoom-out' },
],
onClick: (value) => {
// Handle click events
},
},
],
});

Common style attributes include:

  • backgroundColor: Background color
  • padding: Padding
  • margin/marginTop/marginLeft, etc.: Margin
  • border: Border
  • borderRadius: Border radius
  • boxShadow: Shadow effect
  • opacity: Opacity
  • width/height: Width and height (default is content adaptive)
  • zIndex: Layer (default is 100)
  • display: Display mode (default is flex)

The toolbar container is set to display: flex by default, and child items use row layout by default (or change according to the direction configured by position). You can customize its appearance and position through style.

Asynchronous Loading of Toolbar Items

const graph = new Graph({
// Other configurations...
plugins: [
{
type: 'toolbar',
getItems: async () => {
// Toolbar configuration can be obtained from the server or other asynchronous sources
const response = await fetch('/api/toolbar-config');
const items = await response.json();
return items;
},
onClick: (value) => {
// Handle click events
},
},
],
});

Common Issues

1. Toolbar icons not displaying?

  • Check if the correct built-in icon ID is used
  • Ensure CSS styles are not overridden or conflicting

2. How to use with other plugins?

The toolbar is often used in conjunction with other plugins (such as history):

const graph = new Graph({
plugins: [
{
type: 'history',
key: 'history',
},
{
type: 'toolbar',
getItems: () => [
{ id: 'undo', value: 'undo' },
{ id: 'redo', value: 'redo' },
],
onClick: (value) => {
const history = graph.getPluginInstance('history');
if (value === 'undo') {
history.undo();
} else if (value === 'redo') {
history.redo();
}
},
},
],
});

3. How to dynamically update the toolbar?

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

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

Real Cases