index.vue 7.05 KB
<script lang="ts" setup>
  import { EChartsOption, ECharts, init } from 'echarts';
  import { onMounted } from 'vue';
  import { unref } from 'vue';
  import { ref } from 'vue';
  import { ComponentPropsConfigType } from '../../../index.type';
  import { option } from './config';
  import { UpdateTime } from '/@/views/visual/commonComponents/UpdateTime';
  import { useIntervalFn } from '@vueuse/core';
  import { computed } from 'vue';
  import { useComponentScale } from '../../../hook/useComponentScale';
  import { nextTick } from 'vue';
  import { DeviceName } from '/@/views/visual/commonComponents/DeviceName';
  import { useDataFetch } from '../../../hook/socket/useSocket';
  import { DataFetchUpdateFn } from '../../../hook/socket/useSocket.type';

  const props = defineProps<{
    config: ComponentPropsConfigType<typeof option>;
  }>();

  const chartRefEl = ref<Nullable<HTMLDivElement>>(null);

  const chartInstance = ref<Nullable<ECharts>>(null);

  const time = ref<Nullable<number>>(null);

  const getDesign = computed(() => {
    const { option, persetOption } = props.config;
    const { componentInfo, attribute, attributeRename, attributeName } = option;
    const {
      fontColor: presetFontColor,
      unit: presetUnit,
      showTime: persetShowTime,
      maxNumber: persetMaxNumber,
      valueSize: persetValueSize,
      fontSize: persetFontSize,
    } = persetOption || {};
    const { unit, fontColor, showTime, maxNumber, valueSize, fontSize } = componentInfo || {};
    return {
      unit: unit ?? presetUnit,
      fontColor: fontColor ?? presetFontColor,
      attribute: attributeRename || attributeName || attribute,
      showTime: persetShowTime || showTime,
      maxNumber: maxNumber || persetMaxNumber,
      valueSize: valueSize || persetValueSize || 14,
      fontSize: fontSize || persetFontSize || 14,
    };
  });

  const options = (): EChartsOption => {
    const { unit, fontColor, maxNumber, valueSize } = unref(getDesign);
    return {
      series: [
        {
          type: 'gauge',
          min: 0,
          max: maxNumber,
          radius: '50%',
          center: ['50%', '60%'],
          startAngle: 200,
          endAngle: -20,
          splitNumber: 10,
          itemStyle: {
            color: fontColor,
          },
          progress: {
            show: true,
            width: unref(getRatio) ? 24 * unref(getRatio) : 24,
          },
          pointer: {
            show: false,
          },
          axisLine: {
            lineStyle: {
              width: unref(getRatio) ? 24 * unref(getRatio) : 24,
            },
          },
          axisTick: {
            distance: unref(getRatio) ? -35 * unref(getRatio) : -35,
            length: unref(getRatio) ? 8 * unref(getRatio) : 8,
            splitNumber: 5,
            lineStyle: {
              width: 2,
              color: '#999',
            },
          },
          splitLine: {
            distance: unref(getRatio) ? -40 * unref(getRatio) : -40,
            length: unref(getRatio) ? 12 * unref(getRatio) : 12,
            lineStyle: {
              width: 3,
              color: '#999',
            },
          },
          axisLabel: {
            distance: 0,
            color: '#999',
          },
          anchor: {
            show: false,
          },
          title: {
            show: false,
          },
          detail: {
            valueAnimation: true,
            width: '60%',
            lineHeight: 10,
            borderRadius: 8,
            offsetCenter: [0, '40%'],
            fontSize: unref(getRatio) ? valueSize * unref(getRatio) : valueSize,
            fontWeight: 'bolder',
            formatter: `{value} ${unit ?? ''}`,
            color: fontColor || 'inherit',
          },
          data: [
            {
              value: 20,
            },
          ],
        },
        {
          type: 'gauge',
          radius: '50%',
          center: ['50%', '60%'],
          startAngle: 200,
          endAngle: -20,
          min: 0,
          max: maxNumber,
          itemStyle: {
            color: fontColor,
          },
          progress: {
            show: true,
            width: 8,
          },
          pointer: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisTick: {
            show: false,
          },
          splitLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          detail: {
            show: false,
          },
          data: [
            {
              value: 20,
            },
          ],
        },
      ],
    };
  };

  const updateChart = (value: number) => {
    unref(chartInstance)?.setOption({
      series: [{ data: [{ value: value.toFixed(2) }] }, { data: [{ value: value.toFixed(2) }] }],
    } as EChartsOption);
  };

  const initial = () => {
    chartInstance.value = init(unref(chartRefEl)!);
    chartInstance.value.setOption(options());
  };

  const randomFn = () => {
    useIntervalFn(() => {
      const value = (Math.random() * 100).toFixed(0);
      unref(chartInstance)?.setOption({
        series: [{ data: [{ value }] }, { data: [{ value }] }],
      } as EChartsOption);
    }, 3000);
  };

  const updateFn: DataFetchUpdateFn = (message, attribute) => {
    const { data = {} } = message;
    const [latest] = data[attribute] || [];
    if (!latest.length) return;
    const [timespan, value] = latest;
    time.value = timespan;
    updateChart(isNaN(value as unknown as number) ? 0 : Number(value));
  };

  useDataFetch(props, updateFn);

  onMounted(() => {
    initial();
    !props.config.option.uuid && randomFn();
  });

  const resize = async () => {
    await nextTick();
    unref(chartInstance)?.setOption({
      series: [
        {
          detail: {
            fontSize: unref(getDesign).valueSize * unref(getRatio),
          },
          progress: {
            width: 24 * unref(getRatio),
          },
          axisLine: {
            lineStyle: {
              width: 24 * unref(getRatio),
            },
          },
          axisTick: {
            distance: -35 * unref(getRatio),
            length: 6 * unref(getRatio),
          },
          splitLine: {
            // 分割线与轴线距离
            distance: -40 * unref(getRatio),
            length: 12 * unref(getRatio),
          },
        },
      ],
    });
    unref(chartInstance)?.resize();
  };

  useComponentScale(props, resize);
  const { getRatio } = useComponentScale(props, resize);
</script>

<template>
  <main
    class="w-full h-full flex flex-col justify-center items-center"
    :class="!getDesign.showTime && 'p-5'"
  >
    <DeviceName :config="config" />
    <div class="w-full h-full flex justify-center items-center flex-1 flex-col">
      <div ref="chartRefEl" class="flex-1 w-full h-6/7 flex justify-center items-center flex-col">
      </div>
      <div
        class="text-gray-500 text-center truncate"
        :style="{ fontSize: getDesign.fontSize + 'px' }"
        >{{ getDesign.attribute || '温度' }}</div
      >
    </div>
    <UpdateTime v-show="getDesign.showTime" :time="time" />
  </main>
</template>