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

<script>
import * as echarts from "echarts"
// require("echarts/theme/macarons"); // echarts theme
import resize from "@/components/Charts/mixins/resize"
const animationDuration = 2600
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    title: { type: String },
    colorList: {
      type: Array,
    },
    totalNum: {
      default: 0,
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "280px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      // type: Object,
      // required: true,
      default: "",
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    },
    setOptions(chartData) {
      // if (!chartData.data) return
      let option = {
        title: {
          // text: this.title + "\n\n" + `{number|${this.totalNum}}`,
          left: "center",
          top: "34%",
          textStyle: {
            color: "#ddd",
            fontWeight: "normal",
            fontSize: 14,
            rich: {
              number: {
                color: "#9BDFFF",
                fontSize: 14,
              },
            },
          },
        },
        tooltip: {
          trigger: "item",
          valueFormatter: (value) => value + "%",
          backgroundColor: "rgba(50,50,50,0.7)",
          textStyle: {
            color: "#fff",
          },
        },
        legend: {
          show: true,
          orient: "horizontal",
          left: "center",
          bottom: "5%",
          itemWidth: 8,
          itemHeight: 8,
          textStyle: {
            color: "#aaa",
            fontSize: 14,
          },
        },
        label: {
          show: true,
          position: "outside",
          formatter: "{d}%",
        },
        labelLine: {
          normal: {
            length: 20,
            length2: 30,
            lineStyle: {
              width: 1,
            },
          },
        },
        color: this.colorList,
        series: [
          {
            name: this.title,
            type: "pie",
            top: "-18%",
            radius: "44%",
            // radius: ["30%", "44%"],
            data: this.chartData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      }
      // const series = chartData.data.map((v) => {
      //   return {
      //     name: v.name,
      //     value: Number(v.value),
      //   }
      // })
      // option.series[0].data = series
      this.chart.setOption(option)
    },
  },
}
</script>
