上节课我们说接口给了3组”销量数据”, 这节课我们通过”切换按钮”, 来给饼图切换数据.
333.gif

添加”切换按钮”

使用ant ui中的radio组件.

  1. <a-radio-group v-model:value="type">
  2. <a-radio-button value="salesTypeData">全部</a-radio-button>
  3. <a-radio-button value="salesTypeDataOnline">线上</a-radio-button>
  4. <a-radio-button value="salesTypeDataOffline">线下</a-radio-button>
  5. </a-radio-group>
  6. <div class="chart-sales-category" ref="salesCategoryChartRef"></div>

同时定义一个”type”变量, 用来表示当前选中的值:

  1. // SalesCategroy.vue
  2. setup() {
  3. const salesCategoryChartRef = ref<HTMLElement>();
  4. // 标注type的类型
  5. const type = ref<
  6. "salesTypeData" | "salesTypeDataOnline" | "salesTypeDataOffline"
  7. >("salesTypeData");
  8. // ... 省略部分代码
  9. return { salesCategoryChartRef,type };
  10. },

切换数据

3组数据, 接口以“对象”的形式传递给前端, 获取到数据后, 可以通过”type“变量的值来切换不同类型的”销售数据”, 同时使用”watch“监控每次”切换”动作.

    watch(
      type,
      (type) => {
        // 切换不同类型的数据
        const data = response.data[type].map(({ x, y }) => {
          return { name: x, value: y };
        });

        myChart.setOption({
          tooltip: {},

          series: [
            {
              type: "pie",
              // 圆环的内径和外径比例
              radius: ["40%", "70%"],
              data,
            },
          ],
        });
      },
      { immediate: true }
    );

我们把echart渲染的代码移动到了watch中, 为了打开页面也会渲染饼图, 设置watch的参数”immediate”为”true”.

完整代码

<script lang="ts">
import {
  defineComponent,
  ref,
  onMounted,
  watch,
  nextTick,
  onBeforeUnmount,
} from "vue";
import * as echarts from "echarts";
import type { ECharts } from "echarts";

import { http } from "@/http";
export default defineComponent({
  name: "SalesCategroy",

  setup() {
    const salesCategoryChartRef = ref<HTMLElement>();
    const type = ref<
      "salesTypeData" | "salesTypeDataOnline" | "salesTypeDataOffline"
    >("salesTypeData");
    const isLoading = ref(true);
    let chartPie: ECharts;

    onMounted(async () => {
      isLoading.value = true;
      // 请求接口
      const response = await http.get<{
        salesTypeData: { x: string; y: number }[];
        salesTypeDataOnline: { x: string; y: number }[];
        salesTypeDataOffline: { x: string; y: number }[];
      }>("/sales-category");
      isLoading.value = false;

      // 基于准备好的dom,初始化echarts实例
      await nextTick();
      chartPie = echarts.init(salesCategoryChartRef.value as HTMLElement);
      // 绘制图表
      watch(
        type,
        (type) => {
          // 切换不同类型的数据
          const data = response.data[type].map(({ x, y }) => {
            return { name: x, value: y };
          });

          chartPie.setOption({
            grid: { containLabel: true, left: 0, right: 0, bottom: 16 },

            tooltip: {},

            series: [
              {
                type: "pie",
                // 圆环的内径和外径比例
                radius: ["40%", "70%"],
                data,
              },
            ],
          });
        },
        { immediate: true }
      );
    });

    function _resize() {
      chartPie.resize();
    }

    window.addEventListener("resize", _resize);

    onBeforeUnmount(() => {
      window.removeEventListener("resize", _resize);
      chartPie.dispose();
    });

    return { salesCategoryChartRef, type, isLoading };
  },
});
</script>

<template>
  <a-card size="small" :loading="isLoading" title="品类销量占比">
    <a-radio-group v-model:value="type">
      <a-radio-button value="salesTypeData">全部</a-radio-button>
      <a-radio-button value="salesTypeDataOnline">线上</a-radio-button>
      <a-radio-button value="salesTypeDataOffline">线下</a-radio-button>
    </a-radio-group>
    <div
      v-if="!isLoading"
      class="chart-sales-category"
      ref="salesCategoryChartRef"
    ></div
  ></a-card>
</template>

<style lang="scss">
.chart-sales-category {
  width: 100%;
  height: 300px;
}
</style>