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

box

上一篇
area
下一篇
boxplot

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

概述

box 标记用于绘制箱线图(boxplot),又称盒须图、盒式图,是一种用于展示一组数据分布情况的统计图表。箱线图通常包含以下几个关键统计值:

  • 最小值:数据集中的最小值(不包括异常值)
  • 下四分位数(Q1):数据集中排在 25%位置的值
  • 中位数(Q2):数据集中排在 50%位置的值
  • 上四分位数(Q3):数据集中排在 75%位置的值
  • 最大值:数据集中的最大值(不包括异常值)

box 标记的特殊之处在于,它的 y 通道对应的数据是一个包含这些统计值的数组,G2 会将这些数据映射为箱线图所需的 14 个点集合,形成完整的箱线图图形。

box

box 标记与 boxplot 标记的区别在于:

  • box 是原子标记,需要开发者手动指定 5 个统计点的数据
  • boxplot 是高阶标记,自带数据分组和数据统计聚合功能

因此,box 更适用于后端对超大数据进行计算和统计之后的可视化展示,而 boxplot 更适合用于前端数据的探索和分析过程。

箱线图的点集合结构

box 标记内部会将数据映射为以下 14 个点的集合,形成完整的箱线图:

p0 p2 p1
──────────┬──────────
│
│
│
│ p3
p4 ┌─────────┴──────────┐ p5
│ │
│ │
p8 ├────────────────────┤ p9
│ │
│ p10 │
p7 └─────────┬──────────┘ p6
│
│
│
───────────┴───────────
p12 p11 p13

配置项

属性描述类型默认值必选
encode配置 box 标记的视觉通道,包括 x、y、color、shape 等encode-✓
coordinate配置 box 标记的坐标系,坐标系会执行一系列点转换,从而改变标记的空间展示形式coordinate{type: 'cartesian' }
style配置 box 标记的图形样式style-

encode

配置 box 标记的视觉通道。

属性描述类型默认值必选
x绑定 box 标记的 x 属性通道,通常是分类字段encode-✓
y绑定 box 标记的 y 属性通道,通常是包含 5 个统计值的数组,按顺序为:最小值、下四分位数、中位数、上四分位数、最大值encode-✓
color绑定 box 标记的 color 属性通道,用于区分不同类别的箱线图encode-
shape绑定 box 标记的 shape 属性通道,可选值为 box、violin'box' | 'violin''box'
series绑定 box 标记的 series 属性通道,用于分组显示箱线图encode-

coordinate

box 标记在不同坐标系下的展示有所差别。根据坐标系或坐标系转换的不同,可以绘制不同形式的箱线图。

坐标系或坐标系转换坐标系配置图表
直角坐标系{ type: 'cartesian' }标准箱线图
极坐标系{ type: 'polar' }极坐标箱线图

style

配置 box 标记的图形样式。box 标记支持两种形状:box(默认)和 violin。

属性描述类型默认值必选
fill图形的填充色string | (datum, index, data, column) => string-
fillOpacity图形的填充透明度number | (datum, index, data, column) => number0.95
stroke图形的描边颜色string | (datum, index, data, column) => string#000
strokeOpacity描边透明度number | (datum, index, data, column) => number-
lineWidth图形描边的宽度number | (datum, index, data, column) => number1
lineDash描边的虚线配置,第一个值为虚线每个分段的长度,第二个值为分段间隔的距离[number,number] | (datum, index, data, column) => [number, number]-
opacity图形的整体透明度number | (datum, index, data, column) => number-
shadowColor图形阴影颜色string | (datum, index, data, column) => string-
shadowBlur图形阴影的高斯模糊系数number | (datum, index, data, column) => number-
shadowOffsetX设置阴影距图形的水平距离number | (datum, index, data, column) => number-
shadowOffsetY设置阴影距图形的垂直距离number | (datum, index, data, column) => number-
cursor鼠标样式。同 CSS 的鼠标样式string | (datum, index, data, column) => string'default'

示例

基础箱线图

使用 box 标记可以创建基础箱线图,需要提供包含 5 个统计值的数组数据。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'box',
data: [
{ x: 'Oceania', y: [1, 9, 16, 22, 24] },
{ x: 'East Europe', y: [1, 5, 8, 12, 16] },
{ x: 'Australia', y: [1, 8, 12, 19, 26] },
{ x: 'South America', y: [2, 8, 12, 21, 28] },
{ x: 'North Africa', y: [1, 8, 14, 18, 24] },
{ x: 'North America', y: [3, 10, 17, 28, 30] },
{ x: 'West Europe', y: [1, 7, 10, 17, 22] },
{ x: 'West Africa', y: [1, 6, 8, 13, 16] },
],
encode: {
x: 'x',
y: 'y',
color: 'x',
},
scale: {
x: { paddingInner: 0.6, paddingOuter: 0.3 },
y: { zero: true },
},
legend: false,
style: {
stroke: 'black',
},
});
chart.render();

