<template>
  <div style="width: 100%; height: 100%">
    <div id="circlecz" style="width: 100%; height: 100%"></div>
  </div>
</template>

<script setup>
import * as echarts from "echarts";
import { ref, onMounted, watch, nextTick } from "vue";
const props = defineProps({
  data: Array
});

watch(
  () => props.data,
  (val) => {
      nextTick(()=>{
          lineChartFn(val);
      })
  },
  {
    immediate: true,
    deep: true
  }
);

function lineChartFn(val) {
  let num = val.map(item=>{ return item.value})
  let total = num.reduce((value1,value2)=>{
    return parseInt(value1) + parseInt(value2)
  },0)
  var chartDom = document.getElementById("circlecz");
  var myChart = echarts.init(chartDom, "dark");
  var echartData = val;
  let colors = ["#c487ee", "#deb140", "#49dff0", "#034079",  "#6f81da", "#00ffb4"]
  var rich = {
    yellow: {
      color: "#ffc72b",
      fontSize: 14,
      padding: [5, 4],
      align: "center"
    },
    total: {
      color: "#ffc72b",
      fontSize: 14 ,
      align: "center"
    },
    white: {
      color: "#fff",
      align: "center",
      fontSize: 14 ,
      padding: [0, 0]
    },
    blue: {
      color: "#49dff0",
      fontSize: 14 ,
      align: "center"
    }
  };
  var option = {
    backgroundColor: "rgba(0,0,0,0)",
    legend: {
      orient: "vertical",
      right: 0,
      top: 5
    },
    series: [
      {
        name: "今日警情处置分析图",
        type: "pie",
        radius: ["32%", "50%"],
        hoverAnimation: false,
        color: colors,
        label: {
          normal: {
            formatter: function (params) {
              var percent = 0; //考生占比
              if(total > 0) percent = ((params.value / total) * 100).toFixed(1);
              return ( "{white|" + params.name + "}{yellow|" + params.value + "}\n{blue|" + percent + "%}");
            },
            rich: rich
          }
        },
        data: echartData
      }
    ]
  };

  option && myChart.setOption(option);
  document.getElementById("circlecz").setAttribute("_echarts_instance_", "");
}
</script>

<style lang="scss" scoped>
</style>
