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)

sqrt

Previous
quantize
Next
threshold

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

概述

sqrt 是一种连续型非线性比例尺,它本质上是指数固定为 0.5 的 pow 比例尺。sqrt 比例尺的映射函数为 y = x ^ 0.5 + b,其中 x 是输入数据,b 是偏移量。

sqrt 比例尺属于连续比例尺的一种,适用于:

  • 需要压缩较大数值范围差异的数据
  • 强调较小数值差异的可视化场景
  • 映射面积等需要平方根变换的视觉通道

与线性比例尺相比,sqrt 比例尺对大数值的压缩程度较高,适合处理变化范围较大的数据。

映射效果示例

sqrt 比例尺能够使数据在视觉上更加均匀分布,特别是对于具有较大数值范围的数据集。

  • 使用 sqrt 比例尺效果

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
autoFit: true,
data: [
{ year: '1991', value: 1 },
{ year: '1992', value: 4 },
{ year: '1993', value: 9 },
{ year: '1994', value: 16 },
{ year: '1995', value: 25 },
],
encode: { x: 'year', y: 'value' },
scale: { y: { type: 'sqrt' } },
children: [
{ type: 'line', labels: [{ text: 'value', style: { dx: -10, dy: -12 } }] },
{ type: 'point', style: { fill: 'white' }, tooltip: false },
],
});
chart.render();
  • 未使用 sqrt 比例尺效果

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
autoFit: true,
data: [
{ year: '1991', value: 1 },
{ year: '1992', value: 4 },
{ year: '1993', value: 9 },
{ year: '1994', value: 16 },
{ year: '1995', value: 25 },
],
encode: { x: 'year', y: 'value' },
children: [
{ type: 'line', labels: [{ text: 'value', style: { dx: -10, dy: -12 } }] },
{ type: 'point', style: { fill: 'white' }, tooltip: false },
],
});
chart.render();

配置项

属性描述类型默认值必选
domain设置数据的定义域范围number[]输入数据的最大最小值范围
domainMin设置数据的定义域最小值number输入数据的最小值
domainMax设置数据的定义域最大值number输入数据的最大值
range设置数据映射的值域范围number[] | string[][0, 1]
rangeMin设置数据映射的值域最小值number | string0
rangeMax设置数据映射的值域最大值number | string1
unknown对于 undefined, NaN,null 空值,返回的数据anyundefined
tickCount设置推荐的 tick 生成数量,tickCount 只是建议值,不会完全按照这个值产生 ticknumber5
tickMethod设置生成 tick 的方法,常用于自定义 tick(min: number, max: number, count: number) => number[]d3-ticks
round输出值去四舍五入booleanfalse
clamp将映射值限定在 range 的范围内booleanfalse
nice扩展 domain 范围,让输出的 tick 展示得更加友好booleanfalse
interpolate自定义插值函数(a: number, b: number) => (t: number) => T(a, b) => (t) => a * (1 - t) + b * t
exponent设定指数,对于 sqrt 比例尺,该值固定为 0.5number0.5

示例

基础使用

使用 sqrt 比例尺映射散点图中的点大小,使数值差异更加明显。

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'point',
autoFit: true,
data: {
type: 'fetch',
value: 'https://gw.alipayobjects.com/os/antvdemo/assets/data/bubble.json',
},
encode: {
x: 'GDP',
y: 'LifeExpectancy',
size: 'Population',
color: 'continent',
shape: 'point',
},
scale: { size: { type: 'sqrt', range: [4, 50] } },
style: { fillOpacity: 0.3, lineWidth: 1 },
legend: { size: false },
});
chart.render();

代码解读

  • 这是一个气泡图(point 图),每个点代表一个国家。
  • 数据通过远程 fetch 获取,包含 GDP、预期寿命、人口等字段。
  • encode 部分将 GDP 映射到 x 轴,预期寿命映射到 y 轴,人口数映射到点的大小(size),大洲映射到颜色。
  • scale.size 使用了 sqrt,将人口数通过幂次变换映射到点的半径,range: [4, 20] 控制最小最大点的大小。
  • 视觉样式设置了透明度和描边宽度,关闭了 size 图例。
  • 通过 chart.render() 渲染图表。

结合颜色渐变

使用 sqrt 比例尺处理颜色渐变映射

import { Chart } from '@antv/g2';
const chart = new Chart({
container: 'container',
});
chart.options({
type: 'view',
autoFit: true,
children: [
{
type: 'point',
data: {
type: 'fetch',
value:
'https://gw.alipayobjects.com/os/bmw-prod/56b6b137-e04e-4757-8af5-d75bafaef886.csv',
},
encode: { x: 'date', y: 'value', color: 'value', shape: 'point' },
scale: {
color: { type: 'sqrt', domain: [0, 1], range: ['#1689F1', '#1AC07D'] },
},
style: { stroke: '#000', strokeOpacity: 0.2 },
tooltip: {
items: [
{
channel: 'x',
name: 'year',
valueFormatter: (d) => d.getFullYear(),
},
{ channel: 'y' },
],
},
},
{ type: 'lineY', data: [0], style: { stroke: '#000', strokeOpacity: 0.2 } },
],
});
chart.render();

代码解读

  • 这是一个带有辅助线的点图(scatter plot)。
  • 主体是 point 图,数据同样通过 fetch 获取,包含日期和数值。
  • encode 部分将日期映射到 x 轴,value 映射到 y 轴,同时 value 也映射到颜色(color)。
  • scale.color 使用 sqrt,将 value 通过幂次变换映射到颜色渐变(蓝到绿),domain: [0, 1] 指定输入数据范围。
  • 点有黑色描边和一定透明度。
  • tooltip 配置了自定义的年份格式化。
  • 还添加了一条 y=0 的辅助线(lineY)。
  • 通过 chart.render() 渲染图表。

这个例子中,数据通过 sqrt 比例尺映射到颜色,相比线性映射,能更好地展示区域的差异。