<template>
  <div :id="health" class="dashboard"></div>
</template>

<script>
import uuidv1 from "uuid/v1";
var echarts = require("echarts");
export default {
  props: {
    data: {
      type: Object,
      default: () => {
        return {
          num: 0,
          color: "#e15260",
          text: "敏感"
        };
      }
    }
  },
  components: {},
  data() {
    return {
      health: uuidv1()
    };
  },
  watch: {
    data: {
      handler(val) {
        console.log(val);
        this.init_health(this.data.num, this.data.color, this.data.text);
      }
    }
  },
  computed: {},
  created() {},
  mounted() {
    this.init_health(this.data.num, this.data.color, this.data.text);
  },
  methods: {
    init_health(num, color, text) {
      var options = {
        title: {
          text: num + "%",
          textStyle: {
            color: "#000",
            fontSize: 26,
            fontWeight: 200
          },
          subtext: text,
          subtextStyle: {
            color: "#707693",
            fontSize: 12
          },
          itemGap: 0, // 主副标题距离
          left: "center",
          top: "center"
        },
        angleAxis: {
          max: 100, // 满分
          clockwise: false, // 逆时针
          startAngle: 90,
          // 隐藏刻度线
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: false
          },
          splitLine: {
            show: false
          }
        },
        radiusAxis: {
          type: "category",
          // 隐藏刻度线
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            show: false
          },
          splitLine: {
            show: false
          }
        },
        polar: {
          center: ["50%", "50%"],
          radius: "140%" //图形大小
        },
        series: [
          {
            type: "bar",
            silent: true,
            data: [
              {
                name: "作文得分",
                value: num,
                itemStyle: {
                  normal: {
                    color: color,
                    shadowColor: "rgba(0, 0, 0, 0.2)",
                    shadowBlur: 20
                  }
                }
              }
            ],
            coordinateSystem: "polar",
            roundCap: true,
            barWidth: 8,
            barGap: "-100%", // 两环重叠

            z: 2
          },
          {
            // 灰色环
            type: "bar",
            silent: true,
            data: [
              {
                value: 100,
                itemStyle: {
                  color: "#c9cedd",
                  shadowColor: "rgba(0, 0, 0, 0.2)",
                  shadowBlur: 20
                }
              }
            ],
            coordinateSystem: "polar",
            roundCap: true,
            barWidth: 8,
            barGap: "-100%", // 两环重叠
            z: 1
          }
        ]
      };
      window[this.health] = echarts.init(document.getElementById(this.health));
      window[this.health].setOption(options, true);
    }
  }
};
</script>

<style scoped>
.dashboard {
  width: 100%;
  height: 100%;
}
</style>
