<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,
      default: [{ name: "内镜应答人数" }],
    },
  },
  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,
          subtext: `中高危人数  {number|${this.totalNum}}`,
          left: "center",
          textStyle: {
            color: "#fff",
            fontWeight: "normal",
          },
          subtextStyle: {
            color: "#aaa",
            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,
          },
          data: ["内镜应答人数"],
          formatter: (name) => {
            return `${name}   ${this.chartData[1].value}人`
          },
        },
        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: "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>