<template>
  <v-chart class="chart" :option="option" />
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import Mock from "mockjs";

let colors = ["#0BFC7F", "#A0A0A0", "#F48C02", "#F4023C"];
const option = ref({});
const totalNum = ref(0)
const getData = () => {
const  data = [
  {
    "name": "其他",
    "value": Mock.mock('@integer(1, 100)'),
  },
  {
    "name": "邮件",
    "value":Mock.mock('@integer(1, 100)')
  },
  {
    "name": "微信",
    "value": Mock.mock('@integer(1, 100)')
  },
  {
    "name": "APP",
    "value": Mock.mock('@integer(1, 100)')
  },
  {
    "name": "微博",
    "value": Mock.mock('@integer(1, 100)'),
  },
  {
    "name": "电话",
    "value": Mock.mock('@integer(10, 500)')
  },
  {
    "name": "网站",
    "value": Mock.mock('@integer(1, 100)'),
  }
]
  totalNum.value = data.reduce((total, item) => {
    return total + item.value;
  }, 0);
  setOption(data);
};
const setOption = (data:any) => {
  option.value = {
    title: {
      top: "center",
      left: "center",
      text: [`{value|${totalNum.value}}`, "{name|总数}"].join("\n"),
      textStyle: {
        rich: {
          value: {
            color: "#ffffff",
            fontSize: 24,
            fontWeight: "bold",
            lineHeight: 20,
            padding: [4, 0, 4, 0],
          },
          name: {
            color: "#ffffff",
            lineHeight: 20,
          },
        },
      },
    },
    tooltip: {
      trigger: "item",
      backgroundColor: "rgba(0,0,0,.6)",
      borderColor: "rgba(147, 235, 248, .8)",
      textStyle: {
        color: "#FFF",
      },
    },
    series: [
      {
        name: "",
        type: "pie",
        radius: ["40%", "70%"],
        // avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 6,
          borderColor: "rgba(255,255,255,0)",
          borderWidth: 2,
        },
        color: colors,
        label: {
          show: true,
          formatter: "   {b|{b}}   \n   {c|{c}个}   {per|{d}%}  ",
          //   position: "outside",
          rich: {
            b: {
              color: "#fff",
              fontSize: 12,
              lineHeight: 26,
            },
            c: {
              color: "#31ABE3",
              fontSize: 14,
            },
            per: {
              color: "#31ABE3",
              fontSize: 14,
            },
          },
        },
        emphasis: {
          show: false,
        },
        legend: {
          show: false,
        },
        tooltip: { show: true },

        labelLine: {
          show: true,
          length: 20, // 第一段线 长度
          length2: 36, // 第二段线 长度
          smooth: 0.2,
          lineStyle: {},
        },
        data:data
      },
    ],
  };
};
onMounted(() => {
  getData();
});
</script>
<style scoped lang="scss"></style>
