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"就可以达到同样的效果,且少一次创建图表的过程,性能更高一点。

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

相关推荐
云水一下6 小时前
从零开始!VMware安装Fedora Workstation 44桌面系统完整教程
前端
小码哥_常7 小时前
安卓黑科技:实现多平台商品详情页一键跳转APP
前端
killerbasd7 小时前
还是迷茫 5.3
前端·react.js·前端框架
不会敲代码18 小时前
TCP/IP 与前端性能:从数据包到首次渲染的底层逻辑
前端·tcp/ip
kyriewen8 小时前
奥特曼借GPT-5.5干杯,而你的Copilot正按Token收钱
前端·github·openai
AC赳赳老秦8 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
kyriewen8 小时前
代码写成一锅粥?3个设计模式让你的项目“起死回生”
前端·javascript·设计模式
不会敲代码19 小时前
从零搭建 AI 日记助手:用 Milvus 向量数据库实现语义搜索
javascript·openai
千寻girling9 小时前
《 Git 详细教程 》
前端·后端·面试
threelab10 小时前
Three.js UV 图像变换效果 | 三维可视化 / AI 提示词
javascript·人工智能·uv