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)

Style

Previous
helix
Next
overview

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

Style in G2 is mainly used to control the visual style of mark and view. Supported styles refer to @antv/g supported styles.

Mark can set its own style or the style of the view:

({
type: 'interval',
style: {
// own style
stroke: 'black',
strokeWidth: 2,
},
viewStyle: {
// View style
viewFill: 'red',
contentFill: 'yellow',
},
});
// API
// First way
chart
.interval()
.style('stroke', 'black')
.style('strokeWidth', 2)
.viewStyle('viewFill', 'red')
.viewStyle('contentFill', 'yellow');
// Second way
chart
.interval()
.style({
stroke: 'black',
strokeWidth: 2,
})
.viewStyle({
viewFill: 'red',
contentFill: 'yellow',
});

View can set its own style:

({
type: 'view',
style: {
viewFill: 'red',
contentFill: 'yellow',
},
});
// API
// First way
chart.style('viewFill', 'red').style('contentFill', 'yellow');
// Second way
chart.style({
viewFill: 'red',
contentFill: 'yellow',
});

Mark Style

In addition to mark.encode, the visual properties of the mark can also be set through mark.style. There are two main differences between the two:

  • The channel set by mark.encode will be a bit special, either unique to the mark, such as the src channel of image; or it has some special logic, such as the x channel that affects the generation of the x-direction coordinate axis.
  • mark.encode is more inclined to set up channels related to data, but mark.style prefers to set up channels that have nothing to do with data. Although mark.style also supports callbacks to set up data-driven channels.

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart
.interval()
.data({
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/fb9db6b7-23a5-4c23-bbef-c54a55fee580.csv',
})
.encode('x', 'letter')
.encode('y', 'frequency')
.style('fill', 'steelblue') // Set data-independent channels
.style('strokeWidth', (d) => (d.frequency > 0.1 ? 2 : 1)) // Set data-related channels
.style('stroke', (d) => (d.frequency > 0.1 ? 'red' : 'black'))
.axis('y', { labelFormatter: '.0%' });
chart.render();

View Style

The styles of each area can be set in the form of ${name}${Style}, among them, Style is all styles supported by the rectangle of G, such as fill,stroke,etc. Please note that the first letter should be capitalized to form camel case.

  • view${Style}- Set the style of the view area
  • plot${Style}- Set the style of the drawing area
  • main${Style}- Set the style of the main area
  • content${Style}- Set styles of the content areas

For example, color each area in the picture below:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
height: 280,
inset: 10,
marginTop: 30,
marginLeft: 40,
marginBottom: 10,
marginRight: 20,
style: {
// Set the view style
viewFill: '#4e79a7',
plotFill: '#f28e2c',
mainFill: '#e15759',
contentFill: '#76b7b2',
},
children: [
{
type: 'point',
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/commits.json',
},
encode: {
x: (d) => new Date(d.time).getUTCHours(),
y: (d) => new Date(d.time).getUTCDay(),
size: 'count',
shape: 'point',
},
transform: [{ type: 'group', size: 'sum' }, { type: 'sortY' }],
scale: { y: { type: 'point' } },
style: { shape: 'point', fill: '#59a14f' },
axis: {
x: { title: 'time (hours)', tickCount: 24 },
y: { title: 'time (day)', grid: true },
},
legend: false,
},
],
});
chart.render();