UserStats.vue 3.23 KB
<template>
  <div class="user-stats">
    <div class="user male">
      <div class="avatar male"></div>
      <span>男</span>
      <div class="progress">
        <div class="fill male" :style="{ width: malePercentage==0? 50: malePercentage+ '%','min-width':'40%',
        'max-width':'50%',
        'margin-left':(30 <malePercentage && malePercentage  < 50 ? malePercentage:0 <malePercentage && malePercentage <30?
         malePercentage*2 : malePercentage/4) + '%' }">{{
            malePercentage || 0
          }}%
        </div>
      </div>
      <span class="male-count">{{ maleCount }}人</span>
    </div>
    <div class="user female">
      <div class="avatar female"></div>
      <span>女</span>
      <div class="progress">
        <div class="fill female" :style="{ width: femalePercentage== 0?50:femalePercentage + '%','min-width':'30%'}">
          {{ femalePercentage || 0 }}%
        </div>
      </div>
      <span class="female-count">{{ femaleCount }}人</span>
    </div>
  </div>
</template>

<script>

export default {
  name: "UserStats",
  props: {
    maleCount: {
      type: String,
      default: '',
    },
    femaleCount: {
      type: String,
      default: '',
    }
  },
  computed: {
    malePercentage() {
      let percentage = (this.maleCount / (this.maleCount + this.femaleCount)) * 100;

      return (isNaN(percentage) ? 0 : percentage.toFixed(2));

    },
    femalePercentage() {
      if (this.maleCount + this.femaleCount == 0) {
        return 0
      } else {
        return (100 - this.malePercentage).toFixed(2);
      }

    }
  }
};
</script>

<style lang="scss" scoped>
.user-stats {
  display: flex;
  justify-content: center;
  background-image: linear-gradient(180deg, #F5F4FF 0%, #F8FBFF 100%);
  border-radius: 2px;
  padding: 12px;

  .user {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .avatar {
      width: 60px;
      height: 60px;
      border-radius: 50%;

      &.male {
        background: url('./../../../assets/userPortrait/male.png') center/cover no-repeat;
      }

      &.female {
        background: url('./../../../assets/userPortrait/female.png') center/cover no-repeat;
      }
    }

    .progress {
      justify-content: center;
      width: 8vw;
      height: 2.5vh;
      margin: 10px 0;

      .fill {
        height: 100%;
        line-height: 2.5vh;
        color: #FFFFFF;
        font-size: 14px;

        &.male {
          padding-right: 15px;
          padding-left: 15px;
          text-align: left;
          background: #5A64FF;
          border-top-left-radius: 25px; // 左上角圆角
          border-bottom-left-radius: 25px; // 左下角圆角
          clip-path: polygon(0 0, 100% 0%, 85% 100%, 0% 100%); // 右侧切面
        }

        &.female {
          padding-right: 18px;
          padding-left: 10px;
          text-align: right;
          background: #FF5A92;
          border-top-right-radius: 25px;
          border-bottom-right-radius: 25px;
          clip-path: polygon(10% 0, 100% 0%, 100% 100%, 0% 100%); // 左侧切面
        }
      }
    }

    .male-count {
      color: #5A64FF;
      font-size: 14px;
      text-align: left;
    }

    .female-count {
      color: #FF5A92;
      font-size: 14px;
    }
  }
}

</style>