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

Custom Plugin

Previous
Watermark
Next
Data Transformation Overview

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

Custom plugins can implement additional features, such as adding extra components, rendering logic, etc. Custom plugins can effectively achieve functional decoupling, better manage and orchestrate code, and facilitate subsequent maintenance.

Use Cases

Add extra components, rendering logic, etc.

  • Extra components: Such as built-in plugins like Tooltip, Minimap, Snapline, Grid, Context Menu, Watermark, etc.
  • Rendering logic: Such as built-in plugins like Edge Bundling, and Remote Data Loading (Example), etc.

When built-in plugins cannot meet the requirements

When built-in plugins cannot fully meet business needs, users can also make adjustments and modifications through custom plugins (inheriting built-in plugins).

(If the features supported by built-in plugins are more general, or if there are bugs in built-in plugins, you are welcome to submit issues or PRs on Github)

Custom Plugin Examples

Like interactions, the implementation of plugins is also quite flexible, and you can implement your plugin in your preferred style.

Here are a few simple custom plugin implementations:

Remote Data Loading

Automatically load remote data during graph instantiation:

import { BasePlugin } from '@antv/g6';
import type { BasePluginOptions, RuntimeContext } from '@antv/g6';
interface RemoteDataSourceOptions extends BasePluginOptions {}
class RemoteDataSource extends BasePlugin<RemoteDataSourceOptions> {
constructor(context: RuntimeContext, options: RemoteDataSourceOptions) {
super(context, options);
this.loadData();
}
private async loadData() {
// mock remote data
const data = {
nodes: [
{ id: 'node-1', x: 100, y: 100 },
{ id: 'node-2', x: 200, y: 200 },
],
edges: [{ source: 'node-1', target: 'node-2' }],
};
const { graph } = this.context;
graph.setData(data);
await graph.render();
}
}
  • In this example, we simulate a data loading plugin. After using this plugin, there is no need to pass data when instantiating the Graph, as the plugin will automatically load remote data.

  • BasePlugin is the base class for all plugins, and each custom plugin needs to inherit this base class.

(() => {
const { BasePlugin, Graph, register, ExtensionCategory } = window.g6;
class RemoteDataSource extends BasePlugin {
constructor(context, options) {
super(context, options);
this.loadData();
}
async loadData() {
// mock remote data
const data = {
nodes: [
{ id: 'node-1', style: { x: 25, y: 50 } },
{ id: 'node-2', style: { x: 175, y: 50 } },
],
edges: [{ source: 'node-1', target: 'node-2' }],
};
const { graph } = this.context;
graph.setData(data);
await graph.render();
}
}
register(ExtensionCategory.PLUGIN, 'remote-data-source', RemoteDataSource);
const container = window.createContainer({ width: 200, height: 100 });
const graph = new Graph({
container,
width: 200,
height: 100,
plugins: ['remote-data-source'],
});
graph.render();
return container;
})();

Automatically Enable or Disable Animation Based on Node Count

import type { BasePluginOptions, RuntimeContext } from '@antv/g6';
import { BasePlugin, GraphEvent } from '@antv/g6';
interface AutoSwitchAnimationOptions extends BasePluginOptions {
maxLength: number; // Disable global animation when the number of nodes reaches this value
}
class AutoSwitchAnimation extends BasePlugin<AutoSwitchAnimationOptions> {
static defaultOptions: Partial<AutoSwitchAnimationOptions> = {
maxLength: 1000,
};
constructor(context: RuntimeContext, options: AutoSwitchAnimationOptions) {
super(context, options);
this.bindEvents();
}
private bindEvents() {
const { graph } = this.context;
graph.on(GraphEvent.BEFORE_RENDER, this.switchAnimation);
}
private switchAnimation() {
const { graph } = this.context;
graph.setOptions({
animation: graph.getNodeData().length < this.options.maxLength,
});
}
private unbindEvents() {
const { graph } = this.context;
graph.off(GraphEvent.BEFORE_RENDER, this.switchAnimation);
}
destroy() {
this.unbindEvents();
super.destroy();
}
}
  • In this example, we listen to the GraphEvent.BEFORE_RENDER event and determine whether the current number of nodes exceeds a specified value. If so, global animation is disabled; otherwise, it is enabled.
  • maxLength is a defined configuration item that can be passed in when initializing the graph instance. Plugin Configuration

Register Plugin

Register using the method provided by G6

import { register, ExtensionCategory } from '@antv/g6';
import { MyCustomPlugin } from './my-custom-plugin';
register(ExtensionCategory.PLUGIN, 'my-custom-plugin', MyCustomPlugin);

Configure Plugin

  • You can pass the plugin type name or configuration parameter object in plugins, see Configure Plugin

  • For example, the previous Automatically Enable or Disable Animation Based on Node Count is configured as follows:

    const graph = new Graph({
    // Other configurations
    plugins: [
    {
    type: 'auto-switch-animation',
    maxLength: 500,
    },
    ],
    });