<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";
import $ from "jquery";

export default {
  name: "dsah-pie",
  componentName: "dsah-pie",
  props: {
    // 宽度 默认9.722vh
    width: {
      type: String,
      default: "85%",
    },
    // 高度 默认9.722vh
    height: {
      type: String,
      default: "7.4074vh",
    },
    // 标题
    title: {
      type: String,
      default: "标题",
    },
    unit: {
      type: String,
      default: "",
    },
    // 值
    value: {
      type: Number,
      default: 0,
    },
    // 最小值
    min: {
      type: Number,
      default: 0,
    },
    // 最大值
    max: {
      type: Number,
      default: 100,
    },
    // 颜色 传入 green yellow等 (partten中支持的颜色)
    color: {
      type: String,
      default: "green",
    },
  },
  data() {
    return {
      id: "",
      chart: null,
    };
  },
  computed: {
    colorValue() {
      return partten.getColor(this.color);
    },
  },
  methods: {
    resize() {},
    initChart() {
      let chart = echarts.init(this.$el);
      let title = this.title;
      let unit = this.unit;
      let option = {
        // title: {
        //   show: true,
        //   text: this.unit,
        //   subtext: this.title,
        //   subtextStyle: {
        //     fontSize: 14,
        //     color: "#fff",
        //   },
        //   x: "45%",
        //   y: "56%",
        //   z: 8,
        //   textAlign: "center",
        //   textStyle: {
        //     color: "#999",
        //     fontSize: 12,
        //     fontWeight: "normal",
        //   },
        // },
        tooltip: {
          show: false,
          backgroundColor: "rgba(5, 187, 76,0.35)",
          borderColor: "#05bb4c",
          formatter: function (params) {
            var htmlStr = `<div style='font-size:0.486vw;color:#fff;'>${title}： ${params.value} ${unit} </div>`;

            return htmlStr;
          },
        },
        series: [
          // 进度条
          {
            z: 1,
            name: "内部（环形）进度条",
            type: "gauge",
            radius: "100%",
            center: ["50%", "60%"],
            splitNumber: 5,
            axisLine: {
              lineStyle: {
                color: [
                  [
                    (this.value < 0 ? 0 : this.value) / this.max,
                    new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color: $("#appBody").is(".dark")
                          ? partten.getColor(this.color) + "01"
                          : "rgb(57, 54, 143)",
                      },
                      {
                        offset: 1,
                        color: $("#appBody").is(".dark")
                          ? partten.getColor(this.color) + "ff"
                          : "rgb(57, 54, 143)",
                      },
                    ]),
                  ],
                  [1, "transparent"],
                ],
                width: 6,
              },
            },
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            pointer: {
              show: false,
            },
          },
          // 指针
          {
            name: "指针",
            type: "gauge",
            z: 2,
            center: ["50%", "60%"],
            min: this.min,
            max: this.max,
            radius: "100%",
            axisLine: {
              show: false,
            },
            tooltip: {
              show: false,
            },
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            detail: {
              show: false,
            },
            title: {
              //标题
              show: false,
            },
            data: [
              {
                value: this.value < 0 ? 0 : this.value,
              },
            ],
            itemStyle: {
              normal: {
                color: $("#appBody").is(".dark") ? "#fff" : "#000",
              },
            },
            pointer: {
              show: true,
              length: "35%",
              radius: "100%",
              width: 1, //指针粗细
              offsetCenter: ["0%", "-40%"],
            },
            animationDuration: 0,
          },
          // 刻度
          {
            z: 1,
            name: "外部刻度",
            type: "gauge",
            center: ["50%", "60%"],
            radius: "100%",
            min: this.min, //最小刻度
            max: this.max, //最大刻度
            splitNumber: 10, //刻度数量
            startAngle: 225,
            endAngle: -45,
            axisLine: {
              show: true,
              // 仪表盘刻度线
              lineStyle: {
                width: 1,
                color: [
                  [
                    1,

                    $("#appBody").is(".dark")
                      ? partten.getColor(this.color)
                      : "rgb(57, 54, 143)",
                  ],
                ],
              },
            },
            //仪表盘文字
            axisLabel: {
              show: false,
            },
            //刻度标签。
            axisTick: {
              show: true,
              distance:2,
              splitNumber:3,
              lineStyle: {
                color: partten.getColor(this.color),
                width: 1,
              },
              length: 4,
            }, //刻度样式
            splitLine: {
              show: false,
            }, //分隔线样式
            detail: {
              show: false,
            },
            pointer: {
              show: false,
            },
          },
          // 显示数字
          {
            type: "pie",
            // radius: ["0", "80%"],
            center: ["50%", "60%"],
            z: 8,
            animation: false,
            hoverAnimation: false,
            data: [
              {
                value: this.value < 0 ? 0 : this.value,
                itemStyle: {
                  normal: {
                    color: "transition",
                  },
                },
                label: {
                  normal: {
                    formatter: function (params) {
                      return params.value;
                    },
                    color: $("#appBody").is(".dark")
                      ? partten.getColor(this.color)
                      : "rgb(57, 54, 143)",
                    fontSize: 12,
                    fontWeight: "bold",
                    position: "center",
                    show: true,
                  },
                },
                labelLine: {
                  show: false,
                },
              },
            ],
          },
          {
            name: "内部阴影",
            type: "gauge",
            radius: "95%",
            center: ["50%", "60%"],
            splitNumber: 5,
            axisLine: {
              lineStyle: {
                color: [
                  // [
                  //   1,
                  //   new echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  //     {
                  //       offset: 0,
                  //       color: "rgba(45,230,150,0)",
                  //     },
                  //     {
                  //       offset: 0.5,
                  //       color: "rgba(45,230,150,0.2)",
                  //     },
                  //     {
                  //       offset: 1,
                  //       color: "rgba(45,230,150,1)",
                  //     },
                  //   ]),
                  // ],
                  [
                    1,
                    this.color == "green"
                      ? "rgba(45,230,150,0.1)"
                      : "rgba(255, 131, 0, 0.1)",
                  ],
                  // [1, "rgba(45,230,150,0)"],
                ],
                width: 3,
              },
            },
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            itemStyle: {
              show: false,
            },
          },
          // 光环
          {
            type: "pie",
            radius: "55%",
            center: ["50%", "60%"],
            animationType: "scale",
            animation: false,
            label: {
              show: false,
            },
            labelLine: {
              show: false,
            },
            emphasis: {
              scale: false,
            },
            data: [
              {
                value: this.value,
                itemStyle: {
                  color: "transparent",
                  borderColor: $("#appBody").is(".dark")
                    ? partten.getColor(this.color)
                    : "rgb(57, 54, 143)",
                  opacity: 0.3,
                  shadowColor: $("#appBody").is(".dark")
                    ? partten.getColor(this.color)
                    : "rgb(57, 54, 143)",
                  shadowBlur: 20,
                },
              },
            ],
          },
        ],
      };

      chart.clear();
      chart.setOption(option);

      this.resize = function () {
        chart.resize();
      };

      window.removeEventListener("resize", this.resize);
      window.addEventListener("resize", this.resize);
    },
  },
  created() {
    this.id = "pie-chart-" + util.newGUID();
  },
  mounted() {
    this.$el.style.width = this.width;
    this.$el.style.height = this.height;
    this.initChart();
  },
  updated() {
    this.initChart();
  },
  unmounted() {
    window.removeEventListener("resize", this.resize);
  },

  watch: {
    "$store.state.themeName"() {
      this.initChart();
    },
  },
};
</script>

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