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

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "../../dashboard/mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "350px",
    },
    height: {
      type: String,
      default: "350px",
    },
    data: {
      type: Array,
      default: () => [
        {
          name: "难",
          value: 15,
        },
        {
          name: "中",
          value: 35,
        },
        {
          name: "易",
          value: 20,
        },
      ],
    },
    values: {
      type: Array,
      default: () => [5, 10, 8],
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');

      this.chart.setOption({
        title: {
          text: "难度占比分析图",
          left: "center",
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          left: "center",
          bottom: "10",
          data: this.data.map((i) => i.name),
        },
        series: [
          {
            name: "难度分布",
            type: "pie",
            radius: [0, 100],
            center: ["50%", "45%"],
            data: this.data,
            animationEasing: "cubicInOut",
            animationDuration: 2600,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      });
    },
  },
};
</script>
