Chars.vue 3.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 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
<!--
 * @Author: your name
 * @Date: 2022-04-11 17:13:13
 * @LastEditTime: 2022-04-20 13:48:45
 * @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> -->
    <!-- 如果是默认值,就显示这个 -->
    <div v-if="!options" class="text" :style="{ color: color }">
      <span class="num">0</span><span class="fh">%</span>
    </div>
    <!-- </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: {
              // normal: {
              show: false,
              position: "center",
              // },
            },
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
            },
            data: [
              {
                value: 0,
                name: "未完成",
              },
              {
                value: 100,
                name: "完成",
              },
            ],
          },
        ],
      },
    };
  },
  mounted() {
    // if (!this.options) {
    this.charsInit();
    // }
  },
  watch: {
    options: {
      handler: function (val, oldVal) {
        console.log("val", val);
        this.charsInit();
      },
      deep: true,
    },
  },
  methods: {
    charsInit() {
      if (!this.myChats) {
        this.myChats = this.$echarts.init(this.$refs.chars);
      }

      let options;
      // 如果传进来的options就用传进来的,否则就用默认的
      if (this.options!=null) {
        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>