<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: "300px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      // type: Object,
      // required: true,
    },
  },
  data() {
    return {
      chart: null,
      pieConfig: {
        title: {
          text: this.title,
          subtext: `筛查总例数  {number|${this.totalNum}}`,
          left: "center",
          textStyle: {
            color: "#fff",
            fontWeight: "normal",
          },
          subtextStyle: {
            color: "#aaa",
            rich: {
              number: {
                color: "#9BDFFF",
                fontSize: 14,
              },
            },
          },
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          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",
            radius: "50%",
            data: this.chartData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      },
    }
  },
  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 = JSON.parse(
        JSON.stringify(Object.assign(this.pieConfig, this.chartConfig))
      )
      // const series = chartData.data.map((v) => {
      //   return {
      //     name: v.name,
      //     value: Number(v.value),
      //   }
      // })
      // option.series[0].data = series
      this.chart.setOption(option)
    },
  },
}
</script>
