<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: "dsah-pie",
  componentName: "dsah-pie",
  props: {
    // 宽度 默认9.722vh
    width: {
      type: String,
      default: "70%",
    },
    // 高度 默认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: partten.getColor("gray"),
            fontSize: util.vh(14),
            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: partten.getColor(this.color) + "01",
                      },
                      {
                        offset: 1,
                        color: partten.getColor(this.color) + "ff",
                      },
                    ]),
                  ],
                  [1, "transparent"],
                ],
                width: util.vh(16),
              },
            },
            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: "#fff",
              },
            },
            pointer: {
              show: true,
              length: "35%",
              radius: "20%",
              width: util.vh(2), //指针粗细
              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, partten.getColor(this.color)]],
              },
            },
            //仪表盘文字
            axisLabel: {
              show: false,
            },
            //刻度标签。
            axisTick: {
              show: true,
              distance: 6,
              splitNumber: 2,
              lineStyle: {
                color: "#fff", //用颜色渐变函数不起作用
                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,
            hoverAnimation: false,
            data: [
              {
                value: this.value,
                itemStyle: {
                  normal: {
                    color: "transition",
                  },
                },
                label: {
                  normal: {
                    formatter: function(params) {
                      return params.value;
                    },
                    color: partten.getColor(this.color),
                    fontSize: util.vh(16),
                    fontWeight: "bold",
                    position: "center",
                    show: true,
                  },
                },
                labelLine: {
                  show: false,
                },
              },
            ],
          },
          // 光环
          {
            type: "pie",
            radius: "40%",
            center: ["50%", "50%"],
            animationType: "scale",
            animation: false,
            label: {
              show: false,
            },
            labelLine: {
              show: false,
            },
            data: [
              {
                value: 1,
                itemStyle: {
                  normal: {
                    color: "transparent",
                    borderColor: partten.getColor(this.color),
                    opacity: 0.3,
                    shadowColor: partten.getColor(this.color),
                    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);
  },
};
</script>

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