<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: () => [
        {
          name: "当日预测电量",
          value: 103.62,
        },
        {
          name: "实际发电量",
          value: 98.62,
        },
        {
          name: "当月预测电量",
          value: 113.27,
        },
        {
          name: "实际发电量",
          value: 136.72,
        },
      ],
    },
    total: {
      type: Number,
      default: 150,
    },
    colors: {
      type: Array,
      default: () => ["green", "purple"],
    },
  },
  data() {
    return {
      id: "",
      chart: null,
    };
  },
  computed: {
    datas() {
      return this.list.map((t) => {
        return t.value;
      });
    },
  },
  methods: {
    initChart() {
      let color1 = this.colors[0];
      let color2 = this.colors[1];

      let option = {
        xAxis: {
          max: this.total,
          splitLine: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 16,
          top: 16, // 设置条形图的边s距
          right: 80,
          bottom: 0,
        },
        yAxis: [
          {
            type: "category",
            inverse: true,
            data: this.list,
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
          },
        ],
        series: [
          // 内
          {
            type: "bar",
            barWidth: 6,
            // legendHoverLink: false,
            // silent: true,
            itemStyle: {
              normal: {
                color: function(params) {
                  var color;
                  if (params.dataIndex == 0 || params.dataIndex == 2) {
                    color = {
                      type: "linear",
                      x: 0,
                      y: 0,
                      x2: 1,
                      y2: 0,
                      colorStops: [
                        {
                          offset: 0,
                          color: partten.getColor(color1), // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: partten.getColor(color1), // 100% 处的颜色
                        },
                      ],
                    };
                  } else if (params.dataIndex == 1 || params.dataIndex == 3) {
                    color = {
                      type: "linear",
                      x: 0,
                      y: 0,
                      x2: 1,
                      y2: 0,
                      colorStops: [
                        {
                          offset: 0,
                          color: partten.getColor(color2), // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: partten.getColor(color2), // 100% 处的颜色
                        },
                      ],
                    };
                  } else {
                    color = {
                      type: "linear",
                      x: 0,
                      y: 0,
                      x2: 1,
                      y2: 0,
                      colorStops: [
                        {
                          offset: 0,
                          color: partten.getColor(color1), // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: partten.getColor(color1), // 100% 处的颜色
                        },
                      ],
                    };
                  }
                  return color;
                },
                shadowBlur: 10,
                shadowColor: "rgba(255, 255, 255, 0.30)",
              },
            },
            label: {
              normal: {
                show: true,
                position: [0, util.vh("-20")],
                formatter: function(param) {
                  return param.data.name;
                },
                textStyle: {
                  color: "#7a8385",
                  fontSize: 12,
                },
                rich: {
                  c1: {
                    color: partten.getColor(color1),
                  },
                  c2: {
                    color: partten.getColor(color2),
                  },
                },
              },
            },
            data: this.list,
            z: 1,
            animationEasing: "elasticOut",
          },
          // 三角
          {
            type: "pictorialBar",
            symbolPosition: "end",
            data: this.list,
            symbol: "triangle",
            symbolOffset: [0, -10],
            symbolSize: [5, 5],
            symbolRotate: 180,
            itemStyle: {
              normal: {
                borderWidth: 0,
                color: function(params) {
                  var color;
                  if (params.dataIndex == 0 || params.dataIndex == 2) {
                    color = partten.getColor(color1);
                  } else if (params.dataIndex == 1 || params.dataIndex == 3) {
                    color = partten.getColor(color2);
                  } else {
                    color = partten.getColor(color1);
                  }
                  return color;
                },
              },
            },
          },
          // 分隔
          {
            type: "pictorialBar",
            itemStyle: {
              normal: {
                color: "#20314f",
              },
            },
            animation: this.firstAnimation,
            symbolRepeat: "fixed",
            symbolMargin: 4,
            symbol: "rect",
            symbolClip: true,
            symbolSize: [1, 8],
            symbolPosition: "start",
            symbolOffset: [8, -1],
            symbolBoundingData: this.total,
            symbolRotate: -15,
            data: this.list,
            z: 2,
            animationEasing: "elasticOut",
          },
          // 外边框
          {
            type: "pictorialBar",
            symbol: "rect",
            symbolBoundingData: this.total,
            itemStyle: {
              normal: {
                color: "none",
              },
            },
            label: {
              normal: {
                formatter: (params) => {
                  var text;
                  if (params.dataIndex == 0 || params.dataIndex == 2) {
                    text = "{gm|}{f|  " + params.data + "}";
                  } else if (params.dataIndex == 1 || params.dataIndex == 3) {
                    text = "{pm|}{f|  " + params.data + "}";
                  } else {
                    text = "{gm|}{f|  " + params.data + "}";
                  }
                  return text;
                },
                rich: {
                  f: {
                    color: "#ffffff",
                    fontSize: 14,
                    lineHeight: 20,
                    fontFamily: "Bicubik",
                  },
                  gm: {
                    backgroundColor: partten.getColor(color1),
                    width: 4,
                    height: 4,
                    lineHeight: 20,
                    verticalAlign: "middle",
                    borderRadius: [50, 50, 50, 50],
                  },
                  pm: {
                    backgroundColor: partten.getColor(color2),
                    width: 4,
                    height: 4,
                    lineHeight: 20,
                    verticalAlign: "middle",
                    borderRadius: [50, 50, 50, 50],
                  },
                },
                position: "right",
                distance: 8, // 向右偏移位置
                show: true,
              },
            },
            data: this.datas,
          },
          // 外框
          {
            type: "bar",
            name: "外框",
            barGap: "-120%", // 设置外框粗细
            data: [
              {
                value: this.total,
                itemStyle: {
                  normal: {
                    color: "transparent",
                    borderColor: partten.getColor(color1), // [, "#333"],// [, "#333"],
                    borderWidth: 1, // 边框宽度
                    // barBorderRadius: 0, //圆角半径
                    opacity: 0.5,
                    label: {
                      // 标签显示位置
                      show: false,
                      position: "top", // insideTop 或者横向的 insideLeft
                    },
                  },
                },
              },
              {
                value: this.total,
                itemStyle: {
                  normal: {
                    color: "transparent",
                    borderColor: partten.getColor(color2), // [, "#333"],
                    opacity: 0.5,
                    borderWidth: 1, // 边框宽度
                    // barBorderRadius: 0, //圆角半径
                    label: {
                      // 标签显示位置
                      show: false,
                      position: "top", // insideTop 或者横向的 insideLeft
                    },
                  },
                },
              },
              {
                value: this.total,
                itemStyle: {
                  normal: {
                    color: "transparent",
                    borderColor: partten.getColor(color1), // [, "#333"],
                    borderWidth: 1, // 边框宽度
                    opacity: 0.5,
                    // barBorderRadius: 0, //圆角半径
                    label: {
                      // 标签显示位置
                      show: false,
                      position: "top", // insideTop 或者横向的 insideLeft
                    },
                  },
                },
              },
              {
                value: this.total,
                itemStyle: {
                  normal: {
                    color: "transparent",
                    borderColor: partten.getColor(color2), // [, "#333"],
                    opacity: 0.7,
                    borderWidth: 1, // 边框宽度
                    // barBorderRadius: 0, //圆角半径
                    label: {
                      // 标签显示位置
                      show: false,
                      position: "top", // insideTop 或者横向的 insideLeft
                    },
                  },
                },
              },
            ],
            barWidth: 9,
          },
        ],
      };

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

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