vue中使用antv/f2 圖表

圖表.png

1. 安裝包

npm install @antv/f2

2. 組件實現(xiàn)

說明:必須在onMounted生命周期中渲染數(shù)據(jù)

<!-- 組件模板 -->
<template>
  <canvas id="line"></canvas>
</template>

<script>
import { onMounted } from "vue";
import {} from "vant";
import F2 from "@antv/f2";

const data = [
  {
    time: "2016-08-08 00:00:00",
    tem: 10,
  },
  {
    time: "2016-08-08 00:10:00",
    tem: 22,
  },
  {
    time: "2016-08-08 00:30:00",
    tem: 20,
  },
  {
    time: "2016-08-09 00:35:00",
    tem: 26,
  },
  {
    time: "2016-08-09 01:00:00",
    tem: 20,
  },
  {
    time: "2016-08-09 01:20:00",
    tem: 26,
  },
  {
    time: "2016-08-10 01:40:00",
    tem: 28,
  },
  {
    time: "2016-08-10 02:00:00",
    tem: 20,
  },
  {
    time: "2016-08-10 02:20:00",
    tem: 18,
  },
];

export default {
  props: {},
  setup() {
    onMounted(() => {
      const chart = new F2.Chart({
        id: "line",
        pixelRatio: window.devicePixelRatio,
      });

      const defs = {
        time: {
          type: "timeCat",
          mask: "MM/DD",
          tickCount: 3,
          range: [0, 1],
        },
        tem: {
          tickCount: 5,
          min: 0,
          alias: "日均溫度",
        },
      };
      chart.source(data, defs);
      chart.axis("time", {
        label: function label(text, index, total) {
          const textCfg = {};
          if (index === 0) {
            textCfg.textAlign = "left";
          } else if (index === total - 1) {
            textCfg.textAlign = "right";
          }
          return textCfg;
        },
      });
      chart.tooltip({
        showCrosshairs: true,
      });
      chart.line().position("time*tem").shape("smooth");
      chart.point().position("time*tem").shape("smooth").style({
        stroke: "#fff",
        lineWidth: 1,
      });
      chart.render();
    });
    return {};
  },
  methods: {},
};
</script>

<style lang="scss" scoped>
#line{
    height: 375px;
    width: 375px;
}
</style>

3. 組件使用

<template>
  <van-nav-bar title="測試頁面" fixed placeholder safe-area-inset-top></van-nav-bar>
  <my-line />
</template>

<script>
import { NavBar } from "vant";
import MyLine from "../../components/example/MyLine.vue";

export default {
  components: {
    MyLine,
    [NavBar.name]: NavBar,
  },
  setup() {
    return {};
  },
  data() {
    return {};
  },

  methods: {},
};
</script>

<style lang="scss" scoped></style>

補充說明: 實例參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容