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)

Set pattern

Previous
Package on demand
Next
Server-Side Rendering (SSR)

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

Compared with monotonous fill colors, using pattern fills can enrich expressiveness and it's also useful in accessibility and black-and-white printing scenarios. To achieve this, we offer three methods in increasing order of complexity and usage cost:

  • Use built-in patterns.
  • Custom pattern using the G API.
  • Use other pattern sources.

Use built-in patterns

We have built three common patterns into the g-pattern, and the appearance can be easily adjusted through parameters. This is also the simplest way to use pattern:

  • Dots made of dots
  • Lines made of straight lines
  • Squares made of squares

The usage is as follows, first install the dependencies:

$ npm install @antv/g-pattern --save;

Then you can use the built-in patterns. In this example:

  • We used lines, and set attributes like background color, opacity, line color, and spacing.
  • Through repetition, the tiling mode is specified as horizontal and vertical directions
  • Through transform, the pattern is rotated 30 degrees clockwise
import { lines } from '@antv/g-pattern';
chart
//... Omit other imperative calls
.style('fill', (_, idx) => {
return {
image: lines({
backgroundColor: colors[idx],
backgroundOpacity: 0.65,
stroke: colors[idx],
lineWidth: 4,
spacing: 5,
}),
repetition: 'repeat',
transform: 'rotate(30)',
};
});

The effect is as follows:

built-in lines pattern

For more detailed parameter meanings and effects, see complete g-pattern API。

Definition using G API

When the above-mentioned built-in patterns do not meet the requirements, you can use G API to customize, just like describing the scene.

In this example, we first get document object from context, create a rect and a path by document.createElement, and using them as pattern sources:

mark.style('fill', ({ value }) => {
const { document } = chart.getContext().canvas;
const background = document.createElement('rect', {
style: {
width,
height,
fill: color,
},
});
const line = document.createElement('path', {
style: {
d: `
M 0 ${-height} L ${width * 2} ${height}
M ${-width} ${-height} L ${width} ${height}
M ${-width} 0 L ${width} ${height * 2}`,
stroke,
lineWidth: 1,
strokeOpacity: 0.9,
},
});
background.appendChild(line);
return {
image: background,
repetition: 'repeat',
};
});

The effect is as follows:

custom pattern with G API

For more usage, please refer to G API。

Use other sources

Refer to G API, other available patterns sources include:

  • Image URL, e.g.'http://example.png'
  • HTMLImageElement
  • HTMLCanvasElement
  • HTMLVideoElement

Among them, image URL, HTMLImageElement, and HTMLVideoElement are all static resources, while HTMLCanvasElement can be used to programmatically generate patterns, with the following results:

other pattern source

In this example, we used HTMLCanvasElement with canvas API to customize:

// 程序化生成
const canvas = createCanvas(width, height);
const ctx = canvas.getContext('2d');
drawRect(ctx, width, height, color);
drawLinePattern(ctx, stroke, width, height, cross);
// 使用
chart.style('fill', ({ value }) => {
return { image: canvas, repetition: 'repeat' };
});

It is not difficult to see that this programmatic generation method requires the users to have deep understanding of canvas API, but it also offers the highest level of flexibility.