<template>
  <div>
    <div ref="chart" class="item" style="width: 220px; height: 200px" />
  </div>
</template>

<script>
import * as echarts from "echarts";
import toolUtils from "@/utils/echartsTooltip";

export default {
  props: {
    BasicLsit: Array,
    allNum: Number,
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    BasicLsit: {
      handler(val) {
        if (val.length > 0) {
          this.$nextTick(() => {
            // 获取dom容器
            const chartDom = this.$refs.chart;
            //   // 初始化图表实例
            this.chart = echarts.init(chartDom);
            this.initChart(this.BasicLsit);
          });
        }
      },
      immediate: true,
      deep: true,
    },
  },
  methods: {
    initChart(data) {
      const option = {
        title: [
          {
            text: this.allNum,
            top: "40%",
            textAlign: "center",
            left: "49%",
            textStyle: {
              color: "#fff",
              fontSize: 12,
            },
          },
          {
            text: "高层建筑总数",
            top: "49%",
            textAlign: "center",
            left: "49%",
            textStyle: {
              color: "#fff",
              fontSize: 10,
            },
          },
        ],
        tooltip: {
          trigger: "item",
          show: true,
          confine: true,
          extraCssText: "z-index: 1000;",
          backgroundColor: "#02809a",
          borderColor: "rgba(0,213,255, .6)",
          textStyle: {
            color: "#fff",
          },
          formatter: "{b} <br/>{c} {d}%",
        },
        series: [
          // 最外层圆
          {
            type: "pie",
            radius: ["40%", "80%"],
            center: ["50%", "50%"],
            hoverAnimation: false,
            data: [
              {
                value: data.map((item) => {
                  return item.zzjz;
                }),
                name: "住宅建筑",
                itemStyle: {
                  normal: {
                    color: '#6dd400'
                  },
                },
              },
              {
                value: data.map((item) => {
                  return item.ggjz;
                }),
                name: "公共建筑",
                itemStyle: {
                  normal: {
                    color: '#005af0'
                  },
                },
              },
              {
                value: data.map((item) => {
                  return item.gyjz;
                }),
                name: "工业建筑",
                itemStyle: {
                  normal: {
                    color: '#ffa600'
                  }
                },
              },
            ],
            label: {
              show: false,
            }
          }
        ],
      };

      this.chart.setOption(option);
      //自适应图表
      window.onresize = this.chart.resize;
      // 自动轮播
      toolUtils.autoHover(this.chart, option, 3, 5000);
    },
  },
};
</script>

<style scoped></style>
