logo

G2

  • Chart Gallery
  • Docs
  • Examples
  • Theme
  • Ecosystem
  • Productsantv logo arrow
  • 5.4.8
  • Get Started
  • Introduction
    • What is G2
    • Using in Frontend Frameworks
    • Experimental Spec API
  • Chart API
  • Core Concepts
    • G2's complete configuration system
    • Chart
      • Components of G2 Charts
      • Chart Lifecycle
    • Mark
      • Overview
      • area
      • box
      • boxplot
      • cell
      • chord
      • density
      • gauge
      • heatmap
      • image
      • interval
      • line
      • lineX
      • lineY
      • link
      • liquid
      • sunburst
      • beeswarm
      • 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
      • elementHoverScale
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
      • sliderWheel
      • fisheye
    • Composition
      • Overview
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • Theme
      • Overview
      • academy
      • classic
      • classicDark
    • Event Handling
    • 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)

sortColor

Previous
selectY
Next
sortX

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library
WeaveFox-AI Coding Assistant

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference
weavefoxWeaveFox-AI Developer Community

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-Interactive solution
weavefoxWeaveFox-AI Coding Assistant
© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview

sortColor is a commonly used data transform provided by G2, used for sorting the domain of discrete color channels. By specifying sorting criteria, you can arrange color groups in a chart by certain measure values (such as y, x, etc.) in ascending or descending order, making it more intuitive to display the size relationships or distribution trends of grouped data.
sortColor supports flexible configurations for sorting channels, aggregation methods, and slicing. It's commonly used in scenarios like highlighting key points, optimizing readability, and comparative analysis, especially suitable for grouped bar charts, grouped column charts, stacked charts, and more.


Use Cases

  • Grouped Bar/Column Chart Sorting: Arrange color groups (such as different categories or series) by values from high to low (or low to high) for easy comparison of data across groups.
  • Facet/Multi-series Sorting: In facet or multi-series scenarios, sort by certain metrics within groups to highlight differences between groups.
  • Combined with Other Transforms: Often used in combination with other transforms like sortX, dodgeX, etc., to achieve more complex data layouts and visual effects.

Configuration Options

PropertyDescriptionTypeDefault
bySpecify the channel for sorting (e.g., 'y', 'x', etc.)string'y'
reverseWhether to reverse the orderbooleanfalse
reducerAggregation method for multi-value comparisonReducer'max'

by

Specifies the channel used as the sorting criterion, commonly used values include 'y' (sort by y values), 'x' (sort by x values), etc.
For available channels, see G2 Visual Channel Documentation.

reverse

Whether to reverse the sort order. true means reverse the sorting result, false means maintain the default order. The actual sorting direction is also related to the reducer aggregation method (e.g., when reducer: 'max', reverse: true means descending order; when reducer: 'min', reverse: true means ascending order).

reducer

When the sorting criterion is an array or grouping, specifies the aggregation method. Supports 'max', 'min', 'sum', 'mean', 'median', 'first', 'last', and also allows custom functions.

type Primitive = number | string | boolean | Date;
type Reducer =
| 'max'
| 'min'
| 'sum'
| 'first'
| 'last'
| 'mean'
| 'median'
| ((I: number[], V: Primitive[]) => Primitive);

Examples

1. Grouped Bar Chart Sorting Color Groups by y Values (reverse Controls Order)

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ Category: 'A', Year: '2022', Value: 30 },
{ Category: 'A', Year: '2023', Value: 50 },
{ Category: 'B', Year: '2022', Value: 20 },
{ Category: 'B', Year: '2023', Value: 40 },
{ Category: 'C', Year: '2022', Value: 35 },
{ Category: 'C', Year: '2023', Value: 25 },
],
encode: { x: 'Category', y: 'Value', color: 'Year' },
transform: [
{ type: 'sortColor', by: 'y', reverse: true },
{ type: 'dodgeX' },
],
});
chart.render();

2. Complex Sorting with reducer Configuration

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
data: [
{ Category: 'A', Year: '2022', Value: 30 },
{ Category: 'A', Year: '2023', Value: 50 },
{ Category: 'B', Year: '2022', Value: 20 },
{ Category: 'B', Year: '2023', Value: 40 },
{ Category: 'C', Year: '2022', Value: 35 },
{ Category: 'C', Year: '2023', Value: 25 },
],
encode: { x: 'Category', y: 'Value', color: 'Year' },
transform: [
{ type: 'sortColor', by: 'y', reducer: 'min' }, // Sort by minimum value
{ type: 'dodgeX' },
],
});
chart.render();