<template>
  <div
    :class="className"
    :style="{height:height,width:width}"
  />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import { statistics } from "@/api/index/index";
import { setTimeout } from "timers";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    chartDate: "",
    height: {
      type: String,
      default: "400px"
    }
  },
  watch: {
    chartDate(value) {
      this.chartDate = value;
      let arr = [];
      value.nation_group.map(item => {
        arr.push({
          name: item.name + "(" + item.nums + "人)",
          value: item.nums
        });
      });
      this.nation = arr;
      this.initChart();
    }
  },
  data() {
    return {
      chart: null,
      nation: []
    };
  },
  mounted() {},
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");

      this.chart.setOption({
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        legend: {
          top: "5%",
          left: "center"
        },
        series: [
          {
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2
            },
            label: {
              show: false,
              position: "center"
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "40",
                fontWeight: "bold"
              }
            },
            labelLine: {
              show: false
            },
            data: this.nation
          }
        ]
      });
    }
  }
};
</script>
