F2图表在Vue3中的使用方法

如何在 Vue 中使用 | F2 移动端可视化引擎,这个是官网上面说的配置方法。

F2/packages/f2-vue/examples/vue3/src/App.vue at master · antvis/F2 · GitHub这个是官方提供的vue3的例子。

我没有使用render的方式去创建图表,直接将F2组件写在了template中,所以jsx也就不需要配置了。在这里梳理一下使用步骤:

  1. 安装依赖
bash 复制代码
npm install @antv/f2 --save
npm install @antv/f-vue --save
  1. 页面代码
javascript 复制代码
<template>
  <div className="container">
    <!-- 总资产折线图 -->
    <Canvas
      :pixel-ratio="2"
      :style="{ width: '100%', height: '300px' }"
      :key="can1key"
    >
      <Chart :data="allTotalData" :scale="scaleConfig">
        <Axis field="date" />
        <Axis field="allTotal" />
        <Line x="date" y="allTotal" />
      </Chart>
    </Canvas>

    <!-- 收益折线图 -->
    <Canvas
      :pixel-ratio="2"
      :style="{ width: '100%', height: '300px', marginTop: '20px' }"
      :key="can2key"
    >
      <Chart :data="earningsData" :scale="scaleConfig">
        <Axis field="date" />
        <Axis field="allEarning" />
        <Line x="date" y="allEarning" />
      </Chart>
    </Canvas>
  </div>
</template>

<script lang="ts" setup>
import Canvas from '@antv/f-vue';
import { Chart, Axis, Line } from '@antv/f2';
import { ref } from 'vue';
import { getStatisticList } from './api';
import { getPositionList } from '../money/api';
import { Statistic } from '../money/type';

const allTotalData = ref<{ date: string; allTotal: number }[]>([]);
const earningsData = ref<{ date: string; allEarning: number }[]>([]);
const positionList = ref([]);
const can1key = ref('can1' + 0); // canvas的key,动态变化用于图表数据刷新
const can2key = ref('can2' + 0);

// 可选:配置坐标轴刻度(避免日期重叠)
const scaleConfig = {
  date: {
    range: [0, 1],
    tickCount: 5,
  },
};

async function init() {
  const data: Statistic[] = await getStatisticList({ type: 1 });
  allTotalData.value = data.map((i) => ({
    date: i.createTime.substring(5, 10),
    allTotal: i.allTotal,
  }));
  earningsData.value = data.map((i) => ({
    date: i.createTime.substring(5, 10),
    allEarning: i.allEarnings,
  }));
  can1key.value += 1; // 在远程数据返回后,修改canvas的key
  can2key.value += 1;

  positionList.value = await getPositionList({ type: 1 });
}
init();
</script>

<style scoped></style>

这是一个很简单的页面,页面中有两个基础折线图。

到此这个页面的使用方法就讲完了,看着还挺简单的。不过,这里面有个大坑,就是正常我们请求数据然后修改响应式数据,都能让表格刷新,但是这里不行,这个问题废了我很长时间,最后找到一个方法,就是给canvas组件加key,然后数据返回后,更新key,这样可以强制组件销毁并重建。

不过就在我查阅资料时,这个问题我有了自己的解决办法了,这个问题本质上是canvas不能对data的变化进行响应(源码暂未研究),那么我在初始化的时候先不创建图表,等数据返回后再创建,用v-if="data.length"就可以达到同样的效果,且少一次创建图表的过程,性能更高一点。

遇到问题记录一下,养成总结记录的好习惯。

相关推荐
Aphasia31112 分钟前
https连接传输流程
前端·面试
徐小夕12 分钟前
万字长文!千万级文档 RAG 知识库系统落地实践
前端·算法·github
梦梦代码精20 分钟前
2026年PHP开源商城系统实测对比:架构、多商户、商用授权,谁才是真·省心?
vue.js·docker·架构·开源·代码规范
threelab23 分钟前
Three.js 物理模拟着色器 | 三维可视化 / AI 提示词
开发语言·前端·javascript·人工智能·3d·着色器
武器大师7225 分钟前
lv_binding_js 代码解读
开发语言·javascript·ecmascript
kyriewen33 分钟前
CSS Container Queries:彻底告别 @media 写到手软,附 5 个真实布局案例
前端·css·面试
Patrick_Wilson2 小时前
router.replace 之后紧跟 reload,页面为什么无限刷新?
javascript·react.js·浏览器
小小小小宇2 小时前
OpenMemory MCP
前端
和平宇宙3 小时前
AI笔记005. hermes-DeepSeek V4 Pro, 128K上下文引发的探索
前端·人工智能·笔记
IT_陈寒3 小时前
Redis持久化这个坑,我爬了一整天才出来
前端·人工智能·后端