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)

box

Previous
area
Next
boxplot

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

概述

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');