logo

F2

  • 教程
  • 组件API
  • 图表示例
  • 所有产品antv logo arrow
  • 5.10.0
  • 顶层 API - F2
  • 组件 - Component
  • 画布 - Canvas
  • 时间轴 - Timeline
  • 图表 - Chart
    • 图表 - Chart
    • 几何标记 - Geometry
    • 线 - Line
    • 区间 - Interval
    • 点 - Point
    • 面积 - Area
    • K 线图 - Candlestick
    • 坐标轴 - Axis
    • 图例 - Legend
    • 提示 - tooltip
    • 饼图标签 - PieLabel
    • 标注 - Guide
      • 标签标注 - TagGuide
      • 点标注 - PointGuide
      • 辅助线标注 - LineGuide
      • 矩形标注 - RectGuide
      • 图片标注 - ImageGuide
      • 文本标注 - TextGuide
      • 标注 - Guide
    • 滚动条 - ScrollBar
    • 放大镜 - Magnifier
  • 仪表盘 - Gauge
  • 旭日图 - Sunburst
  • 矩形树图 - Treemap

坐标轴 - Axis

上一篇
K 线图 - Candlestick
下一篇
图例 - Legend

资源

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

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会
weavefoxWeaveFox-智能研发技术社区

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
weavefoxWeaveFox-前端智能研发
© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

坐标轴配置。F2 的坐标轴的组成如下:

术语英文对应属性
坐标轴文本labelstyle.label
坐标轴线linestyle.line
坐标轴刻度线tickLinestyle.tickLine
坐标轴网格线gridstyle.grid

TypeScript 类型定义

interface AxisProps {
visible?: boolean;
field: string;
position?: 'top' | 'right' | 'bottom' | 'left';
formatter?: (value: any) => string | number;
type?: 'identity' | 'linear' | 'cat' | 'timeCat';
tickCount?: number;
range?: [number, number];
mask?: string;
min?: number;
max?: number;
nice?: boolean;
ticks?: Array<string | number>;
style?: StyleProps;
grid?: 'arc' | 'line';
labelAutoRotate?: boolean;
labelAutoHide?: boolean;
safetyDistance?: number | string;
}
interface StyleProps {
label?: TextStyleProps | LabelCallback;
line?: LineStyleProps;
tickLine?: TickLineProps;
grid?: LineStyleProps | GridCallback;
labelOffset?: number | string;
symbol?: MarkerStyleProps | MarkerStyleProps[];
width?: number | string;
height?: number | string;
}
interface Tick {
/** 归一化值 (0-1) */
value: number;
/** 显示文本 */
text: string;
/** 原始值 */
tickValue: string | number;
}
interface TickLineProps {
length?: number;
stroke?: string;
lineWidth?: number | string;
lineDash?: Array<string | number>;
}
interface MarkerStyleProps {
/** 标记类型 */
symbol?: 'circle' | 'square' | 'arrow';
/** 标记半径 */
radius?: string | number;
}

Usage

import { Canvas, Chart, Interval, Axis } from '@antv/f2';
const data = [
{ genre: 'Sports', sold: 5 },
{ genre: 'Strategy', sold: 10 },
{ genre: 'Action', sold: 20 },
{ genre: 'Shooter', sold: 20 },
{ genre: 'Other', sold: 40 },
];
<Canvas context={context}>
<Chart data={data}>
<Axis field="genre" />
<Axis field="sold" />
<Interval x="genre" y="sold" />
</Chart>
</Canvas>;

Props

部分属性可参考 scale 图表度量,度量详细介绍可见:度量

基础配置

属性类型默认值说明
fieldstring-坐标轴的数据字段(必填)
visiblebooleantrue是否显示该坐标轴
position'top' | 'right' | 'bottom' | 'left'自动判断坐标轴显示位置

度量配置

属性类型默认值说明
type'identity' | 'linear' | 'cat' | 'timeCat'-度量类型
tickCountnumber-坐标轴刻度点个数
range[number, number]-输出范围 [min, max],值域 0-1
maskstring-时间格式化 mask
minnumber-数值范围最小值
maxnumber-数值范围最大值
nicebooleantrue优化数值范围使刻度均匀分布
ticksArray<string | number>-自定义刻度值
formatter(value: any) => string | number-格式化刻度点文本

