当前位置: 首页 > news >正文

AntV F2入门教程

以下教程将系统地介绍 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';

二、Canvas 与 Chart

2.1 <Canvas>

  • Props

    • context:必选,传入小程序或浏览器 Canvas 的绘图上下文
    • pixelRatio:设备像素比,默认为 window.devicePixelRatio
    • width/height:画布尺寸,可选
<Canvas context={ctx} pixelRatio={2} width={375} height={300}>{/* Chart 放在 Canvas 内 */}
</Canvas>

([f2.antv.antgroup.com][1])

2.2 <Chart>

  • 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 },
];<Canvas context={ctx}><Chartdata={data}scale={{ value: { min: 0, max: 100 } }}coord={{ type: 'rect', transposed: false }}padding={[10, 20, 50, 20]}animate={true}>{/* Geoms、Axis、Legend 等放在此 */}</Chart>
</Canvas>

([f2.antv.antgroup.com][1])


三、几何标记(Geometry)

所有几何标记均继承通用属性:

  • x: stringy: string:映射字段
  • color:颜色映射,可为固定值、字段名、数组或对象形式
  • size:大小映射,用法同 color
  • adjust?: 'stack' | 'dodge' | 'symmetric':调整方式
  • viewClip?: boolean:只显示图表区域内
  • animation?: AnimationOption:分阶段动画

([f2.antv.antgroup.com][2])

3.1 折线图 <Line>

<Linex="type"y="value"color="#1890FF"size={2}style={{ lineDash: [4, 2] }}animation={{appear: { duration: 500, easing: 'easeCubicIn' },}}
/>

3.2 柱状图 <Interval>

<Intervalx="type"y="value"color={['type', ['#5B8FF9', '#61DDAA', '#65789B']]}adjust="dodge"
/>

3.3 散点图 <Point>

<Pointx="type"y="value"color="type"size={[ 'value', [4, 10] ]}
/>

3.4 面积图 <Area>

<Areax="type"y="value"color="#FF4D4F"adjust="stack"style={{ opacity: 0.6 }}
/>

3.5 K 线图 <Candlestick>

<Candlestickx="date"y="value"color={{ field: 'trend', range: ['#0f0', '#f00'] }}
/>

([f2.antv.antgroup.com][2])


四、坐标轴(Axis)

  • 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 }
<Axisfield="type"position="bottom"tickCount={data.length}formatter={val => `类型:${val}`}style={{label: { fill: '#666', fontSize: 10 },tickLine: { length: 4 },grid: { stroke: '#eee' },}}
/>
<Axisfield="value"position="left"formatter={val => `${val} 单位`}nice={true}
/>

([f2.antv.antgroup.com][3])


五、图例(Legend)

  • Props

    • position?: 'top'|'right'|'bottom'|'left'
    • itemFormatter?: (item) => string
    • marker?: 'circle'|'square'|'line'
    • clickable?: boolean
    • onClick?: (item) => void
    • style?, itemStyle?(Flex 布局)
    • nameStyle?, valueStyle?(TextAttr)
<Legendposition="top"marker="square"itemFormatter={name => name.toUpperCase()}onClick={item => console.log('点击图例', item)}style={{ flexDirection: 'row', justifyContent: 'space-around' }}
/>

([f2.antv.antgroup.com][4])


六、提示(Tooltip)

  • Props

    • triggerOn?: 'press'|'click'
    • triggerOff?: 'pressend'
    • alwaysShow?: boolean
    • showCrosshairs?: boolean
    • crosshairsType?: 'x'|'y'|'xy'
    • nameStyle?: TextAttrvalueStyle?: TextAttrbackground?: RectAttr
    • xTip?: string|((x) => string)xTipTextStyle?xTipBackground?
    • showItemMarker?: boolean
    • onChange?: (items) => void
  • Methods(通过 ref 调用)

    • show({ x, y })
    • hide()
