logo

G2

  • 文档
  • 图表介绍
  • API
  • 图表示例
  • 主题
  • 周边生态
  • 所有产品antv logo arrow
  • 5.3.3
  • 快速上手
  • 简介
    • 什么是 G2
    • 在前端框架中使用
    • Spec 和 API
  • 核心概念
    • 图表(Chart)
      • G2 图表组成
      • 如何使用图表
    • 标记(Mark)
      • 概览
      • 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)
      • 概览
      • custom
      • ema
      • fetch
      • filter
      • fold
      • inline
      • join
      • kde
      • log
      • map
      • pick
      • rename
      • slice
      • sort
      • sortBy
    • 编码(Encode)
    • 比例尺(Scale)
      • 概览
      • band
      • linear
      • log
      • ordinal
      • point
      • quantile
      • quantize
      • sqrt
      • threshold
      • time
      • pow
    • 转换(Transform)
      • 概览
      • 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)
      • 概览
      • fisheye
      • parallel
      • polar
      • theta
      • transpose
      • radial
      • cartesian3D
      • helix
    • 样式(Style)
    • 动画(Animate)
      • 概览
      • fadeIn
      • fadeOut
      • growInX
      • growInY
      • morphing
      • pathIn
      • scaleInX
      • scaleInY
      • scaleOutX
      • scaleOutY
      • waveIn
      • zoomIn
      • zoomOut
    • 状态(State)
    • 交互(Interaction)
      • 概览
      • brushAxisHighlight
      • brushHighlight
      • brushXHighlight
      • brushYHighlight
      • brushFilter
      • brushXFilter
      • brushYFilter
      • chartIndex
      • elementHighlight
      • elementHighlightByColor
      • elementHighlightByX
      • elementSelect
      • elementSelectByColor
      • elementSelectByX
      • fisheye
      • legendFilter
      • legendHighlight
      • poptip
      • scrollbarFilter
      • sliderFilter
    • 复合(Composition)
      • 概览
      • facetCircle
      • facetRect
      • repeatMatrix
      • spaceFlex
      • spaceLayer
      • timingKeyframe
    • 主题(Theme)
      • 概览
      • academy
      • classic
      • classicDark
    • 事件(Event)
    • 颜色映射(Color)
  • 图表 API
  • 图表组件
    • 标题(Title)
    • 坐标轴(Axis)
    • 图例(Legend)
    • 滚动条(Scrollbar)
    • 缩略轴(Slider)
    • 提示信息(Tooltip)
    • 数据标签(Label)
  • 进阶主题
    • 关系图(Graph)
      • forceGraph
      • pack
      • sankey
      • tree
      • treemap
    • 地图(Geo)
      • geoPath
      • geoView
    • 3D 图表(3D Chart)
      • 绘制 3D 图表
      • point3D
      • line3D
      • interval3D
      • surface3D
    • 插件扩展(Plugin)
      • renderer
      • rough
      • lottie
      • a11y
    • 按需打包
    • 设置纹理
    • 服务端渲染(SSR)
    • Spec 函数表达式支持 (5.3.0 支持)
  • 版本特性
    • 新版本特性
    • v4 升级 v5 指南
  • 常见问题 FAQ

flexX

上一篇
dodgeX
下一篇
group

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

概述

flexX 是一个用于调整柱形图宽度的转换方法。它允许根据数据值动态调整柱形的宽度,使得柱形的宽度能够反映另一个数据维度,从而在可视化中展示更多的信息维度。这种转换特别适用于:

  1. 需要同时展示两个定量变量的场景,如国家 GDP(宽度)和人均 GDP(高度)的关系
  2. 展示市场份额和细分结构的商业分析图表
  3. 人口统计数据的多维度可视化,如人口总数(宽度)和年龄分布(高度)
  4. 销售数据分析,如销售总额(宽度)和利润率(高度)的关系