标签自动处理

属性类型默认值说明
labelAutoRotatebooleanfalse自动旋转标签以防止重叠
labelAutoHidebooleanfalse自动隐藏重叠标签
safetyDistancenumber | string2重叠检测安全边距

样式配置(style)

属性类型默认值说明
style.labelTextStyleProps | LabelCallback#808080, 20px标签样式
style.lineLineStyleProps#E8E8E8, 1px坐标轴线样式
style.tickLineTickLineProps#E8E8E8刻度线样式
style.gridLineStyleProps | GridCallback#E8E8E8, 1px网格线样式(默认虚线)
style.labelOffsetnumber | string'15px'标签偏移距离
style.symbolMarkerStyleProps | MarkerStyleProps[]-轴箭头/圆点标记
style.widthnumber | string-组件宽度
style.heightnumber | string-组件高度

极坐标配置

属性类型默认值说明
grid'arc' | 'line'-极坐标网格线类型

默认样式值

来源:packages/f2/src/theme.ts

const defaultStyle = {
labelOffset: '15px',
line: { stroke: '#E8E8E8', lineWidth: '1px' },
symbol: { fill: '#E8E8E8', radius: '10px' },
tickLine: { stroke: '#E8E8E8' },
label: { fill: '#808080', fontSize: '20px' },
grid: { stroke: '#E8E8E8', lineWidth: '1px', lineDash: ['4px'] },
};

用法示例

格式化刻度值

<Axis
field="sold"
formatter={(value) => value.toFixed(2) + '%'}
/>

自定义标签样式(函数形式)

<Axis
field="value"
formatter={(v) => v.toFixed(2) + '%'}
style={{
label: (text, index, ticks) => {
// text: formatter 处理后的文本,如 "-0.48%"
// ticks: 所有刻度数组,ticks[index].tickValue 是原始值
const number = parseFloat(text);
if (number > 0) {
return { text: '+' + text, fill: '#F5222D' };
} else if (number === 0) {
return { fill: '#000', fontWeight: 'bold' };
} else {
return { fill: '#52C41A' };
}
},
}}
/>

使用 ticks 数组数据

<Axis
field="value"
style={{
label: (text, index, ticks) => {
const total = ticks.length;
const isFirst = index === 0;
const isLast = index === total - 1;
if (isFirst || isLast) {
return { fill: '#1890FF', fontWeight: 'bold' };
}
return { fill: '#808080' };
},
}}
/>

注意: ticks[index].value 是归一化值(0-1),原始值请使用 ticks[index].tickValue。

自定义网格线(函数形式)

<Axis
field="value"
style={{
grid: (text, index, total) => {
// text: 格式化后的文本,index: 当前索引,total: 刻度总数
if (index === total - 1) {
return { stroke: 'rgb(113, 113, 112)', strokeOpacity: 1, lineDash: null };
}
return { stroke: 'rgb(220, 220, 220)', strokeOpacity: 0.4, lineDash: null };
},
}}
/>

自动处理标签

<Axis
field="month"
labelAutoRotate={true}
labelAutoHide={true}
/>

注意:safetyDistance 默认值为 2,通常无需手动设置。

坐标轴箭头标记

<Axis
field="value"
style={{
line: {},
// symbol 数组:[最大值端, 最小值端]
// 单箭头:[{ type: 'arrow' }],双端:[{ type: 'arrow' }, { type: 'circle' }]
symbol: [{ type: 'arrow' }],
}}
/>

旋转标签

旋转标签用于解决标签重叠问题,但会降低可读性。

⚠️ 使用优先级

1. labelAutoRotate(推荐)
2. 旋转 45°
3. 旋转 90°(谨慎)

自动旋转

<Axis field="month" labelAutoRotate={true} />

手动旋转 45°

<Axis
field="month"
style={{
label: { transform: 'rotate(-45deg)', align: 'end', textBaseline: 'middle' },
}}
/>

手动旋转 90°

<Axis
field="year"
style={{
label: { transform: 'rotate(-90deg)', align: 'end', textBaseline: 'middle' },
}}
/>