<template>
  <div ref="pieChart2" :style="{ height: height, width: width }"></div>
</template>
<script>
import * as echarts from "echarts";
import api from "../../../../api/Overview/index";
import { ElMessage } from "element-plus";
export default {
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "100%",
    },
  },
  data() {
    return {};
  },
  mounted() {},
  beforeUnmount() {
    window.removeEventListener("resize", this.chart);
  },
  watch: {
    /**
     * @监听vuex存储值变化 用于调用api
     */
    "$store.state.siteId": {
      immediate: true, // 首次加载的时候执行函数
      deep: true, // 深入观察,监听数组值，对象属性值的变化
      handler: function () {
        this.deviceTypeCount();
      },
    },
  },
  methods: {
    deviceTypeCount() {
      var _this = this;
      api
        .deviceTypeCount({
          site: _this.$store.state.siteId,
        })
        .then((requset) => {
          if (requset.status === "SUCCESS") {
            var data = requset.data;
            _this.initChart(
              data.epCount == null ? 0 : data.epCount,
              data.videoCount == null ? 0 : data.videoCount
            );
          } else {
            ElMessage.success({
              message: requset.msg,
              type: "success",
            });
          }
        });
    },
    //次数分布折线图
    initChart(epCount, videoCount) {
      var chart = echarts.init(this.$refs.pieChart2);
      var option;

      var pie = [
        {
          value: epCount,
          name: "电力",
        },
        {
          value: videoCount,
          name: "视频",
        },
      ];
      option = {
        grid: {},

        title: [
          {
            text: "{name|" + (epCount + videoCount) + "}\n{val|总数}",
            top: "center",
            left: "center",
            textStyle: {
              rich: {
                name: {
                  fontSize: '.4rem',
                  fontWeight: "normal",
                  color: "#FFFFFF",
                  fontFamily: "impact",
                  padding: [0, 0, 3, 0],
                },
                val: {
                  fontSize: 14,
                  fontWeight: "normal",
                  color: "#FFFFFF",
                  padding: [3, 0, 0, 0],
                },
              },
            },
          },
        ],
        tooltip: {
          // trigger: "item",
          trigger: "item",
          confine: true, //将此限制打开后tooltip将不再溢出
          formatter: function (params) {
            return (
              params.name +
              "：" +
              params.value +
              "<br>占比：" +
              params.percent.toFixed(2) +
              "%"
            );
          },
        },

        series: [
          {
            label: {
          
            
              normal: {
                 overflow: 'none',
                position: 'outside',
                padding:[30,-10,30,-10],
                show: true,
                textStyle: {
                  color: "#fff",
                  fontSize:12,

                },
                formatter: "{b}",
              },
              // emphasis: {
              //   show: true,
              // },
            },
            radius: ["43%", "60%"],
            type: "pie",
            data: pie,
            labelLine: {
              normal: {
                length: 2, //aa折线长度
                // length2: 4, //aa折线长度
              },
            },
          },
        ],
        color: ["#39FEFC", "#FC3535"],
      };

      chart.setOption(option, true);
      window.addEventListener("resize", () => {
        chart.resize();
      });
      this.chart = chart;
    },
  },
};
</script>