<template>
  <div class="ech">
    <div id="fxp" style="width: 100%; height: 260px"></div>
  </div>
</template>

<script setup>
import * as echarts from "echarts";
import { ref, onMounted, defineProps, watch } from "vue";
const props = defineProps({
  data: { type: Object }
});
watch(
  () => props.data.value,
  () => {
    chartFn();
  }
);
function chartFn() {
  var chartDom = document.getElementById("fxp");
  var myChart = echarts.init(chartDom);
  var option;
  option = {
    grid: {
      left: 0,
      top: 0,
      right: 0,
      bottom: 0
    },
    series: [
      {
        type: "gauge",
        radius: "100%",
        center: ["50%", "65%"],
        startAngle: 180,
        endAngle: 0,
        min: 0,
        max: 1,
        splitNumber: 8,
        axisLine: {
          lineStyle: {
            width: 6,
            color: [
              [0.75, "#58D9F9"],
              [1, "rgb(238, 6, 6)"]
            ]
          }
        },
        pointer: {
          itemStyle: {
            color: "auto"
          },
          showAbove: true
        },
        axisTick: {
          length: 12,
          lineStyle: {
            color: "auto",
            width: 2
          }
        },
        splitLine: {
          length: 20,
          lineStyle: {
            color: "auto",
            width: 5
          }
        },
        axisLabel: {
          show: false
        },
        title: {
          offsetCenter: [0, "-20%"],
          fontSize: 16,
          color: "#ddd"
        },
        detail: {
          fontSize: 20,
          offsetCenter: [0, "35%"],
          valueAnimation: true,
          formatter: function (value) {
            return Math.round(value * props.data.total) + "个";
          },
          color: "auto"
        },
        data: [
          {
            value: (props.data.value / props.data.total).toFixed(2),
            name: "警情数量"
          }
        ]
      }
    ]
  };
  option && myChart.setOption(option);
  document.getElementById("fxp").setAttribute("_echarts_instance_", "");
}
onMounted(() => {
  chartFn();
  window.addEventListener("resize", () => {
    chartFn();
  });
  window.onresize = function () {
    chartFn();
  };
});
</script>

<style lang="scss" scoped>
.ech {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
}
</style>
