<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: "标题",
    },
    // 值
    value: {
      type: Number,
      default: 42.1,
    },
    // 最小值
    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 option = {
        title: {
          show: true,
          text: this.title,
          x: "45%",
          y: "80%",
          z: 8,
          textAlign: "center",
          textStyle: {
            color: $("#appBody").is(".dark")
              ? partten.getColor("gray")
              : "#000",
            fontSize: util.vh(12),
            fontWeight: "normal",
          },
        },
        series: [
          // 进度条
          {
            z: 1,
            name: "内部（环形）进度条",
            type: "gauge",
            radius: "100%",
            splitNumber: 5,
            axisLine: {
              lineStyle: {
                color: [
                  [
                    this.value / this.max,
                    new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                      {
                        offset: 0,
                        color: $("#appBody").is(".dark")
                          ? partten.getColor(this.color) + "01"
                          : partten.getColor('deepblue') + "01",
                      },
                      {
                        offset: 1,
                        color: $("#appBody").is(".dark")
                          ? partten.getColor(this.color) + "ff"
                          : partten.getColor('deepblue') + "ff",
                      },
                    ]),
                  ],
                  [1, "transparent"],
                ],
                width: util.vh(12),
              },
            },
            axisLabel: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              show: false,
            },
            pointer: {
              show: false,
            },
          },
          // 指针
          {
            name: "指针",
            type: "gauge",
            z: 2,
            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,
              },
            ],
            itemStyle: {
              normal: {
                color: $("#appBody").is(".dark") ? "#fff" : "#000",
              },
            },
            pointer: {
              show: true,
              length: "35%",
              radius: "20%",
              width: 1, //指针粗细
              offsetCenter: ["0%", "-40%"],
            },
            animationDuration: 0,
          },
          // 刻度
          {
            name: "外部刻度",
            type: "gauge",
            //  center: ['20%', '50%'],
            radius: "100%",
            min: this.min, //最小刻度
            max: this.max, //最大刻度
            splitNumber: 10, //刻度数量
            startAngle: 225,
            endAngle: -45,
            axisLine: {
              show: true,
              // 仪表盘刻度线
              lineStyle: {
                width: util.vh(1),
                color: [
                  [
                    1,
                    $("#appBody").is(".dark")
                      ? partten.getColor(this.color)
                      : "rgb(57, 54, 143)",
                  ],
                ],
              },
            },
            //仪表盘文字
            axisLabel: {
              show: false,
            },
            //刻度标签。
            axisTick: {
              show: true,
              distance: 6,
              splitNumber: 2,
              lineStyle: {
                color: $("#appBody").is(".dark")
                  ? partten.getColor("green")
                  : "rgb(57, 54, 143)", //用颜色渐变函数不起作用
                width: util.vh(1),
              },
              length: util.vh(4),
            }, //刻度样式
            splitLine: {
              show: false,
            }, //分隔线样式
            detail: {
              show: false,
            },
            pointer: {
              show: false,
            },
          },
          // 显示数字
          {
            type: "pie",
            radius: ["0", "40%"],
            center: ["50%", "50%"],
            z: 8,
            animation: false,
            hoverAnimation: false,
            data: [
              {
                value: 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: util.vh(12),
                    fontWeight: "bold",
                    position: "center",
                    show: true,
                  },
                },
                labelLine: {
                  show: false,
                },
              },
            ],
          },
          {
            name: "内部阴影",
            type: "gauge",
            radius: "90%",
            splitNumber: 10,
            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,
                    $("#appBody").is(".dark")
                      ? "rgba(45,230,150,0.1)"
                      : "rgba(57, 54, 143, 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: "40%",
            center: ["50%", "50%"],
            animationType: "scale",
            animation: false,
            label: {
              show: false,
            },
            labelLine: {
              show: false,
            },
            emphasis: {
              scale: false,
            },
            data: [
              {
                value: 1,
                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: util.vh(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>
