<!--
 * @Author: your name
 * @Date: 2021-10-14 10:48:56
 * @LastEditTime: 2021-11-09 13:16:07
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /data-show/src/views/WeiboDetails/weiboCharacters/index.vue
-->
<template>
  <div class="wc-outter" v-loading="load">
    <v-label-div title="微博人物画像">
      <div>
        <v-tab-group
          :btns="['性别', '认证', '地区']"
          @change="handlerTab"
        ></v-tab-group>
      </div>
    </v-label-div>
    <div class="wc-inner">
      <div class="d1">
        <v-echarts :opt="opt"></v-echarts>
      </div>
      <div class="d2">
        <div v-if="labelArr.length<=3">
          <v-label-ctx
            v-for="(item, index) in labelArr"
            :key="index"
            :label="item.key"
            :cont="item.value"
            :percentage="((item.value / total) * 100).toFixed(2) + '%'"
            :color="colors[index]"
            :eStyle="{ height: '8.54rem' }"
          ></v-label-ctx>
        </div>
        <vue-scroll v-if="labelArr.length>3">
          <v-label-ctx
            v-for="(item, index) in labelArr"
            :key="index"
            :label="item.key"
            :cont="item.value"
            :percentage="((item.value / total) * 100).toFixed(2) + '%'"
            :color="colors[index]"
            :eStyle="{ height: '5.14rem' }"
          ></v-label-ctx>
        </vue-scroll>
      </div>
    </div>
  </div>
</template>

<script>
import { getSexMergeWeiBo } from "@/api/WeiboDetails";
import createOpt from "./opt";
export default {
  name: "weiboCharacters",
  data() {
    return {
      load: false,
      form: {
        sBrand: "",
        token: "",
      },
      RegionWeiBo: [],
      attestation: [],
      sex: [],
      total: 0,
      labelArr: [],

      opt: createOpt(),
      colors: [
        "#54BF93",
        "#3373CC",
        "#CC9D12",
        "#f15c80",
        "#e4d354",
        "#8085e8",
        "#8d4653",
        "#91e8e1",
        "#f7a35c",
        "#90ed7d",
        "#54BF93",
        "#3373CC",
        "#CC9D12",
        "#f15c80",
        "#e4d354",
        "#8085e8",
        "#8d4653",
        "#91e8e1",
        "#f7a35c",
        "#90ed7d",
      ],
    };
  },
  created() {
    this.form.token = this.getToken;
    this.form.sBrand = this.getBrand.brandname || '奥迪';
    this.getData();
  },
  methods: {
    // 获取后台数据
    getData() {
      let obj = Object.assign({}, this.getCtime2, this.form);
      this.load = true;
      getSexMergeWeiBo(obj).then((res) => {
        let data = res.data || {};
        let RegionWeiBo = data.RegionWeiBo;
        let attestation = data.attestation;
        let sex = data.sex;
        this.RegionWeiBo = this.toArr(RegionWeiBo);
        this.attestation = this.toArr(attestation);
        this.sex = this.toArr(sex);
        this.doVal(this.sex);
        this.load = false;
      });
    },
    // 将对象变成数组
    toArr(obj) {
      let arr = [];
      for (let key in obj) {
        let o = {
          key: key,
          value: obj[key],
        };
        arr.push(o);
      }
      return arr;
    },
    // 给页面变化值赋值
    doVal(arr = []) {
      let total = 0;
      arr.forEach((ele) => {
        total += ele.value * 1;
      });
      this.total = total;
      this.labelArr = arr;
      this.opt = createOpt(this.labelArr, this.colors);
    },
    // 切换数据
    handlerTab(n) {
      switch (n) {
        case 0:
          this.doVal(this.sex);
          break;
        case 1:
          this.doVal(this.attestation);
          break;
        case 2:
          this.doVal(this.RegionWeiBo);
          break;
        default:
          this.doVal(this.sex);
          break;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.wc-outter {
  width: 618px;
  height: 460px;
  border: 2px solid #0f2a4d;
  margin-left: 16px;
  overflow: hidden;
  .wc-inner {
    width: 100%;
    height: calc(100% - 48px);
    display: flex;
    justify-content: flex-start;
    .d1 {
      width: 280px;
      height: 100%;
    }
    .d2 {
      width: 300px;
      margin-left: 16px;
    }
  }
}
</style>

