logo

G2

  • Docs
  • Chart Introduction
  • API
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.3
  • Get Started
  • Introduction
    • What is G2
    • Use In Framework
    • Experimental Spec API
  • Core Concepts
    • Chart
      • Components of G2 Charts
      • How to Use Charts
    • Mark
      • overview
      • area
      • box
      • boxplot
      • cell
      • chord
      • density
      • gauge
      • heatmap
      • image
      • interval
      • line
      • lineX
      • lineY
      • link
      • liquid
      • sunburst
      • point
      • polygon
      • range
      • rangeX
      • rangeY
      • rect
      • shape
      • text
      • vector
      • wordCloud
    • View
    • Data
      • overview
      • custom
      • ema
      • fetch
      • filter
      • fold
      • inline
      • join
      • kde
      • log
      • map
      • pick
      • rename
      • slice
      • sort
      • sortBy
    • Encode
    • Scale
      • overview
      • band
      • linear
      • log
      • ordinal
      • point
      • pow
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
    • Transform
      • overview
      • bin
      • binX
      • diffY
      • dodgeX
      • flexX
      • group
      • groupColor
      • groupX
      • groupY
      • jitter
      • jitterX
      • jitterY
      • normalizeY
      • pack
      • sample
      • select
      • selectX
      • selectX
      • sortColor
      • sortX
      • sortY
      • stackEnter
      • stackY
      • symmetryY
    • Coordinate
      • overview
      • fisheye
      • parallel
      • polar
      • radial
      • theta
      • transpose
      • cartesian3D
      • helix
    • Style
    • Animate
      • overview
      • fadeIn
      • fadeOut
      • growInX
      • growInY
      • morphing
      • pathIn
      • scaleInX
      • scaleInY
      • scaleOutX
      • scaleOutY
      • waveIn
      • zoomIn
      • zoomOut
    • State
    • Interaction
      • Overview
      • brushAxisHighlight
      • brushHighlight
      • brushXHighlight
      • brushYHighlight
      • brushFilter
      • brushXFilter
      • brushYFilter
      • chartIndex
      • elementHighlight
      • elementHighlightByColor
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • fisheye
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
    • Composition
      • overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • overview
      • Academy
      • classic
      • classicDark
    • event
    • Color
  • Chart API
  • Chart Component
    • 标题(Title)
    • Axis
    • Legend
    • Scrollbar
    • Slider
    • Tooltip
    • Label
  • Extra Topics
    • Graph
      • forceGraph
      • pack
      • sankey
      • tree
      • treemap
    • Geo
      • geoPath
      • geoView
    • 3D
      • Draw 3D Chart
      • point3D
      • line3D
      • interval3D
      • surface3D
    • Plugin
      • renderer
      • rough
      • lottie
      • a11y
    • Package on demand
    • Set pattern
    • Server-side rendering(SSR)
    • Spec Function Expression Support (Available in 5.3.0)
  • Whats New
    • New Version Features
    • Migration from v4 to v5
  • Frequently Asked Questions (FAQ)

shape

Previous
rect
Next
text

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

概述

形状图( shape )图形标记是 G2 中一种特殊的 Mark 类型,通常用来在图表上绘制静态的自定义图形,具有较高的灵活性和自由度,shape 标记可以用于添加自定义注解、水印、徽章等场景,是实现图表个性化的重要工具。在使用 shape 绘制图形时,可以从图表上下文中获取 document 对象,随后使用 document.createElement 创建完成注册的图形。更复杂的场景,开发者可能需要了解 G 自定义图形 的相关概念。

