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)

dodgeX

Previous
diffY
Next
flexX

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

概述

dodgeX 是一种分组布局转换,它通过生成并应用 series 通道值,使图表元素能够按系列进行分组展示。dodgeX 转换主要通过以下步骤工作:

  1. 按分组条件(默认为 x 通道)将数据进行分组
  2. 使用 color 通道的值生成 series 通道的值
  3. 按顺序和间距调整同一分组内不同系列的坐标位置,形成并列结构

使用场景

dodgeX 转换主要用于将同组数据按系列在坐标系上错开排列的场景,以突显各系列之间的数据差异和分布特征。

例如下面的案例展示了美国各州不同年龄段人口的分布情况,通过 dodgeX 转换让不同年龄段的数据在同一州内并排展示,便于直观比较。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
autoFit: true,
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/f129b517-158d-41a9-83a3-3294d639b39e.csv',
format: 'csv',
},
axis: { y: { labelFormatter: '~s' } },
encode: { x: 'state', y: 'population', color: 'age' },
transform: [
{ type: 'sortX', by: 'y', reverse: true, slice: 6 },
{ type: 'dodgeX' },
],
});
chart.render();

配置项

属性描述类型默认值必选
groupBy数据分组的通道或通道组合string | string[]x
orderBy分组内元素的排序规则TransformOrder() => null
reverse是否逆序排列分组内的元素booleanfalse
padding分组内元素之间的间距(0 ~ 1)number0

groupBy

groupBy 用于指定数据分组的通道或通道组合,默认按 x 通道分组,也可以指定为其他通道或多个通道的组合。

orderBy

orderBy 用于指定分组内元素的排序规则,支持多种排序策略:

type Primitive = number | string | boolean | Date;
type TransformOrder =
| 'value'
| 'sum'
| 'series'
| 'maxIndex'
| string[]
| null
| ((data: Record<string, Primitive>) => Primitive);

reverse

reverse 用于控制是否逆序排列分组内的元素。当设置为 true 时,分组内的元素会以与默认相反的顺序排列。

padding

padding 用于控制分组内元素之间的间距,取值范围为 0 到 1。值越大,元素之间的间距越大;当值为 0 时,元素之间没有间距。

示例

以下示例展示了 dodgeX 转换各配置项的功能:

  • groupBy: 按 x 通道(季度)分组显示各部门数据
  • orderBy: 设置为 value,按业绩值排序组内元素
  • reverse: 设置为 true,使组内元素按业绩值从高到低排列
  • padding: 设置组内元素间距为 0.1

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
const data = [
{ 季度: 'Q1', 部门: '销售部', 业绩: 90, 年份: '2024' },
{ 季度: 'Q1', 部门: '市场部', 业绩: 80, 年份: '2024' },
{ 季度: 'Q1', 部门: '研发部', 业绩: 70, 年份: '2024' },
{ 季度: 'Q2', 部门: '销售部', 业绩: 90, 年份: '2024' },
{ 季度: 'Q2', 部门: '市场部', 业绩: 70, 年份: '2024' },
{ 季度: 'Q2', 部门: '研发部', 业绩: 80, 年份: '2024' },
{ 季度: 'Q3', 部门: '销售部', 业绩: 70, 年份: '2024' },
{ 季度: 'Q3', 部门: '市场部', 业绩: 80, 年份: '2024' },
{ 季度: 'Q3', 部门: '研发部', 业绩: 90, 年份: '2024' },
{ 季度: 'Q4', 部门: '销售部', 业绩: 80, 年份: '2024' },
{ 季度: 'Q4', 部门: '市场部', 业绩: 70, 年份: '2024' },
{ 季度: 'Q4', 部门: '研发部', 业绩: 90, 年份: '2024' },
];
chart.options({
type: 'interval',
autoFit: true,
data,
encode: {
x: '季度',
y: '业绩',
color: '部门',
},
transform: [
{
type: 'dodgeX',
groupBy: 'x',
orderBy: 'value',
reverse: true,
padding: 0.1,
},
],
});
chart.render();