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

<script>
import G2 from "@antv/g2";
import { mapGetters } from "vuex";

export default {
  name: "cake",
  data() {
    return {
    };
  },
  props: {
    width: {
      type: Number,
      default() {
        return 0;
      }
    },
    height: {
      type: Number,
      default() {
        return 0;
      }
    },
    id: {
      type: String,
      default() {
        return "";
      }
    },
    dataList:{
      type: Array,
      default() {
        return [];
      }
    }
  },
  computed: {
    ...mapGetters(["fontSize"])
  },
  mounted() {
    let max = 0;
    let total=0
    this.dataList.forEach(function(obj) {
      if (obj.totalNum > max) {
        max = obj.totalNum;
      }
      total+=obj.totalNum
    });
    // 自定义 other 的图形，增加两条线
    G2.Shape.registerShape("interval", "sliceShape", {
      draw(cfg, container) {
        const points = cfg.points;
        const origin = cfg.origin._origin;
        const percent = origin.totalNum / max;
        const xWidth = points[2].x - points[1].x;
        const width = xWidth * percent;
        let path = [];
        path.push(["M", points[0].x+0.3, points[0].y]);
        path.push(["L", points[1].x+0.3, points[1].y]);
        path.push(["L", points[0].x+0.3 + width, points[2].y]);
        path.push(["L", points[0].x+0.3 + width, points[3].y]);
        path.push("Z");
        path = this.parsePath(path);
        return container.addShape("path", {
          attrs: {
            fill: cfg.color,
            path
          }
        });
      }
    });

    const chart = new G2.Chart({
      container: this.id,
      width: this.width,
      autoFit: true,
      padding: [this.$root.$options.filters.filterPx(0.4,this.fontSize),'auto',this.$root.$options.filters.filterPx(0.4,this.fontSize),this.$root.$options.filters.filterPx(0.3,this.fontSize)],
      height: this.height
    });

    chart.source(this.dataList);
    chart.coord("theta", {
      radius: 0.8
    });
    chart.legend({
      position: "right-center",
      offsetX:  this.$root.$options.filters.filterPx(0.8,this.fontSize),
      textStyle: {
        fill: "#ffffff",
        fontSize:  this.$root.$options.filters.filterPx(0.16,this.fontSize),
        fontWeight: 300
      }
    });
    chart
      .intervalStack()
      .position("totalNum")
      .color("name", ["#10BF79", "#0A9EBF", "#065ABF", "#BD2B4A", "#BCBF33"])
      .shape("sliceShape")
      .label("totalNum", {
        textStyle: {
          fill: "#ffffff",
          fontSize:  this.$root.$options.filters.filterPx(0.16,this.fontSize),
          fontWeight: 300
        },
        offset:  this.$root.$options.filters.filterPx(0.42,this.fontSize),
        formatter: (val, item) => {
          return  parseInt((val/total)*100) + "%";
        }
      });

    chart.render();
  },
  methods: {}
};
</script>

<style lang="less">
.commandCake {
}
</style>
