<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,
    },
  },
  data() {
    return {
      id: "",
      chart: null,
    };
  },
  computed: {
    datas() {
      return this.list.map((t) => {
        return t.value;
      });
    },
  },
  methods: {
    initChart() {
      let chart = echarts.init(this.$el);

      let option = {
        xAxis: {
          max: this.total,
          splitLine: {
            show: false,
          },
          axisLine: {
            show: false,
          },
          axisLabel: {
            show: false,
          },
          axisTick: {
            show: false,
          },
        },
        // tooltip: {
        //   trigger: "axis",
        //   backgroundColor: "rgba(0,0,0,0.4)",
        //   borderColor: partten.getColor("gray"),
        //   textStyle: {
        //     color: "#fff",
        //     fontSize: 14,
        //   },
        // },
        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: 10,
            // 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("green"), // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: partten.getColor("green"), // 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("purple"), // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: partten.getColor("purple"), // 100% 处的颜色
                        },
                      ],
                    };
                  } else {
                    color = {
                      type: "linear",
                      x: 0,
                      y: 0,
                      x2: 1,
                      y2: 0,
                      colorStops: [
                        {
                          offset: 0,
                          color: partten.getColor("green"), // 0% 处的颜色
                        },
                        {
                          offset: 1,
                          color: partten.getColor("green"), // 100% 处的颜色
                        },
                      ],
                    };
                  }
                  return color;
                },
                shadowBlur: 3,
                shadowColor: "rgba(255, 255, 255, 0.70)",
              },
            },
            label: {
              normal: {
                show: true,
                position: [0, util.vh("-20")],
                formatter: "{b}",
                textStyle: {
                  color: "#7a8385",
                  fontSize: util.vh("14"),
                },
              },
            },
            data: this.list,
            z: 1,
            animationEasing: "elasticOut",
          },
          {
            // 三角
            type: "pictorialBar",
            symbolPosition: "end",
            data: this.list,
            symbol: "triangle",
            symbolOffset: [0, -16],
            symbolSize: [10, 10],
            symbolRotate: 180,
            itemStyle: {
              normal: {
                borderWidth: 0,
                color: function(params) {
                  var color;
                  if (params.dataIndex == 0 || params.dataIndex == 2) {
                    color = partten.getColor("green");
                  } else if (params.dataIndex == 1 || params.dataIndex == 3) {
                    color = partten.getColor("purple");
                  } else {
                    color = partten.getColor("green");
                  }
                  return color;
                },
                shadowBlur: 2,
                shadowColor: "rgba(255, 255, 255, 0.80)",
              },
            },
          },
          {
            // 分隔
            type: "pictorialBar",
            itemStyle: {
              normal: {
                color: "#20314f",
              },
            },
            symbolRepeat: "fixed",
            symbolMargin: 6,
            symbol: "rect",
            symbolClip: true,
            symbolSize: [1, 10],
            symbolPosition: "start",
            symbolOffset: [10, -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: util.vh("14"),
                    lineHeight: util.vh(20),
                  },
                  gm: {
                    backgroundColor: partten.getColor("green"),
                    width: util.vh(8),
                    height: util.vh(8),
                    lineHeight: util.vh(20),
                    verticalAlign: "middle",
                    borderRadius: [50, 50, 50, 50],
                  },
                  pm: {
                    backgroundColor: partten.getColor("purple"),
                    width: util.vh(8),
                    height: util.vh(8),
                    lineHeight: util.vh(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("green"), // [, "#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("purple"), // [, "#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("green"), // [, "#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("purple"), // [, "#333"],
                    opacity: 0.7,
                    borderWidth: 1, // 边框宽度
                    // barBorderRadius: 0, //圆角半径
                    label: {
                      // 标签显示位置
                      show: false,
                      position: "top", // insideTop 或者横向的 insideLeft
                    },
                  },
                },
              },
            ],
            barWidth: 14,
          },
        ],
      };

      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.initChart();
    });
  },
  updated() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
};
</script>

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