CustomerTrend.vue 3.31 KB
<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts" setup>
  import { ref, Ref, onMounted, watch } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { getTrendData } from '/@/api/dashboard';
  import { getDateByShortcutQueryKey, ShortcutQueryKeyEnum } from '../hooks/useDate';
  import { useI18n } from '/@/hooks/web/useI18n';

  interface Props {
    width?: string;
    height?: string;
    customerTrendList?: Array<[number, string]>;
  }
  const props = withDefaults(defineProps<Props>(), {
    width: '100%',
    height: '320px',
    customerTrendList: () => [],
  });

  const { t } = useI18n();
  watch(
    () => props.customerTrendList,
    (newValue) => {
      setOptions({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            lineStyle: {
              width: 1,
              color: '#019680',
            },
          },
        },
        xAxis: {
          type: 'category',
          splitLine: {
            show: true,
            lineStyle: {
              width: 1,
              type: 'solid',
              color: 'rgba(226,226,226,0.5)',
            },
          },
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          splitArea: {
            show: true,
            areaStyle: {
              color: ['rgba(255,255,255,0.2)', 'rgba(226,226,226,0.2)'],
            },
          },
        },
        grid: {
          left: '1%',
          right: '2%',
          bottom: 0,
          containLabel: true,
        },
        series: [
          {
            smooth: true,
            name: t('home.index.currentTrend'),
            type: 'line',
            data: newValue,
            areaStyle: {},
            itemStyle: {
              color: '#5ab1ef',
            },
          },
        ],
        animationDuration: 1500,
      });
    }
  );

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  onMounted(async () => {
    const res = await getTrendData({
      ...getDateByShortcutQueryKey(ShortcutQueryKeyEnum.LATEST_1_MONTH),
      trend: 'CUSTOMER_TREND',
    });

    const transferResult = res.map((item) => [item.date, item.value]);
    setOptions({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
      },
      xAxis: {
        type: 'category',
        splitLine: {
          show: true,
          lineStyle: {
            width: 1,
            type: 'solid',
            color: 'rgba(226,226,226,0.5)',
          },
        },
        axisTick: {
          show: false,
        },
      },

      yAxis: {
        splitArea: {
          show: true,
          areaStyle: {
            color: ['rgba(255,255,255,0.2)', 'rgba(226,226,226,0.2)'],
          },
        },
      },
      grid: {
        left: '1%',
        right: '2%',
        bottom: 0,
        containLabel: true,
      },
      series: [
        {
          smooth: true,
          name: t('home.index.currentTrend'),
          type: 'line',
          data: transferResult,
          areaStyle: {},
          itemStyle: {
            color: '#5ab1ef',
          },
        },
      ],
      animationDuration: 1500,
    });
  });
</script>