以下教程将系统地介绍 AntV F2(移动端可视化引擎)的核心 组件 API,包含安装与引入、画布与图表、数据映射、几何标记、坐标轴、图例、提示、标注和滚动条等,每个 API 都附带完整示例代码,帮助你快速掌握 F2 用法。
# 安装 F2 主包
npm install @antv/f2 --save
# 或者使用 yarn
yarn add @antv/f2
// 在小程序或浏览器中引入
import { Canvas, Chart, Line, Interval, Point, Area, Candlestick, Axis, Legend, Tooltip, Guide, ScrollBar } from '@antv/f2';
Props
context
:必选,传入小程序或浏览器 Canvas 的绘图上下文pixelRatio
:设备像素比,默认为 window.devicePixelRatio
width
/height
:画布尺寸,可选
([f2.antv.antgroup.com][1])
Props
data: any[]
:数据源scale?: ScaleOption
:度量配置coord?: CoordOption
:坐标系配置padding?: number \| [top, right, bottom, left]
:内边距animate?: boolean
:是否开启动画const data = [
{ type: 'A', value: 30 },
{ type: 'B', value: 80 },
{ type: 'C', value: 45 },
];
([f2.antv.antgroup.com][1])
所有几何标记均继承通用属性:
x: string
、y: string
:映射字段color
:颜色映射,可为固定值、字段名、数组或对象形式size
:大小映射,用法同 color
adjust?: 'stack' | 'dodge' | 'symmetric'
:调整方式viewClip?: boolean
:只显示图表区域内animation?: AnimationOption
:分阶段动画([f2.antv.antgroup.com][2])
([f2.antv.antgroup.com][2])
Props
field: string
:数据字段position?: 'top'|'right'|'bottom'|'left'
visible?: boolean
tickCount?: number
formatter?: (val) => string
grid?: 'line'|'arc'
style?: { label?: TextAttr; line?: LineAttr; tickLine?: { length: number }; grid?: LineAttr }
`类型:${val}`}
style={{
label: { fill: '#666', fontSize: 10 },
tickLine: { length: 4 },
grid: { stroke: '#eee' },
}}
/>
`${val} 单位`}
nice={true}
/>
([f2.antv.antgroup.com][3])
Props
position?: 'top'|'right'|'bottom'|'left'
itemFormatter?: (item) => string
marker?: 'circle'|'square'|'line'
clickable?: boolean
onClick?: (item) => void
style?
, itemStyle?
(Flex 布局)nameStyle?
, valueStyle?
(TextAttr)
([f2.antv.antgroup.com][4])
Props
triggerOn?: 'press'|'click'
triggerOff?: 'pressend'
alwaysShow?: boolean
showCrosshairs?: boolean
crosshairsType?: 'x'|'y'|'xy'
nameStyle?: TextAttr
、valueStyle?: TextAttr
、background?: RectAttr
xTip?: string|((x) => string)
、xTipTextStyle?
、xTipBackground?
showItemMarker?: boolean
onChange?: (items) => void
Methods(通过 ref
调用)
show({ x, y })
hide()
([f2.antv.antgroup.com][5])
内置
,
,
,
,
,用于在图表上添加注解。
Props
records: Array<{ [field]: value } \| 'min'|'max'|…>
content: string
attrs?: ShapeAttr
offsetX?: number
、offsetY?: number
{data.map(item => (
))}
([f2.antv.antgroup.com][6])
import { PointGuide, TagGuide, LineGuide, ImageGuide } from '@antv/f2';
// 点标注
// 标签标注
// 线标注
// 图片标注
Props
mode?: 'x'|'y'|['x','y']
range?: [0,1]
pan?: boolean
、pinch?: boolean
、autoFit?: boolean
visible?: boolean
、position?: 'top'|'right'|'bottom'|'left'
style?: ShapeProps
、background?: ShapeProps
、barStyle?: ShapeProps
([f2.antv.antgroup.com][7])
、
、
、
、
等,灵活配置样式和交互。ref
调用组件实例方法(如 Tooltip 的 show
/hide
)、以及生命周期 API(changeData
、render
等)实现动态更新。