自定义渲染
上一篇
结合@antv/g2
下一篇
自定义 Hook
Loading...
S2 的每一个单元格对应 AntV/G
的一个 Group 图形分组. 所以可以在单元格内添加任意 G 的图形,甚至是任意基于 G 的图表库,比如 AntV/G2
.
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);}};
通过 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,},}),);
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,},}),);
表格内的 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);