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)

rect

Previous
rangeY
Next
shape

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

rect is a core chart element used to draw rectangular marks. It implements different visualization requirements by defining the starting point, ending point, width, height, and other graphic properties (such as color and style) of rectangles. rect can dynamically adjust the position, size, and style of rectangles based on bound data, thus intuitively displaying data distribution, comparison relationships, or density information. rect is widely used in scenarios such as bar charts, heatmaps, and matrix charts.

Core functional features of rect:

  • Drawing rectangular shapes: rect is the fundamental unit for building rectangular charts, which can be used to construct bar, block, heatmap and other charts. Each rectangular unit displays specific data content by mapping numerical or categorical information from the data.
  • Rich encoding mapping: Users can freely map data fields to visual attributes such as X-axis, Y-axis, size, color, etc. of rectangles. Provides flexible customization capabilities, generating various styles of rectangular graphics through the combination of fields and styles.
  • High extensibility and interactivity: Supports interactive functions (such as click, highlight, zoom, etc.), enhancing dynamic interaction between graphics and users. Suitable for rectangular visualization needs in different fields, such as grid charts (heatmap), data density distribution charts, etc.
  • Seamless integration with G2 ecosystem: As part of the G2 Mark system, rect can be freely combined with other components (such as line, point, etc.) to meet complex data visualization requirements.

Getting Started

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'rect',
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/athletes.json',
},
encode: { x: 'weight', y: 'height', color: 'sex' },
transform: [{ type: 'bin', opacity: 'count' }],
style: { inset: 0.5 },
});
chart.render(); // Render chart

For more examples, please visit the Chart Examples page.

Configuration

PropertyDescriptionTypeDefaultRequired
encodeConfigure visual channels for rect mark, including x, y, color, etc., used to specify relationships between visual element properties and dataencode-✓
transformConfigure data transformation operations for rect (such as binning, sorting, filtering, etc.).transform-
styleConfigure graphic styles for rectstyle-

encode

Configure visual channels for rect mark, an important configuration that defines the mapping relationship between data fields and visual properties, determining how data is transformed into visual representation.

PropertyDescriptionTypeDefaultRequired
colorBind the color property channel of rect mark. If a data field is mapped to the color channel, the data will be grouped and split into multiple areas of different colorsencode-
shapeBind the shape property channel of rect mark, changing the drawing shape of the graphic markencode'rect'

color

The color visual channel affects the fill color of rect graphic marks (when applying certain hollow shapes, such as hollow, it will change the stroke color of the graphic). When applied to point charts, it generally maps categorical fields and groups the data.

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'rect',
data: [
{ x: 1, y: 1, type: 'type1' },
{ x: 1, y: 2, type: 'type2' },
{ x: 2, y: 1, type: 'type3' },
{ x: 2, y: 2, type: 'type1' },
],
transform: [{ type: 'bin' }],
encode: { shape: 'rect', x: 'x', y: 'y', color: 'type' },
style: { inset: 0.5 },
});
chart.render(); // Render chart

shape

Through the shape property of encode, you can specify the geometric shape of cells. Shape determines what shape each cell is rendered as on the canvas. The supported shapes for shape mark are as follows:

ShapeDescription
rectRectangle
hollowHollow rectangle

(() => {
const shapeMap = [
{
shape: 'rect',
label: 'Rectangle',
},
{
shape: 'hollow',
label: 'Hollow Rectangle',
},
];
const chart = new G2.Chart();
chart.options({
type: 'rect',
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/athletes.json',
},
encode: { shape: 'rect', x: 'weight', y: 'height', color: 'sex' },
transform: [{ type: 'bin', opacity: 'count' }],
style: { inset: 0.5 },
});
const handleSetShape = (shape) => {
// Set the selected coordinate system
chart.options({
encode: { shape },
});
chart.render(); // Re-render chart
};
// Insert Encode-Color selector
const selectorContainer = document.createElement('div');
selectorContainer.textContent = 'Select Shape ';
const selector = document.createElement('select');
selector.innerHTML = shapeMap.map(
(shape, index) =>
`<option value="${shape.shape}" ${index === 0 ? 'selected' : ''}>${
shape.label
}</option>`,
);
selector.onchange = (e) => {
handleSetShape(e.target.value);
};
selectorContainer.appendChild(selector);
const node = chart.getContainer();
node.insertBefore(selectorContainer, node.childNodes[0]);
chart.render();
return node;
})();

For more encode configurations, please check the encode introduction page.

transform

transform is a core configuration item for data transformation, allowing preprocessing of raw data before it's bound to graphic marks. By processing data, it generates structured data more suitable for visualization, thus more clearly expressing data distribution, density, or statistical characteristics.

Common transformation types include:

  • bin: Bin continuous data to generate histogram rectangles
  • stackY: Stack rectangles vertically, automatically calculating stacked height for each category

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'rect',
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/athletes.json',
},
encode: { x: 'weight', color: 'sex' },
transform: [
{ type: 'binX', y: 'count' },
{ type: 'stackY', orderBy: 'series' },
],
});
chart.render();

For more transform configurations, please check the transform introduction page.

style

style is used to set the appearance styles of chart elements, including fill color, border style, shadow effects, etc.

PropertyDescriptionTypeDefault
radiusBorder radius for all four corners of the rectanglenumber | (datum, index, data, column) => number0
radiusTopLeftBorder radius for top-left cornernumber | (datum, index, data, column) => number0
radiusTopRightBorder radius for top-right cornernumber | (datum, index, data, column) => number0
radiusBottomRightBorder radius for bottom-right cornernumber | (datum, index, data, column) => number0
radiusBottomLeftBorder radius for bottom-left cornernumber | (datum, index, data, column) => number0
insetInner padding for all four directions of the rectanglenumber | (datum, index, data, column) => number0
insetLeftLeft inner spacingnumber | (datum, index, data, column) => number0
insetRightRight inner spacingnumber | (datum, index, data, column) => number0
insetBottomBottom inner spacingnumber | (datum, index, data, column) => number0
insetTopTop inner spacingnumber | (datum, index, data, column) => number0
fillFill color of the graphicnumber | (datum, index, data, column) => string-
fillOpacityFill opacity of the graphicnumber | (datum, index, data, column) => number-
strokeStroke of the graphicnumber | (datum, index, data, column) => string-
strokeOpacityStroke opacitynumber | (datum, index, data, column) => number-
lineWidthWidth of the graphic strokenumber | (datum, index, data, column) => number-
lineDashDashed line configuration for stroke. First value is the length of each dash segment, second value is the gap distance. Setting lineDash to [0, 0] results in no stroke.[number,number] |(datum, index, data, column) => [number, number]-
opacityOverall opacity of the graphicnumber | (datum, index, data, column) => number-
shadowColorShadow color of the graphicnumber | (datum, index, data, column) => string-
shadowBlurGaussian blur coefficient for the graphic shadownumber | (datum, index, data, column) => number-
shadowOffsetXHorizontal distance of shadow from the graphicnumber | (datum, index, data, column) => number-
shadowOffsetYVertical distance of shadow from the graphicnumber | (datum, index, data, column) => number-
cursorMouse cursor style. Same as CSS cursor style, default 'default'.number | (datum, index, data, column) => string'default'

Try it out

For more style configurations, please check the style introduction page.