<TooltiptriggerOn="press"showCrosshairs={true}crosshairsType="xy"nameStyle={{ fontSize: 12, fill: '#333' }}valueStyle={{ fontSize: 12 }}
/>

([f2.antv.antgroup.com][5])


七、标注(Guide)

内置 <PointGuide>, <TextGuide>, <TagGuide>, <ImageGuide>, <LineGuide>,用于在图表上添加注解。

7.1 文本标注 <TextGuide>

  • Props

    • records: Array<{ [field]: value } \| 'min'|'max'|…>
    • content: string
    • attrs?: ShapeAttr
    • offsetX?: numberoffsetY?: number
{data.map(item => (<TextGuiderecords={[item]}content={`${item.value}`}attrs={{ fill: '#000', fontSize: '14px' }}offsetY={-10}/>
))}

([f2.antv.antgroup.com][6])

7.2 其他标注示例

import { PointGuide, TagGuide, LineGuide, ImageGuide } from '@antv/f2';// 点标注
<PointGuide records={[data[0]]} style={{ fill: '#f00' }} />// 标签标注
<TagGuiderecords={[{ type: 'A', value: 30 }]}content="重点"direct="tr"background={{ fill: '#fff' }}
/>// 线标注
<LineGuide records={[{ type: 'min', value: 30 }]} />// 图片标注
<ImageGuiderecords={[{ type: 'C', value: 45 }]}src="https://example.com/icon.png"
/>

八、滚动条(ScrollBar)

  • Props

    • mode?: 'x'|'y'|['x','y']
    • range?: [0,1]
    • pan?: booleanpinch?: booleanautoFit?: boolean
    • visible?: booleanposition?: 'top'|'right'|'bottom'|'left'
    • style?: ShapePropsbackground?: ShapePropsbarStyle?: ShapeProps
<ScrollBarmode="x"range={[0, 0.5]}pan={true}pinch={true}position="bottom"style={{ margin: ['8px', 0] }}
/>

([f2.antv.antgroup.com][7])


九、小结

  1. Canvas & Chart:搭建画布并实例化图表,配置数据、度量、坐标系和动画。
  2. Geometry:常用折线、柱状、散点、面积、K 线等标记,支持丰富的数据映射和调整方式。
  3. 组件<Axis><Legend><Tooltip><Guide><ScrollBar> 等,灵活配置样式和交互。
  4. 扩展:通过 ref 调用组件实例方法(如 Tooltip 的 show/hide)、以及生命周期 API(changeDatarender 等)实现动态更新。

相关文章:

  • OpenCV CUDA模块设备层---- 绝对值函数abs()
  • HarmonyOS 5 原子化服务卡片测试全攻略
  • 探究webView与html的通讯
  • LLaVA-Med常见问题解决方案
  • 论文笔记 <交通灯><多智能体>MetaLight:基于价值的元强化学习用于交通信号控制
  • day13-软件包管理
  • 22.流程控制函数
  • python校园服务交流系统
  • VUE3(一)、基础语法
  • iOS开发中的安全实践:如何通过Ipa混淆与加固确保应用安全
  • 特种设备安全管理:使用单位的 “责任清单”
  • 贝叶斯定理:AI大模型的概率统计基石
  • 青少年编程与数学 01-011 系统软件简介 27 备份恢复工具
  • 新零售系统商城开发全解析
  • 爱普特APT32F1104C8T6单片机 高抗干扰+硬件加密双保障
  • TensorFlow深度学习实战——Transformer变体模型
  • C++11中alignof和alignas的入门到精通指南
  • C++ 标准模板库各个容器的应用场景分析
  • 查询pgsql表字段 包含 name 的表
  • 基于Rust的Polars学习笔记
  • 小白怎么做网页/上海建站seo
  • 湖州品牌网站设计/杭州seo服务公司
  • 同程网 网站模板/互联网营销师考试题库
  • 我想注册网站怎么做/网络推广合作协议范本
  • 杭州建设局官网/谷歌seo 外贸建站
  • 广东网站备案查询系统/百度首页 百度