<template>
  <div>
    <div :id="id" :style="styleObj"></div>
  </div>
</template>

<script>
export default {
  props: {
    title: {
      type: String,
      default: "",
    },
    id: {
      type: String,
      default: "",
    },
    legendName: {
      type: String,
      default: "",
    },
    styleObj: {
      type: Object,
      default: () => {},
    },
    parameter: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      myChart: null
    };
  },
  watch: {
    title(val) {
      this.myChart && this.myChart.setOption({title:{text:val}})
    },
    parameter: {
      handler: function (v) {
        this.drawLine(v)
      },
      deep: true
    }
  },
  mounted() {
    this.drawLine();
  },

  methods: {
    drawLine(obj) {
      if (obj && this.myChart) {
        // 更新
        this.myChart.setOption({
          series: [
            {
              data: obj,
            },
          ]
        })
        return
      }
      // 基于dom，初始化echarts实例
      let myChart = this.$echarts.init(document.getElementById(this.id));
      this.myChart = myChart
      // 绘制图表
      myChart.setOption({
        title: { text: this.title },
        legend: {
          orient: "vertical",
          left: "right",
          bottom: "0",
          itemWidth: 20,
          itemHeight: 10,
          icon: "roundRect",
          textStyle: {
            fontSize: "14px",
            color: "#000",
          },
        },
        // color:'#6e98ed',
        tooltip: {
          trigger: "item",
        },

        grid: {
          left: "3%",
          right: "4%",
          containLabel: true,
        },
        series: [
          {
            name: this.legendName,
            type: "pie",
            radius: "65%",
            data: this.parameter,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      });
    },
  },
};
</script>

<style lang="less" scoped></style>