分组箱线图

使用 series 通道可以创建分组箱线图,对不同类别的数据进行比较。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'box',
data: [
{ Species: 'I. setosa', type: 'SepalLength', bin: [4.3, 4.8, 5, 5.2, 5.8] },
{
Species: 'I. setosa',
type: 'SepalWidth',
bin: [2.3, 3.2, 3.4, 3.7, 4.4],
},
{ Species: 'I. setosa', type: 'PetalLength', bin: [1, 1.4, 1.5, 1.6, 1.9] },
{
Species: 'I. setosa',
type: 'PetalWidth',
bin: [0.1, 0.2, 0.2, 0.3, 0.6],
},
{
Species: 'I. versicolor',
type: 'SepalLength',
bin: [4.9, 5.6, 5.9, 6.3, 7],
},
{
Species: 'I. versicolor',
type: 'SepalWidth',
bin: [2, 2.5, 2.8, 3, 3.4],
},
{
Species: 'I. versicolor',
type: 'PetalLength',
bin: [3, 4, 4.35, 4.6, 5.1],
},
{
Species: 'I. versicolor',
type: 'PetalWidth',
bin: [1, 1.2, 1.3, 1.5, 1.8],
},
{
Species: 'I. virginica',
type: 'SepalLength',
bin: [4.9, 6.2, 6.5, 6.9, 7.9],
},
{
Species: 'I. virginica',
type: 'SepalWidth',
bin: [2.2, 2.8, 3, 3.2, 3.8],
},
{
Species: 'I. virginica',
type: 'PetalLength',
bin: [4.5, 5.1, 5.55, 5.9, 6.9],
},
{
Species: 'I. virginica',
type: 'PetalWidth',
bin: [1.4, 1.8, 2, 2.3, 2.5],
},
],
encode: {
x: 'type',
y: 'bin',
series: 'Species',
color: 'Species',
},
scale: {
x: { paddingInner: 0.2, paddingOuter: 0.1 },
y: { zero: true },
series: { paddingInner: 0.3, paddingOuter: 0.1 },
},
style: {
stroke: 'black',
},
tooltip: [
{ name: 'min', channel: 'y' },
{ name: 'q1', channel: 'y1' },
{ name: 'q2', channel: 'y2' },
{ name: 'q3', channel: 'y3' },
{ name: 'max', channel: 'y4' },
],
});
chart.render();

极坐标箱线图

通过配置极坐标系,可以创建极坐标下的箱线图。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'box',
data: [
{ x: 'Oceania', y: [1, 9, 16, 22, 24] },
{ x: 'East Europe', y: [1, 5, 8, 12, 16] },
{ x: 'Australia', y: [1, 8, 12, 19, 26] },
{ x: 'South America', y: [2, 8, 12, 21, 28] },
{ x: 'North Africa', y: [1, 8, 14, 18, 24] },
{ x: 'North America', y: [3, 10, 17, 28, 30] },
{ x: 'West Europe', y: [1, 7, 10, 17, 22] },
{ x: 'West Africa', y: [1, 6, 8, 13, 16] },
],
coordinate: {
type: 'polar',
innerRadius: 0.2,
},
encode: {
x: 'x',
y: 'y',
color: 'x',
},
scale: {
x: { paddingInner: 0.6, paddingOuter: 0.3 },
y: { zero: true },
},
style: {
stroke: 'black',
},
axis: {
y: { tickCount: 5 },
},
tooltip: [
{ name: 'min', channel: 'y' },
{ name: 'q1', channel: 'y1' },
{ name: 'q2', channel: 'y2' },
{ name: 'q3', channel: 'y3' },
{ name: 'max', channel: 'y4' },
],
legend: false,
});
chart.render();

常见问题

如何在前端进行数据分布情况的分析?

G2 提供了多种方式来进行数据分布的分析:

  • 使用 transform 进行数据转换,可以实现对数据进行统计分析,计算出最小值、下四分位数、中位数、上四分位数、最大值等统计值:
chart.box().data({
type: 'connector',
value: [
/* your detail data */
],
callback: (data) => {
// 在这里对数据进行统计分析
// 可以使用自定义算法或第三方库
return data;
},
});
  • 使用社区提供的算法库进行数据统计。

  • 直接使用 boxplot 标记,它是一个高阶标记,自带数据分组和统计聚合功能,更适合前端数据的探索和分析:

chart.boxplot().data(data).encode('x', 'category').encode('y', 'value');