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

binX

上一篇
bin
下一篇
diffY

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

概述

binX 是一个针对 X 通道数据进行分箱处理的重要函数。它主要用于将连续型数据按照指定的规则划分为离散的区间或分组,使数据更易于分析,并适用于特定的可视化场景(例如:热力图、条形图、网格图等)。

与 bin 类似,binX 专门用于 X 轴方向的数据分箱,而 bin 通常处理二维(X 和 Y)或单维度的数据。binX 可以独立应用,只对 X 通道的数据进行分箱,而无需处理其他维度的数据。

使用场景

  • 将连续的 X 通道数据划分为多个离散区间:根据数据范围,通过指定分箱规则,生成离散的区间(如 [0-10), [10-20) 等)。
  • 转换原始数据格式:通过分箱的处理,生成新的字段(如指定输出字段名),用于表示数据所属区间。
  • 便于统计数据分布:分箱后的数据可直接用于可视化处理(如统计每个区间内的频率或值)。
  • 单独处理 X 通道数据维度:当 Y 通道无需分箱时,binX 可以仅对 X 通道数据进行分箱,灵活调整数据处理逻辑。

希望对 Y 通道进行分箱,使用 binX + transpose 坐标系。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'rect',
autoFit: true,
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/unemployment2.json',
},
encode: { x: 'rate' },
transform: [{ type: 'binX', y: 'count' }],
});
chart.render();

配置项

属性描述类型默认值
thresholds对 x 分箱的数量numberd3.thresholdScott
[channel]输出到具体 mark 的 channel 数据的聚合方式channel

thresholdsX

binX 是一种专门用于对 X 轴方向上的连续型数据进行分箱(离散化)的变换操作。通过配置 thresholdsX,可以明确指定 X 轴方向上的分箱边界,从而实现对数据的精细化控制。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
let thresholdsX;
chart.options({
type: 'rect', // 图表类型为矩形图(直方图)
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/movies.json',
},
encode: {
x: 'IMDB Rating', // X 轴编码为 IMDB 评分
y: 'Rotten Tomatoes Rating', // Y 轴编码为 Rotten Tomatoes 评分
},
transform: [
{
type: 'bin', // 数据转换类型为分箱
color: 'count', // 通过颜色编码表示每个分箱内的数据点数量
},
],
});
// 插入 thresholdsX 的输入框
const container = document.createElement('div');
const thresholdsX_Text = document.createElement('span');
thresholdsX_Text.textContent = 'thresholdsX: ';
const thresholdsX_Input = document.createElement('input');
thresholdsX_Input.setAttribute('type', 'number');
thresholdsX_Input.addEventListener('input', (e) => {
thresholdsX = e.target.value;
chart.options({
transform: [
{
type: 'bin',
color: 'count',
thresholdsX,
thresholdsY,
},
],
});
chart.render();
});
container.appendChild(thresholdsX_Text);
container.appendChild(thresholdsX_Input);
const node = chart.getContainer();
node.insertBefore(container, node.childNodes[0]);
chart.render();

channel

理论上,channel 可以设置为所有的通道值,具体可以参考 encode 文档。所有的枚举值如下:

type Channel =
| 'x'
| 'y'
| 'z'
| 'x1'
| 'y1'
| 'series'
| 'color'
| 'opacity'
| 'shape'
| 'size'
| 'key'
| 'groupKey'
| 'position'
| 'series'
| 'enterType'
| 'enterEasing'
| 'enterDuration'
| 'enterDelay'
| 'updateType'
| 'updateEasing'
| 'updateDuration'
| 'updateDelay'
| 'exitType'
| 'exitEasing'
| 'exitDuration'
| 'exitDelay'
| `position${number}`;

示例

使用 bin + opacity 渲染出颜色分类直方图

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'rect',
autoFit: true,
data: {
type: 'fetch',
value: 'https://assets.antv.antgroup.com/g2/athletes.json',
},
encode: { x: 'weight', color: 'sex' },
transform: [
{ type: 'binX', y: 'count' },
{ type: 'stackY', orderBy: 'series' },
],
style: { inset: 0.5 },
});
chart.render();

泊松分布

import { Chart } from '@antv/g2';
const random = d3Random.randomPoisson(Math.pow(10, 2.6));
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'rect',
autoFit: true,
data: new Array(5000).fill(0).map(random),
encode: { x: (d) => d },
transform: [{ type: 'binX', y: 'count' }],
style: { stroke: 'white' },
tooltip: {
title: (d, i, data, column) => ({
value: `${column.x.value[i]} ~ ${column.x1.value[i]}`,
}),
},
});
chart.render();