Loading...
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.
This plugin is mainly used for:
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 eventsif (value === 'zoom-in') {graph.zoomTo(1.1);} else if (value === 'zoom-out') {graph.zoomTo(0.9);} else if (value === 'auto-fit') {graph.fitView();}},},],});
Property | Description | Type | Default Value | Required |
---|---|---|---|---|
type | Plugin type | string | toolbar | ✓ |
key | Unique identifier for the plugin, used for updates | string | - | |
className | Additional CSS class name for the toolbar DOM element | string | - | |
position | Toolbar position relative to the canvas, optional values | string | 'top-left' | |
style | Custom style for the toolbar DOM element, common values | CSSStyleDeclaration | - | |
getItems | Returns the list of toolbar items | () => ToolbarItem[] | Promise<ToolbarItem[]> | - | ✓ |
onClick | Callback function after a toolbar item is clicked | (value: string, target: Element) => void | - |
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, bottomAttribute | Description | Type | Default Value |
---|---|---|---|
backgroundColor | Background color | string | #fff |
border | Border | string | 1px solid #e8e8e8 |
borderRadius | Border radius | string | 4px |
height | Height | string | auto |
margin | Margin | string | 12px |
opacity | Opacity | number | 0.9 |
padding | Padding | string | 8px |
width | Width | string | auto |
Each toolbar item (ToolbarItem) includes the following attributes:
Attribute | Description | Type | Required |
---|---|---|---|
id | Icon ID of the item, see Built-in Icons | string | ✓ |
value | Value returned when the item is clicked | string | ✓ |
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 fullscreenIn addition to using built-in icons, you can also use custom icons by introducing third-party icon libraries (such as Alibaba iconfont):
// Introduce iconfont scriptconst iconFont = document.createElement('script');iconFont.src = '//at.alicdn.com/t/font_8d5l8fzk5b87iudi.js'; // Replace with your iconfont script URLdocument.head.appendChild(iconFont);// Use custom icons in the toolbarconst 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.
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 pluginconst 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;}},},],});
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 colorpadding
: Paddingmargin
/marginTop
/marginLeft
, etc.: Marginborder
: BorderborderRadius
: Border radiusboxShadow
: Shadow effectopacity
: Opacitywidth
/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
.
const graph = new Graph({// Other configurations...plugins: [{type: 'toolbar',getItems: async () => {// Toolbar configuration can be obtained from the server or other asynchronous sourcesconst response = await fetch('/api/toolbar-config');const items = await response.json();return items;},onClick: (value) => {// Handle click events},},],});
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();}},},],});
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 positiongraph.updatePlugin({key: 'my-toolbar',position: 'bottom-right',});