<template>
  <div
    :id="id"
    :class="className"
    :style="{ height: height, width: width }"
  ></div>
</template>
<script>
import echarts from "echarts"
import resize from "./mixins/resize"
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100px",
    },
    height: {
      type: String,
      default: "100px",
    },
  },
  data() {
    return {
      chart: null,
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        //标题
        title: {
          text: "储存状态",
          x: "center",
          textStyle: {
            fontSize: 14,
            color: "blue",
          },
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)",
        },
        legend: {
          orient: "horizontal",
          left: "30%",
          bottom: "1%",
          //图例标记的图形宽度
          //  itemWeight:'10px',
          data: ["半仓", "满仓", "空仓"],
        },
        series: [
          {
            // 是否启用图例 hover 时的联动高亮。
            legendHoverLink: true,
            name: "储存状态",
            type: "pie",
            radius: ["60%", "40%"],
            avoidLabelOverlap: false,
            // 板块之间的间隔
            itemStyle: {
              borderWidth: 2, //设置border的宽度有多大
              borderColor: "#fff",
            },
            label: {
              show: true,
              position: "outside", //标签的位置
              //   position: "center",
              formatter: "{d}%", //设置百分比
              textStyle: {
                fontWeight: 12,
                fontSize: 12, //文字的字体大小
                // color:"#FFF",
              },
            },
            emphasis: {
              label: {
                show: true,
                fontSize: "15",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: [
              { value: 335, name: "半仓" },
              { value: 310, name: "满仓" },
              { value: 234, name: "空仓" },
            ],
          },
        ],
      })
    },
  },
}
</script>