<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";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    chartData: {
      type: Array,
      required: true,
    },
    height: {
      type: String,
      default: "400px",
    },
  },
  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() {
      setTimeout(() => {
        this.chart = echarts.init(this.$el, "macarons");
        this.setOptions(this.chartData);
      }, 300);
    },
    setOptions(chartData) {
      let indicatorData = [];
      let seriesData = [];
      if (chartData) {
        chartData.forEach((item, index) => {
          indicatorData.push({
            text: item.name,
          });
          // seriesData.push(item.value);
          seriesData.push(item.value);
        });
      }
      this.chart.setOption({
        color: ["rgba(37, 97, 239, 1)"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow", // 默认为直线，可选为：'line' | 'shadow'
          },
        },
        radar: [
          {
            indicator: indicatorData,
            center: ["50%", "60%"],
          },
        ],
        series: [
          {
            type: "radar",
            areaStyle: {}, //内圈填充
            tooltip: {
              trigger: "item",
            },
            data: [
              {
                value: seriesData,
                name: "绩效指标分值",
              },
            ],
            animationDuration,
          },
        ],
      });
    },
  },
};
</script>
