<template>
  <div class="chart" :id="id"></div>
</template>

<script>
import util from "@/helper/util.js";
import partten from "@/helper/partten.js";
import * as echarts from "echarts";

export default {
  name: "percent-pie",
  componentName: "percent-pie",
  props: {
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "18.519vh",
    },
    // 百分比值
    // 外部饼图数据
    list: {
      type: Array,
      default: () => [
        {
          value: 310,
          name: "邮件营销",
        },
        {
          value: 234,
          name: "联盟广告",
        },
        {
          value: 335,
          name: "视频广告",
        },
        {
          value: 548,
          name: "百度",
        },
        {
          value: 351,
          name: "谷歌",
        },
      ],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
    };
  },
  computed: {},
  methods: {
    initChart() {
      let option = {
        color: ["#05bb4c", "#4b55ae", "#fa8c16", "#f8de5b", "#1a93cf", "#c531c7", "#bd3338"],
        tooltip: {
          trigger: "item",
          backgroundColor: this.$store.state.themeName === "dark" ? "rgba(0,0,0,0.4)" : "rgba(255,255,255,0.5)",
          borderColor: this.$store.state.themeName === "dark" ? partten.getColor("gray") : "#000",
          textStyle: {
            color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
            fontSize: util.vh(16),
          },
        },
        grid: {
          top: 8,
          left: 8,
          right: 8,
          bottom: 8,
        },
        legend: {
          icon: "circle",
          orient: "vertical",
          top: "center",
          right: "0",
          align: "left",
          textStyle: {
            color: this.$store.state.themeName === "dark" ? "#fff" : "#000",
          },
          itemGap: 8,
        },
        series: [
          {
            type: "pie",
            radius: "70%",
            center: ["35%", "50%"],
            data: this.list,
            labelLine: {
              show: false,
            },
            label: {
              show: false,
            },
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };

      this.chart.setOption(option);
    },
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
  },
  mounted() {
    this.$el.style.width = this.width;
    this.$el.style.height = this.height;
    this.chart = echarts.init(this.$el);
    this.initChart();
  },
  updated() {
    this.initChart();
  },
};
</script>

<style lang="less">
.chart {
  width: 100%;
  height: 100%;
  display: inline-block;
}
</style>
