logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.48
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 内置节点
        • 节点通用配置项
        • Diamond 菱形
        • Donut 甜甜圈
        • Ellipse 椭圆形
        • Hexagon 六边形
        • Html HTML
        • Image 图片
        • Rect 矩形
        • Star 五角形
        • Triangle 三角形
        • Circle 圆形
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 内置边
        • 边通用配置项
        • Cubic 三次贝塞尔曲线
        • CubicHorizontal 水平三次贝塞尔曲线
        • CubicVertical 垂直三次贝塞尔曲线
        • Line 直线
        • Polyline 折线
        • Quadratic 二次贝塞尔曲线
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 内置组合
        • 组合配置项
        • Circle 圆形
        • Rect 矩形
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 内置布局
      • 布局通用配置项
      • AntvDagre 布局
      • Circular 环形布局
      • ComboCombined 复合布局
      • CompactBox 紧凑树
      • Concentric 同心圆布局
      • D3Force 力导向布局
      • D3Force3D 3D 力导向布局
      • Dagre 布局
      • Dendrogram 生态树
      • Fishbone 鱼骨布局
      • Force 力导向布局
      • ForceAtlas2 力导向布局
      • Fruchterman 力导向布局
      • Grid 网格布局
      • Indented 缩进树
      • MDS 高维数据降维布局
      • Mindmap 脑图树
      • Radial 径向布局
      • Random 随机布局
      • Snake 蛇形布局
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 内置交互
      • AutoAdaptLabel 标签自适应显示
      • BrushSelect 框选
      • ClickSelect 点击选中
      • CollapseExpand 展开/收起元素
      • CreateEdge 创建边
      • DragCanvas 拖拽画布
      • DragElement 拖拽元素
      • DragElementForce 力导向拖拽元素
      • FixElementSize 缩放画布时固定元素大小
      • FocusElement 聚焦元素
      • HoverActivate 悬停激活
      • LassoSelect 套索选择
      • OptimizeViewportTransform 优化视口变换
      • ScrollCanvas 滚动画布
      • ZoomCanvas 缩放画布
    • 自定义交互
  • 插件 Plugin
    • 插件总览
    • 内置插件
      • Background 背景
      • BubbleSets 气泡集
      • Contextmenu 上下文菜单
      • EdgeBundling 边绑定
      • EdgeFilterLens 边过滤镜
      • Fisheye 鱼眼放大镜
      • Fullscreen 全屏展示
      • GridLine 网格线
      • History 历史记录
      • Hull 轮廓包围
      • Legend 图例
      • Minimap 小地图
      • Snapline 对齐线
      • Timebar 时间条
      • Toolbar 工具栏
      • Tooltip 提示框
      • Watermark 水印
    • 自定义插件
  • 数据处理 Transform
    • 数据处理总览
    • 内置数据处理
      • MapNodeSize 动态调整节点大小
      • PlaceRadialLabels 径向标签
      • ProcessParallelEdges 平行边
    • 自定义数据处理
  • 主题 Theme
    • 主题总览
    • 自定义主题
    • 色板
    • 自定义色板
  • 动画 Animation
    • 动画总览
    • 自定义动画
  • 扩展阅读
    • 事件
    • 渲染器
    • 坐标系
    • 下载图片
    • 使用 iconfont
    • 使用 3D
    • 项目打包
  • 版本特性
    • 新版本特性
    • 升级到 5.0
  • 常见问题
  • 参与贡献

自定义插件

上一篇
Watermark 水印
下一篇
数据处理总览

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

概述

自定义插件可以实现一些额外的功能,例如添加额外的组件、渲染逻辑等。通过自定义插件可以很好地实现功能解耦,更好地进行管理、编排代码以及后续维护。

使用场景

添加额外的组件、渲染逻辑等

  • 额外的组件:如内置插件的提示框、小地图、对齐线、网格线、上下文菜单、水印等
  • 渲染逻辑:如内置插件的边绑定,以及远程数据加载(示例)等

内置插件无法满足需求

当内置插件无法完全满足业务需求时,用户也可以通过自定义插件(继承内置插件)进行调整和修改。

(如果需要内置插件支持的特性是较通用的,或者内置插件存在 Bug ,这种时候欢迎大家到 Github 提 Issue 或者 PR )

自定义插件示例

与交互类似,插件的实现也相当灵活,你可以以你喜欢的风格实现你的插件。

下面列举几个简单的自定义插件实现:

远程数据加载

在图实例化过程中自动加载远程数据:

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();
}
}
  • 在这个例子中,我们模拟实现了一个数据加载插件,在使用该插件后,实例化 Graph 时不用再传入数据,该插件会自动加载远程数据。

  • BasePlugin 是所有插件的基类,每个自定义插件都需要继承这个基类实现。

import { BasePlugin, Graph, register, ExtensionCategory } from '@antv/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 graph = new Graph({
container: 'container',
width: 200,
height: 100,
plugins: ['remote-data-source'],
});
graph.render();

自动判断节点数量开启或关闭动画

import type { BasePluginOptions, RuntimeContext } from '@antv/g6';
import { BasePlugin, GraphEvent } from '@antv/g6';
interface AutoSwitchAnimationOptions extends BasePluginOptions {
maxLength: number; // 节点数量达到这个值后关闭全局动画
}
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();
}
}
  • 在这个例子中,我们监听 GraphEvent.BEFORE_RENDER 事件,在响应中判断当前节点数量是否大于指定值,是的话则关闭全局动画,否则开启
  • maxLength 是定义的配置项,可在初始化画布实例时传入具体配置,插件配置

注册插件

通过 G6 提供的 register 方法注册即可

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

配置插件

  • 可在 plugins 中传入插件类型名称或配置参数对象,详见配置插件

  • 比如前面的自动判断节点数量开启或关闭动画,配置如下:

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