例如,有时需要在图表中的特定数据点添加标记来突出重要信息。以下示例展示了如何使用 shape 标记在折线图的关键点上添加自定义注解,我们通过创建基础图形 Circle 和基础图形 Text,结合 场景图 能力实现了一个自定义注解。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
const notes = (style, context) => {
const { x, y } = style;
const { document } = context;
const g = document.createElement('g', {});
const c1 = document.createElement('circle', {
style: {
cx: x,
cy: y,
r: 3,
fill: 'red',
},
});
const c2 = document.createElement('circle', {
style: {
cx: x,
cy: y,
r: 5,
lineWidth: 8,
stroke: 'red',
opacity: 0.3,
},
});
const text = document.createElement('text', {
style: {
x: x + 12,
y,
text: '最高降雨量',
fontSize: 12,
textAlign: 'left',
textBaseline: 'middle',
},
});
g.appendChild(c1);
g.appendChild(c2);
g.appendChild(text);
return g;
};
chart.options({
type: 'view',
autoFit: true,
children: [
{
type: 'line',
data: [
{ month: 'Jan.', rainfall: 18.9 },
{ month: 'Feb.', rainfall: 28.8 },
{ month: 'Mar.', rainfall: 39.3 },
{ month: 'Apr.', rainfall: 81.4 },
{ month: 'May', rainfall: 47 },
{ month: 'Jun.', rainfall: 20.3 },
],
encode: {
x: 'month',
y: 'rainfall',
},
},
{
type: 'shape',
data: [{ month: 'Apr.', rainfall: 81.4 }],
encode: {
x: 'month',
y: 'rainfall',
},
style: {
render: (style, context) => notes(style, context),
},
},
],
});
chart.render();

更多的案例,可以查看图形标注 - 徽章水印页面。

配置项

属性描述类型默认值必选
encode配置 shape 标记的视觉通道,包括x、y,用于指定视觉元素属性和数据之间的关系encode-
style配置 shape 标记的图形样式,包括x、y、render等style-✓

encode

配置 shape 标记的视觉通道,包括 x、y,用于指定视觉元素属性和数据之间的关系。

属性描述类型默认值必选
x绑定 shape 标记的 x 属性通道,一般是 data 中的数值或字符,用于设置标记的 x 位置encode-若 style 中未配置,则为 ✓
y绑定 shape 标记的 y 属性通道,一般是 data 中的数值或字符,用于设置标记的 y 位置encode-若 style 中未配置,则为 ✓

style

配置 shape 标记的图形样式。

属性描述类型默认值必选
x统一设置 shape 标记的 x 位置(相对定位的百分比 | 绝对定位的像素值),优先级最高(string | number)-若 encode 中未配置,则为 ✓
y统一设置 shape 标记的 y 位置(相对定位的百分比 | 绝对定位的像素值),优先级最高(string | number)-若 encode 中未配置,则为 ✓
render自定义图形渲染函数,接收样式 style 和 上下文 context 参数,返回 G 的 DisplayObject(style, context) => DisplayObject-✓
{ ...rest }自定义图形的额外参数,都会作为 render 函数的 style 参数object-

示例

水印

在图表中添加水印可以保护数据的安全性和知识产权。以下示例展示了如何使用 shape 标记在图表上添加水印。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
// 定义水印渲染方法
const watermark = (style, context) => {
const { document, canvas } = context;
const { width, height } = canvas.context.config;
const g = document.createElement('g', {});
// 创建重复的水印文字
const spacing = 120; // 水印间距
for (let x = 0; x < width; x += spacing) {
for (let y = 0; y < height; y += spacing) {
const text = document.createElement('text', {
style: {
x: x,
y: y,
text: 'AntV G2',
transformOrigin: 'center',
transform: 'rotate(-30)',
fontSize: 16,
fill: '#000',
textAlign: 'center',
textBaseline: 'middle',
fillOpacity: 0.2,
},
});
g.appendChild(text);
}
}
return g;
};
chart.options({
type: 'view',
autoFit: true,
children: [
// 创建饼图
{
type: 'interval',
zIndex: 2,
data: [
{ item: '分类一', count: 40 },
{ item: '分类二', count: 21 },
{ item: '分类三', count: 17 },
{ item: '分类四', count: 13 },
{ item: '分类五', count: 9 },
],
encode: { y: 'count', color: 'item' },
transform: [{ type: 'stackY' }],
coordinate: {
type: 'theta',
outerRadius: 0.8,
},
},
// 添加全图水印
{
type: 'shape',
zIndex: 1,
style: {
x: 0,
y: 0,
render: (style, context) => watermark(style, context),
},
},
],
});
chart.render();