<template>
  <div class="char">
    <div id="piecircles" style="width: 100%; height: 100%"></div>
  </div>
</template>

<script setup>
import * as echarts from "echarts";
import { ref, onMounted, watch, defineProps } from "vue";
const props = defineProps({
  key: Number,
  list: { type: Array }
});
watch(
  () => props.key,
  () => {
    lineChartFn();
  },
  {
    deep: true
  }
);


function lineChartFn() {
  var chartDom = document.getElementById("piecircles");
  var myChart = echarts.init(chartDom, "dark");
  var option;

  option = {
    backgroundColor: "#fff",
    title: {
      text: "事故分类统计（20类）",
      left: 'center',
      textStyle: {
        color: '#000'
      },
    },
    tooltip: {
      trigger: "item",
      formatter: "{a} <br/>{b} : {c} ({d}%)"
    },
    legend: {
      orient: "vertical",
      x: "left",
      y: "10%",
      textStyle: {
        //图例中文字的样式
        color: "#000",
      },
      data: props.list.map(item => item.typeName)
    },
    toolbox: {
      show: true,
      feature: {
        mark: { show: true },
        dataView: { show: true, readOnly: false },
        magicType: {
          show: true,
          type: ["pie", "funnel"]
        },
        restore: { show: true },
        saveAsImage: { show: true }
      },
      axisLabel: {
        color: '#000',
      },
    },
    calculable: false,
    series: [
      {
        name: "事故事件饼状图",
        type: "pie",
        x: "50%",
        width: "50%",
        funnelAlign: "left",
        max: 1048,
        label: {
          show: true,
          color: '#000', // 这里设置字体颜色
        },

        data: props.list.map((item) => {
          return { name: item.typeName, value: item.count, itemStyle: { color: item.color } }
        })
      }


    ]
  };
  option && myChart.setOption(option);
  window.onresize = function () {
    myChart.resize();
  };
  document.getElementById("piecircles").setAttribute("_echarts_instance_", "");
}
onMounted(() => {
  lineChartFn();
});
</script>

<style lang="scss" scoped>
.char {
  width: 100% !important;
  height: 320px !important;
}

.canvas {
  background-color: #fff !important;
}
</style>
