@tarojsx/library
Version:
Taro3 library
105 lines (90 loc) • 3.9 kB
text/mdx
---
id: antv-f2
title: AntV F2
sidebar_label: AntV F2
---
## [AntV F2](https://antv.gitee.io/zh)
### 需求
- 小程序基础库 >= 2.7.0
### 使用
```jsx title="默认全量导入 F2"
import React from 'react'
import { F2 } from '@tarojsx/library/dist/antv'
const Demo = () => {
return (
<F2
style={{ width: '100vw', height: '50vh' }}
// F2 对数据源格式的要求,仅仅是 JSON 数组,数组的每个元素是一个标准 JSON 对象。
data={[
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
]}
>
{
// Step 1: 接收 Chart 对象
({ chart, data }) => {
// Step 2: 载入数据源
chart.source(data)
// Step 3:创建图形语法,绘制柱状图,由 genre 和 sold 两个属性决定图形位置,genre 映射至 x 轴,sold 映射至 y 轴
chart.interval().position('genre*sold').color('genre')
// Step 4: 渲染图表
chart.render()
}
}
</F2>
)
}
```
```jsx title="按需引用 F2"
import React from 'react'
import { Chart, Global } from '@antv/f2/es/core'
import { F2Core } from '@tarojsx/library/dist/antv/F2Core'
require('@antv/f2/es/coord/polar')
require('@antv/f2/es/component/guide/text')
Chart.plugins.register(require('@antv/f2/es/plugin/guide'))
const Demo = () => {
return (
<F2Core style={{ width: '100vw', height: '50vh' }} config={{ Chart, Global }} data={[]}>
{({ chart, data }) => {
chart.source(data)
chart.render()
}}
</F2Core>
)
}
```
import { F2 } from '@tarojsx/library/dist/antv'
import { UI } from '@/ui'
<UI phone title="AntV F2">
{() => {
return (
<F2
style={{ width: '750px', height: '750px' }}
data={[
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
]}
>
{({ chart, data }) => {
chart.source(data)
chart.interval().position('genre*sold').color('genre')
chart.render()
}}
</F2>
)
}}
</UI>
| 参数 | 说明 | 类型 | 默认值 |
| ----------- | ----------------------------------------------------------------------------------------- | ------------------------- | ------ |
| className? | | string | |
| style? | | {} | |
| config? | 图表配置, [参考 Chart](https://f2.antv.vision/zh/docs/api/chart/chart#%E5%8F%82%E6%95%B0) | `{}` | |
| data | 图表数据 | `[]` | |
| fontFamily? | 字体 | `string` | |
| children | 图表渲染逻辑 | `({chart, data}) => void` | |