VisitAnalysis.vue 1.88 KB
<template>
  <div style="min-width: 100%">
    <p class="center">告警数</p>
    <div ref="chartRef" :style="{ height, width }" v-show="alarmList.length"></div>
    <div v-show="!alarmList.length"><Empty /></div>
  </div>
</template>
<script lang="ts" setup>
  import { onMounted, ref, Ref, withDefaults, watch } from 'vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  import { Empty } from 'ant-design-vue';
  interface Props {
    width?: string;
    height?: string;
    alarmList: [number, string][];
  }
  const props = withDefaults(defineProps<Props>(), {
    width: '100%',
    height: '280px',
    alarmList: () => [],
  });

  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  onMounted(() => {
    setOptions({
      tooltip: {
        trigger: 'axis',
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },

      xAxis: {
        type: 'time',
      },
      yAxis: {
        type: 'value',
      },
      series: [
        {
          name: '告警数',
          type: 'bar',
          stack: 'Total',
          data: props.alarmList,
          color: '#3C78FF',
        },
      ],
    });
  });
  watch(
    () => props.alarmList,
    (newValue) => {
      setOptions({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },

        xAxis: {
          type: 'time',
        },
        yAxis: {
          type: 'value',
        },
        series: [
          {
            name: '告警数',
            type: 'bar',
            stack: 'Total',
            color: '#3C78FF',
            data: newValue,
          },
        ],
      });
    }
  );
</script>