VisitAnalysis.vue 5.32 KB
<template>
  <span style="margin-left: 50px; font-size: 18px">选择年份:</span>
  <a-select
    v-model:value="yearValue"
    show-search
    placeholder="请选择"
    style="width: 200px"
    :options="yearOptions"
    :filter-option="filterOption"
    @change="handleChangeYear"
  />
  <div style="position: fixed; top: 80px; right: 70px">
    <a-radio-group
      v-model:value="periodCopy"
      class="custom-radio-group"
      :style="{ marginLeft: '100px' }"
    >
      <a-radio-button value="月" @click="handlePeriodChange('月')">月</a-radio-button>
      <a-radio-button value="年" @click="handlePeriodChange('年')">年</a-radio-button>
    </a-radio-group>
  </div>
  <div ref="chartRef" :style="{ height: '500px', width }"></div>
</template>

<script lang="ts" setup>
  import { ref, Ref, watchEffect } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { basicProps } from './props';
  import { max } from 'lodash-es';
  import { useDataStoreWithOut } from '/@/store/modules/data';
  import { getChartData } from '/@/api/project/global';
  import { dateTime, period } from '/@/store/modules/user';
  import { useMessage } from '/@/hooks/web/useMessage';

  defineProps({
    ...basicProps,
  });
  const yearValue = ref();
  const yearOptions = [
    // {
    //   label: '2025',
    //   value: 2025,
    // },
    {
      label: '2024',
      value: 2024,
    },
    {
      label: '2023',
      value: 2023,
    },
  ];
  const periodCopy = ref();
  const { createMessage } = useMessage(); //错误提示组件
  const { error } = createMessage;
  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  const filterOption = (input: string, option: any) => {
    return option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0;
  };
  function handlePeriodChange(value) {
    //没选年份报错
    if (value == '月') {
      if (dateTime.value == undefined) {
        error('请选择年份');
        return;
      }
    } else if (value == '年') {
      yearValue.value = null;
    }
    period.value = value;
  }
  function handleChangeYear(value) {
    if (period.value == null) {
      period.value = '月';
      periodCopy.value = '月';
    }
    yearValue.value = value;
    dateTime.value = yearValue.value;
  }
  const dataStore = useDataStoreWithOut();
  //监控选择器变化
  watchEffect(async () => {
    // const data = dataStore?.getChartData || {};
    const chartData = await getChartData({
      dateTime: dateTime.value,
      period: period.value,
    });
    const x = [],
      y = [];
    for (const key in chartData) {
      x.push(chartData[key].date);
      y.push(chartData[key].count);
    }
    const data = { x, y };
    const maxY = data?.y ? max(data?.y) : 0;
    setOptions({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
      },
      xAxis: {
        type: 'category',
        data: data?.x,
        splitLine: {
          show: true,
          lineStyle: {
            width: 1,
            type: 'solid',
            color: 'rgba(226,226,226,0.5)',
          },
        },
        axisTick: {
          show: false,
        },
      },
      yAxis: [
        {
          type: 'value',
          max: maxY + 20,
          splitNumber: 20,
          axisTick: {
            show: false,
          },
          splitArea: {
            show: true,
            areaStyle: {
              color: ['rgba(255,255,255,0.2)', 'rgba(226,226,226,0.2)'],
            },
          },
        },
      ],
      grid: { left: '1%', right: '1%', top: '2  %', bottom: 0, containLabel: true },
      series: [
        {
          smooth: true,
          data: data?.y,
          type: 'line',
          areaStyle: {},
          itemStyle: {
            color: '#5ab1ef',
          },
        },
        // {
        //   smooth: true,
        //   data: [
        //     33, 66, 88, 333, 3333, 5000, 18000, 3000, 1200, 13000, 22000, 11000, 2221, 1201, 390,
        //     198, 60, 30, 22, 11,
        //   ],
        //   type: 'line',
        //   areaStyle: {},
        //   itemStyle: {
        //     color: '#019680',
        //   },
        // },
      ],
    });
  });
</script>
<style scoped>
  .custom-radio-group .ant-radio-button-wrapper {
    border-color: #1684fc; /* 未选中按钮的边框颜色 */
    background-color: white; /* 未选中按钮的背景颜色为白色 */
    color: #1684fc; /* 未选中按钮的文字颜色为 #1684fc */
  }

  .custom-radio-group .ant-radio-button-wrapper-checked {
    border-color: #1684fc; /* 选中按钮的边框颜色 */
    background-color: #1684fc; /* 选中按钮的背景颜色为 #1684fc */
    color: white; /* 选中按钮的文字颜色为白色 */
  }

  .custom-radio-group .ant-radio-button-wrapper-checked:hover {
    background-color: #1684fc; /* 悬停在选中按钮时保持相同的背景颜色 */
    color: white; /* 悬停在选中按钮时保持文字为白色 */
  }

  .custom-radio-group .ant-radio-button-wrapper:hover {
    border-color: #1684fc; /* 悬停在未选中按钮时的边框颜色 */
  }

  .custom-radio-group .ant-radio-button-wrapper:not(.ant-radio-button-wrapper-checked):hover {
    color: #1684fc; /* 悬停在未选中按钮时文字颜色为 #1684fc */
  }
</style>