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.
G2 supports configuring styles for different states at the mark level through the state
field. Common states include:
State Name | Description | Typical Scenario |
---|---|---|
active | Style when highlighted | Mouse hover |
inactive | Style when not highlighted | Other non-highlighted elements |
selected | Style when selected | Mouse click |
unselected | Style when not selected | Other non-selected elements |
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();
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();
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: 3unselected: 3active: 2inactive: 2default: 1
selected
/unselected
have higher priority, commonly used for click selectionactive
/inactive
have secondary priority, commonly used for hover highlightingdefault
is the default styleimport { 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();
active
state takes effect, showing green color and black strokeselected
and active
states are active, but selected
has higher priority, so the final fill color is redG2 provides rich interactions that, combined with state styles, can achieve various interactive effects:
Name | Description | Typical States |
---|---|---|
brushAxisHighlight | Axis brush highlight | active/inactive |
brushHighlight | Area brush highlight | active/inactive |
brushXHighlight | X-axis brush highlight | active/inactive |
brushYHighlight | Y-axis brush highlight | active/inactive |
elementHighlight | Hover highlight | active/inactive |
elementHighlightByColor | Highlight by color | active/inactive |
elementHighlightByX | Highlight by X | active/inactive |
legendHighlight | Legend highlight | active/inactive |
elementSelect | Click selection | selected/unselected |
elementSelectByColor | Select by color | selected/unselected |
elementSelectByX | Select by X | selected/unselected |
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:
active
style, while other bars apply the inactive
style.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:
selected
style, while other bars apply the unselected
style.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();
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...});
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...});
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.