Chars.vue 3.48 KB
Newer Older
纪泽龙's avatar
纪泽龙 committed
1 2 3
<!--
 * @Author: your name
 * @Date: 2022-04-11 17:13:13
纪泽龙's avatar
纪泽龙 committed
4
 * @LastEditTime: 2022-04-20 13:48:45
纪泽龙's avatar
纪泽龙 committed
5 6 7 8 9 10 11 12
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /gassafety-progress/gassafetyprogress-web/src/components/allCharsCom/Chars.vue
-->
<template>
  <div class="my-chars">
    <div ref="chars" class="chars"></div>
    <!-- <slot> -->
纪泽龙's avatar
纪泽龙 committed
13 14 15 16
    <!-- 如果是默认值,就显示这个 -->
    <div v-if="!options" class="text" :style="{ color: color }">
      <span class="num">87</span><span class="fh">%</span>
    </div>
纪泽龙's avatar
纪泽龙 committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    <!-- </slot> -->
  </div>
</template>

<script>
export default {
  props: {
    options: {
      type: Object,
      default: null,
    },
    // 默认饼图的颜色
    color: {
      tupe: String,
      default: "#399AF4",
    },
  },
  data() {
    return {
      myChats: null,
      // 默认options
      defaultOptions: {
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)",
        },
        color: ["#ffffff", "#399AF4"],
        // title: {
        //   text: "资产总数",
        //   left: "center",
        //   top: "50%",
        //   textStyle: {
        //     textAlign: "center",
        //     fill: "#333",
        //     fontSize: 14,
        //     fontWeight: 400,
        //   },
        // },
        graphic: {
          type: "text",
          left: "center",
          top: "45%",
          style: {
            text: "",
            textAlign: "center",
            fill: "#399AF4",
            fontSize: 18,
          },
        },
        series: [
          {
            name: "完成情况",
            type: "pie",
            radius: ["62%", "80%"],
            avoidLabelOverlap: false,
            // hoverAnimation: false,
            label: {
纪泽龙's avatar
纪泽龙 committed
74 75 76 77
              // normal: {
              show: false,
              position: "center",
              // },
纪泽龙's avatar
纪泽龙 committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            },
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
            },
            data: [
              {
                value: 13,
                name: "未完成",
              },
              {
                value: 87,
                name: "完成",
              },
            ],
          },
        ],
      },
    };
  },
  mounted() {
    // if (!this.options) {
纪泽龙's avatar
纪泽龙 committed
100
    this.charsInit();
纪泽龙's avatar
纪泽龙 committed
101 102
    // }
  },
纪泽龙's avatar
纪泽龙 committed
103 104 105 106 107 108 109 110 111
  watch: {
    options: {
      handler: function (val, oldVal) {
        console.log("val", val);
        this.charsInit();
      },
      deep: true,
    },
  },
纪泽龙's avatar
纪泽龙 committed
112 113
  methods: {
    charsInit() {
纪泽龙's avatar
纪泽龙 committed
114 115 116 117
      if (!this.myChats) {
        this.myChats = this.$echarts.init(this.$refs.chars);
      }

纪泽龙's avatar
纪泽龙 committed
118 119
      let options;
      // 如果传进来的options就用传进来的,否则就用默认的
纪泽龙's avatar
纪泽龙 committed
120
      if (this.options!=null) {
纪泽龙's avatar
纪泽龙 committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        options = this.options;
      } else {
        options = JSON.parse(JSON.stringify(this.defaultOptions));
        options.color[1] = this.color;
      }
      this.myChats.setOption(options);
    },
  },
};
</script>

<style lang="scss" scoped>
.my-chars {
  width: 100%;
  height: 100%;
  position: relative;
  // background-color: blue;
}

.chars {
  width: 100%;
  height: 100%;
  // background: yellow;
}
.text {
  position: absolute;
  width: 80px;
  height: 40px;
  top: 50%;
  left: 50%;
  margin-left: -40px;
  margin-top: -10px;
  text-align: center;
  color: #399af4;

  .num {
    font-size: 21px;
    letter-spacing: 0px;
  }
  .fh {
    font-size: 1px;
  }
}
</style>