<!--
 * @Author: your name
 * @Date: 2021-10-11 14:54:18
 * @LastEditTime: 2021-12-03 14:58:55
 * @LastEditors: Please set LastEditors
 * @Description: 生产情况的环形图
 * @FilePath: \hyyfClient\src\components\erp\ProductionDonut.vue
-->
<template>
  <div class="production-donut">
    <div :id="id" class="chartPie"></div>
  </div>
</template>
<script>
export default {
  props: {
    id: {
      type: Number,
      required: true,
    },
    stockName: {
      type: String,
      required: true,
    },
    stockQuantity: {
      type: String,
      required: true,
    },
    color: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      myChart: null,
      rest: 0,
    };
  },
  methods: {
    init() {
      this.rest = 1 - parseFloat(this.stockQuantity);
      const useColor = this.color;
      let options = {
        title: {
          text: this.stockName,
          x: 35,
          y: 175,
        },
        grid: {
          x: 100,
          y: 200,
        },
        // tooltip: {
        //   trigger: 'item',
        //   formatter: function ({name, value, percent}) {
        //     return name + '：' + value + '头&nbsp;&nbsp;&nbsp;&nbsp;' + percent + '%'
        //   }
        // },
        // legend: {
        //   top: '15%',
        //   left: '55%',
        //   orient: 'vertical',
        //   icon: "circle",
        //   selectedMode: false, // 取消右侧项选中
        //   itemGap: 20, // 各项间隔
        //   textStyle: {
        //     fontSize: 15,
        //     color: '#666'
        //   },
        // },
        graphic: [
          {
            type: "text",
            left: "35",
            top: "38%",
            style: {
              text: (parseFloat(this.stockQuantity) * 100).toFixed(2) + "%",
              fontSize: 28,
              fill: useColor,
            },
          },
        ],
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["65%", "80%"], // 半径
            center: ["30%", "45%"],
            avoidLabelOverlap: false,
            label: {
              show: false,
            },
            hoverAnimation: false,
            emphasis: {
              label: {
                show: false,
                fontSize: "40",
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            itemStyle: {
              normal: {
                color: function(colors) {
                  // 颜色设置
                  var colorList = [useColor, "#000000"];
                  return colorList[colors.dataIndex];
                },
                // borderWidth: 5, // 设置各项空隙
                // borderColor: '#fff'
              },
            },
            data: [
              { value: parseFloat(this.stockQuantity) },
              { value: this.rest },
              // { value: 2032, name: '哺乳仔猪存栏' },
              // { value: 1644, name: '保育猪存栏' },
              // { value: 1546, name: '育肥猪存栏' }
            ],
          },
        ],
      };

      this.myChart.setOption(options);
    },
  },
  mounted() {
    this.myChart = this.$echarts.init(document.getElementById(this.id));
    this.init();
  },
  watch: {
    stockName: {
      handler(newValue) {
        console.log(newValue);
        this.init();
      },
    },
  },
};
</script>
<style scoped>
.production-donut {
  display: inline-block;
  margin-top: 20px;
  width: 20%;
  height: 200px;
}
.chartPie {
  width: 100%;
  height: 100%;
}
</style>