通过 flexX 转换,我们可以在传统柱状图的基础上增加一个额外的数据维度,使图表更加信息丰富。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
width: 800,
height: 400,
paddingLeft: 60,
});
chart
.interval()
.data([
{ category: '电子产品', sales: 1200000, profitRate: 0.15 },
{ category: '服装', sales: 800000, profitRate: 0.25 },
{ category: '食品', sales: 600000, profitRate: 0.12 },
{ category: '家具', sales: 400000, profitRate: 0.18 },
{ category: '图书', sales: 200000, profitRate: 0.3 },
])
.transform({ type: 'flexX', field: 'sales' })
.encode('x', 'category')
.encode('y', 'profitRate')
.encode('color', 'category')
.scale('y', { nice: true })
.axis('y', {
title: '利润率',
labelFormatter: '.0%',
});
chart.render();

使用场景

  1. 不等宽柱形图:当需要柱子的宽度反映数据的某个维度时,例如国家 GDP 总量决定柱子宽度,而柱高展示人均 GDP。

  2. 马利梅柯图(Marimekko Chart):结合 stackY 和 normalizeY 转换,可以创建复杂的商业分析图表,展示市场份额和细分数据。
    使用国家 GDP 总量作为柱形宽度,人均 GDP 作为柱形高度:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
width: 1000,
paddingBottom: 100,
});
chart
.interval()
.data({
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/90873879-09d7-4842-a493-03fb560267bc.csv',
})
.transform({ type: 'flexX', field: 'gdp' })
.encode('x', 'country')
.encode('y', 'value')
.encode('color', 'country')
.axis('y', { labelFormatter: '~s' });
chart.render();

配置项

属性描述类型默认值必选
field指定生成权重数组的字段string | (d: any) => Primitive[]-否
channel指定生成权重数组的通道stringy否
reducer聚合每一组权重的函数Reducersum否

类型定义

// 基础数据类型
type Primitive = number | string | boolean | Date;
// 聚合函数类型
type Reducer = 'sum' | ((I: number[], V: Primitive[]) => Primitive);

参数说明

  • field: 用于指定柱形宽度的数据字段。当设置了 field 时,其优先级高于 channel。
  • channel: 指定用于计算柱形宽度的编码通道,默认使用 'y' 通道的值。
  • reducer: 聚合函数,用于计算最终的宽度值。默认使用 'sum' 求和。

示例

1. 马利梅柯图(Marimekko Chart)

结合 stackY 和 normalizeY 转换创建市场分析图:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
width: 900,
height: 800,
paddingLeft: 0,
paddingRight: 0,
});
chart
.interval()
.data({
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/3041da62-1bf4-4849-aac3-01a387544bf4.csv',
})
.transform({ type: 'flexX', reducer: 'sum' })
.transform({ type: 'stackY' })
.transform({ type: 'normalizeY' })
.encode('x', 'market')
.encode('y', 'value')
.encode('color', 'segment')
.scale('x', { paddingOuter: 0, paddingInner: 0.01 });
chart.render();

在这个例子中,flexX 转换使得每个市场部分的宽度与其总价值成正比,结合堆叠和归一化处理,可以清晰地展示市场份额的分布情况。

这个例子展示了如何使用 flexX 来可视化人口数据,其中柱子的宽度表示州/省的总人口数量,高度表示人口密度,颜色区分不同地区。

2. 时间序列数据分析

展示每月销售数据,使用交易量作为宽度,价格变化率作为高度:

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'interval',
width: 800,
height: 400,
paddingLeft: 60,
data: [
{ month: '1月', volume: 5000, priceChange: 0.08 },
{ month: '2月', volume: 8000, priceChange: -0.05 },
{ month: '3月', volume: 12000, priceChange: 0.12 },
{ month: '4月', volume: 6000, priceChange: -0.03 },
{ month: '5月', volume: 9000, priceChange: 0.15 },
{ month: '6月', volume: 15000, priceChange: -0.08 },
],
encode: {
x: 'month',
y: 'priceChange',
color: (d) => (d.priceChange > 0 ? 'red' : 'green'),
},
transform: [{ type: 'flexX', field: 'volume' }],
scale: { y: { nice: true } },
style: { radius: 4 },
axis: { y: { title: '价格变化率', labelFormatter: '.0%' } },
});
chart.render();

这个时间序列示例展示了如何使用 flexX 来可视化交易数据,其中柱形的宽度表示交易量大小,高度表示价格变化率,颜色区分涨跌情况。通过这种方式,我们可以同时观察到交易活跃度和价格走势的关系。