logo

G2

  • Chart Gallery
  • Docs
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.3.3
  • Get Started
  • Introduction
    • What is G2
    • Using in Frontend Frameworks
    • Experimental Spec API
  • Chart 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
      • connector
      • 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
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
      • pow
    • Transform
      • Overview
      • bin
      • binX
      • diffY
      • dodgeX
      • flexX
      • group
      • groupColor
      • groupX
      • groupY
      • jitter
      • jitterX
      • jitterY
      • normalizeY
      • pack
      • sample
      • select
      • selectX
      • selectY
      • sortColor
      • sortX
      • sortY
      • stackEnter
      • stackY
      • symmetryY
    • Coordinate
      • Overview
      • fisheye
      • parallel
      • polar
      • theta
      • transpose
      • radial
      • 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
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
      • fisheye
    • Composition
      • Overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • Overview
      • academy
      • classic
      • classicDark
    • Events
    • Color Mapping
  • Chart Component
    • Title
    • Axis
    • Legend
    • Scrollbar
    • Slider
    • Tooltip
    • Data 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...

Overview

The shape mark is a special Mark type in G2, typically used to draw static custom graphics on charts. It has high flexibility and freedom. The shape mark can be used for adding custom annotations, watermarks, badges, and other scenarios, making it an important tool for personalizing charts. When using shape to draw graphics, you can obtain the document object from the chart context, and then use document.createElement to create registered graphics. For more complex scenarios, developers may need to understand concepts related to G custom graphics.

For example, sometimes you need to add markers at specific data points in a chart to highlight important information. The following example shows how to use the shape mark to add custom annotations at key points on a line chart. We create basic graphics Circle and basic graphics Text, combined with scene graph capabilities to implement a custom annotation.

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: 'Highest Rainfall',
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();

For more examples, you can visit the Graphic Annotation - Badge Watermark page.

Configuration

PropertyDescriptionTypeDefaultRequired
encodeConfigure the visual channels of the shape mark, including x, y, to specify the relationship between visual element properties and dataencode-
styleConfigure the graphic style of the shape mark, including x, y, render, etc.style-✓

encode

Configure the visual channels of the shape mark, including x, y, to specify the relationship between visual element properties and data.

PropertyDescriptionTypeDefaultRequired
xBind the x property channel of the shape mark, usually a numeric or character value from data to set the x position of the markencode-✓ if not configured in style
yBind the y property channel of the shape mark, usually a numeric or character value from data to set the y position of the markencode-✓ if not configured in style

style

Configure the graphic style of the shape mark.

PropertyDescriptionTypeDefaultRequired
xUniformly set the x position of the shape mark (relative positioning percentage | absolute positioning pixel value), highest priority(string | number)-✓ if not configured in encode
yUniformly set the y position of the shape mark (relative positioning percentage | absolute positioning pixel value), highest priority(string | number)-✓ if not configured in encode
renderCustom graphic rendering function that receives style and context parameters and returns G's DisplayObject(style, context) => DisplayObject-✓
{ ...rest }Additional parameters for custom graphics, all passed as style parameters to the render functionobject-

Examples

Watermark

Adding watermarks to charts can protect data security and intellectual property. The following example shows how to use the shape mark to add watermarks to a chart.

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
// Define watermark rendering method
const watermark = (style, context) => {
const { document, canvas } = context;
const { width, height } = canvas.context.config;
const g = document.createElement('g', {});
// Create repeating watermark text
const spacing = 120; // Watermark spacing
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: [
// Create pie chart
{
type: 'interval',
zIndex: 2,
data: [
{ item: 'Category 1', count: 40 },
{ item: 'Category 2', count: 21 },
{ item: 'Category 3', count: 17 },
{ item: 'Category 4', count: 13 },
{ item: 'Category 5', count: 9 },
],
encode: { y: 'count', color: 'item' },
transform: [{ type: 'stackY' }],
coordinate: {
type: 'theta',
outerRadius: 0.8,
},
},
// Add full chart watermark
{
type: 'shape',
zIndex: 1,
style: {
x: 0,
y: 0,
render: (style, context) => watermark(style, context),
},
},
],
});
chart.render();