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)

State

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

In data visualization, State is the core mechanism for implementing interactive feedback, highlighting, selection, and other effects. G2 provides a flexible and powerful state system that allows developers to configure different state styles for each mark in the chart, enabling various interactive scenarios such as mouse hover highlighting and click selection, greatly enhancing chart usability and expressiveness.

State style properties are consistent with the style properties supported by @antv/g, commonly including fill (fill color), stroke (stroke color), strokeWidth (stroke width), opacity (transparency), etc. For details, see Style.


Use Cases

  • Mouse hover highlighting data
  • Click to select/deselect data
  • Multi-dimensional interactive feedback (e.g., hover + select overlay)
  • Dynamic effects with animations for state transitions

Configuration Options

G2 supports configuring styles for different states at the mark level through the state field. Common states include:

State NameDescriptionTypical Scenario
activeStyle when highlightedMouse hover
inactiveStyle when not highlightedOther non-highlighted elements
selectedStyle when selectedMouse click
unselectedStyle when not selectedOther non-selected elements

Configuration Methods

1. Declarative Approach (Recommended)

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
const state = {
selected: { fill: 'red' },
active: { fill: 'green', stroke: 'black', lineWidth: 1 },
};
chart.options({
type: 'interval',
data: [
{ type: 'A', value: 30 },
{ type: 'B', value: 50 },
{ type: 'C', value: 20 },
],
encode: { x: 'type', y: 'value' },
state: {
active: { fill: 'red', stroke: 'blue', strokeWidth: 2 },
inactive: { fill: '#aaa' },
selected: { fill: 'orange', stroke: 'black', strokeWidth: 2 },
unselected: { fill: '#eee' },
},
interaction: { elementHighlight: true, elementSelect: true },
});
chart.render();

2. Dynamic Styles (Function Support)

State style properties support functions that dynamically return styles based on data:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ type: 'A', value: 30 },
{ type: 'B', value: 50 },
{ type: 'C', value: 20 },
],
encode: { x: 'type', y: 'value' },
state: {
active: {
fill: (d) => (d.value > 40 ? 'red' : 'blue'),
},
},
interaction: { elementHighlight: true },
});
chart.render();

State Interactions and Priority Mechanism

G2 supports multiple states being active simultaneously. When the same property is configured by multiple states, the final effective style is chosen based on priority.

Priority order:

selected: 3
unselected: 3
active: 2
inactive: 2
default: 1
  • selected/unselected have higher priority, commonly used for click selection
  • active/inactive have secondary priority, commonly used for hover highlighting
  • default is the default style

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ letter: 'A', frequency: 0.08167 },
{ letter: 'B', frequency: 0.01492 },
{ letter: 'C', frequency: 0.02782 },
],
encode: { x: 'letter', y: 'frequency' },
state: {
selected: { fill: 'red' },
active: { fill: 'green', stroke: 'black', lineWidth: 1 },
},
interaction: { elementHighlight: true, elementSelect: true },
});
chart.render();
  • When hovering, the active state takes effect, showing green color and black stroke
  • When clicked, both selected and active states are active, but selected has higher priority, so the final fill color is red

Common Interactions and State Integration

G2 provides rich interactions that, combined with state styles, can achieve various interactive effects:

NameDescriptionTypical States
brushAxisHighlightAxis brush highlightactive/inactive
brushHighlightArea brush highlightactive/inactive
brushXHighlightX-axis brush highlightactive/inactive
brushYHighlightY-axis brush highlightactive/inactive
elementHighlightHover highlightactive/inactive
elementHighlightByColorHighlight by coloractive/inactive
elementHighlightByXHighlight by Xactive/inactive
legendHighlightLegend highlightactive/inactive
elementSelectClick selectionselected/unselected
elementSelectByColorSelect by colorselected/unselected
elementSelectByXSelect by Xselected/unselected

Typical Scenario Examples

1. Highlight Interaction (elementHighlight)

Using the elementHighlight interaction plugin with active and inactive state styles to achieve mouse hover highlighting:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ letter: 'A', frequency: 0.08167 },
{ letter: 'B', frequency: 0.01492 },
{ letter: 'C', frequency: 0.02782 },
],
encode: { x: 'letter', y: 'frequency' },
state: {
active: { fill: 'red' },
inactive: { fill: '#aaa' },
},
interaction: { elementHighlight: true },
});
chart.render();

Effect Description:

  • When hovering over a bar, it applies the active style, while other bars apply the inactive style.

2. Selection Interaction (elementSelect)

Using the elementSelect interaction plugin with selected and unselected state styles to achieve click selection:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ letter: 'A', frequency: 0.08167 },
{ letter: 'B', frequency: 0.01492 },
{ letter: 'C', frequency: 0.02782 },
],
encode: { x: 'letter', y: 'frequency' },
state: {
selected: { fill: 'orange', stroke: 'black', strokeWidth: 2 },
unselected: { fill: '#eee' },
},
interaction: { elementSelect: true },
});
chart.render();

Effect Description:

  • When clicking a bar, it applies the selected style, while other bars apply the unselected style.

3. Multiple State Overlay (Highlight + Selection)

Supports simultaneous highlighting and selection, commonly used in dashboards and BI reports:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ type: 'A', value: 30 },
{ type: 'B', value: 50 },
{ type: 'C', value: 20 },
],
encode: { x: 'type', y: 'value' },
state: {
active: { fill: 'yellow' },
inactive: { fill: '#eee' },
selected: { fill: 'orange', stroke: 'black', strokeWidth: 2 },
unselected: { fill: '#ccc' },
},
interaction: { elementHighlight: true, elementSelect: true },
});
chart.render();

4. State and Animation Integration

State transitions can be combined with animations (such as fade in/out, scaling, etc.) to enhance the interactive experience. For details, see Animation System.

chart.options({
type: 'interval',
state: {
active: {
fill: 'red',
opacity: (d) => (d.value > 40 ? 1 : 0.5),
},
},
// Other configurations...
});

Advanced Usage

1. Dynamic State Style Calculation

Supports setting style properties as functions that dynamically return styles based on data:

chart.options({
type: 'interval',
state: {
active: {
fill: (d) => (d.value > 40 ? 'red' : 'blue'),
},
},
// Other configurations...
});

Common Issues

  • State styles not taking effect?
    Check if interaction plugins (such as elementHighlight, elementSelect) are correctly configured, and ensure the state configuration is correct.

  • Multiple state conflicts?
    Make good use of the priority mechanism to avoid repeatedly configuring the same property in multiple high-priority states.

  • State styles conflicting with animations?
    Pay attention to animation configuration during state transitions to avoid visual anomalies caused by overlapping styles and animations.