logo

F2

  • 教程
  • 组件API
  • 图表示例
  • 所有产品antv logo arrow
  • 5.0.3
  • 快速上手
  • 图表组成
  • 图形语法
  • 数据 - Data
  • 度量 - Scale
  • 坐标系 - Coordinate
  • 图形标签 - Shape
  • 绘图属性 - Style
  • 动画属性 - Animation
  • 事件属性 - Event
  • 图形使用 - JSX
  • 组件介绍 - Component
  • 多端/框架 - Framework
    • 多端适配
    • 如何在 React 中使用
    • 如何在 Vue 中使用
    • 如何在小程序中使用
    • 如何在 Node.js 中使用
    • 配置 jsx transform
    • 使用 svg 渲染
  • 进阶 - Advanced
    • 自定义 View
  • 常见问题 - Question
    • 和 React 同时使用时,TS 类型报错

如何在 Vue 中使用

上一篇
如何在 React 中使用
下一篇
如何在小程序中使用

Resources

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

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

为了方便 Vue 项目的使用,Fengine 也封装了一个 vue 的组件

Usage

1. 安装依赖

npm install @antv/f2 --save
npm install @antv/f-vue --save

2. 配置 F2 的 JSX 编译

npm install @babel/plugin-transform-react-jsx --save-dev

打开 vue.config.js 添加如下代码

{
chainWebpack: (config) => {
config.module
.rule('F2')
.test(/\.jsx$/)
.use('babel')
.loader('babel-loader')
.options({
plugins: [
[
'@babel/plugin-transform-react-jsx',
{
runtime: 'automatic',
importSource: '@antv/f2',
},
],
],
})
.end();
},
}

3. Vite 中配置

npm install @rollup/plugin-babel --save-dev
npm install @babel/plugin-transform-react-jsx --save-dev

打开 vite.config.js 添加如下配置

import vue from '@vitejs/plugin-vue';
import vueJsx from '@vitejs/plugin-vue-jsx';
import { babel } from '@rollup/plugin-babel';
export default defineConfig({
plugins: [
babel({
plugins: [
[
'@babel/plugin-transform-react-jsx',
{
runtime: 'automatic',
importSource: '@antv/f2',
},
],
],
}),
vue(),
vueJsx(),
],
});

4. 使用示例

<script>
import { toRaw } from 'vue';
import Canvas from '@antv/f-vue';
import { Chart, Interval, Axis } from '@antv/f2';
const data1 = [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 120 },
{ genre: 'Shooter', sold: 350 },
{ genre: 'Other', sold: 150 },
];
const data2 = [
{ genre: 'Sports', sold: 275 },
{ genre: 'Strategy', sold: 115 },
{ genre: 'Action', sold: 20 },
{ genre: 'Shooter', sold: 50 },
{ genre: 'Other', sold: 50 },
];
export default {
name: 'App',
data() {
return {
year: '2021',
chartData: data1,
};
},
mounted() {
setTimeout(() => {
this.year = '2022';
this.chartData = data2;
}, 1000);
},
render() {
const { year, chartData } = this;
return (
<div className="container">
<Canvas pixelRatio={window.devicePixelRatio}>
<Chart data={toRaw(chartData)}>
<Axis field="genre" />
<Axis field="sold" />
<Interval x="genre" y="sold" color="genre" />
</Chart>
</Canvas>
</div>
);
},
};
</script>
<style>
.container {
width: 500px;
height: 300px;
}
</style>

完整示例可参考

  • codesandbox: https://codesandbox.io/s/f-vue-wlwtkb?file=/src/App.vue
  • https://github.com/antvis/FEngine/tree/master/packages/f-vue/examples