logo

S2

  • 使用文档
  • API 文档
  • 图表示例
  • 在线体验
  • 更新日志
  • 所有产品antv logo arrow
  • 2.x
  • 简介
  • 快速上手
  • 基础教程
    • 基本概念
      Updated
    • 表形态
      • 透视表
        Updated
      • 明细表
        Updated
    • 字段标记
      Updated
    • 小计总计
    • 排序
      • 基础排序
        Updated
      • 组内排序
        Updated
    • 主题配置
      Updated
    • Tooltip
      Updated
    • 数据格式化
      New
    • 多行文本
      New
    • 国际化
    • 分页
      New
  • 进阶教程
    • 单元格渲染类型
      • 链接
      • 图片
        New
      • 视频
        New
      • 迷你图表
      • 结合@antv/g2
      • 自定义渲染
    • 自定义
      • 自定义 Hook
        Updated
      • 自定义行列头分组
        New
      • 自定义 Icon
        Updated
      • 自定义单元格对齐方式
        Updated
      • 自定义单元格宽高
        Updated
      • 自定义排序操作
        Updated
      • 自定义折叠/展开节点
        New
    • 交互
      • 基础交互
        Updated
      • 自定义交互
      • 隐藏列头
        Updated
      • 行列宽高调整
        Updated
      • 合并单元格
      • 滚动
        Updated
      • 复制与导出
        New
      • 高亮/选中单元格
        New
    • 分析组件
      • 简介
        New
      • 高级排序
        Updated
      • 维度切换
        Updated
      • 导出
        Updated
      • 分页
        Updated
      • 维度下钻
        Updated
    • 表格组件
      • 编辑表
      • 趋势分析表
        Updated
    • 高清适配
      Updated
    • 获取表格实例
    • 表格自适应
    • 获取单元格数据
      Updated
    • 注册 AntV/G 插件
      New
    • 透视组合图
      Experimental
    • Vue 3.0 组件 (停止维护)
  • 扩展阅读
    • 数据流处理
      • 透视表
      • 明细表
    • 布局流程
      • 透视表
      • 明细表
    • 性能介绍
      Updated
  • 贡献指南
  • 常见问题
  • S2 2.0 升级指南

自定义渲染

上一篇
结合@antv/g2
下一篇
自定义 Hook

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...

绘制 G 自定义图形

S2 的每一个单元格对应 AntV/G 的一个 Group 图形分组. 所以可以在单元格内添加任意 G 的图形,甚至是任意基于 G 的图表库,比如 AntV/G2.

1 自定义单元格,重写绘制逻辑,添加任意图形

import { Image as GImage } from '@antv/g';
import { CornerCell } from '@antv/s2';
class CustomCornerCell extends CornerCell {
initCell()
super.initCell()
// 绘制任意图形
this.appendChild(...)
}
drawBackgroundShape() {
const url = 'https://gw.alipayobjects.com/zos/antfincdn/og1XQOMyyj/1e3a8de1-3b42-405d-9f82-f92cb1c10413.png';
this.backgroundShape = this.appendChild(
new GImage({
style: {
...this.getBBoxByType(),
src: url,
},
}),
);
this.drawTextShape();
}
}
const s2Options = {
cornerCell: (node, spreadsheet, headerConfig) => {
return new CustomCornerCell(node, spreadsheet, headerConfig);
}
};

2 直接在表格 (Canvas) 上绘制任意图形

通过 s2.getCanvas() 获取 G 的 Canvas 实例。

import { Rect } from '@antv/g';
await s2.render();
// 直接在表格 (Canvas) 上绘制任意图形
s2.getCanvas().appendChild(
new Rect({
style: {
x: 300,
y: 200,
width: 100,
height: 100,
fill: '#1890FF',
fillOpacity: 0.8,
stroke: '#F04864',
strokeOpacity: 0.8,
lineWidth: 4,
radius: 100,
zIndex: 999,
},
}),
);

3 手动获取指定单元格实例 (Group) 后绘制任意图形

import { Rect } from '@antv/g';
await s2.render();
const targetCell = s2.facet.getDataCells()[0];
targetCell?.appendChild(
new Rect({
style: {
x: 0,
y: 100,
width: 20,
height: 20,
fill: '#396',
fillOpacity: 0.8,
stroke: '#ddd',
strokeOpacity: 0.8,
lineWidth: 4,
radius: 10,
zIndex: 999,
},
}),
);

4 手动获取指定单元格实例 (Group) 后绘制 icon

表格内的 Icon 也是一种特殊图形,可以通过 GuiIcon 生成图标实例,然后绘制。

import { GuiIcon } from '@antv/s2';
await s2.render();
const targetCell = s2.facet.getDataCells()[0];
const size = 12;
const meta = targetCell.getMeta();
// 例:绘制在右下角
const icon = new GuiIcon({
x: meta.x + meta.width - size,
y: meta.y + meta.height - size,
name: 'Trend',
width: size,
height: size,
fill: 'red',
});
icon.addEventListener('click', (e) => {
console.log('trend icon click:', e);
});
targetCell.appendChild(icon);

5 效果

preview
查看示例