UserStats.vue
3.66 KB
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
<template>
<div class="user-stats">
<div class="user male">
<div class="avatar male"></div>
<span>男</span>
<div class="progress">
<div class="fill male" :style="styleObject">{{
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: {
styleObject() {
// 基本的宽度样式
let widthStyle = this.malePercentage === 0 ? '50%' : `${this.malePercentage}%`;
// 计算marginLeft
let marginLeftStyle;
if (this.malePercentage > 30 && this.malePercentage < 50) {
marginLeftStyle = `${this.malePercentage}%`;
} else if (this.malePercentage > 0 && this.malePercentage < 30) {
marginLeftStyle = `${this.malePercentage * 2}%`;
} else {
marginLeftStyle = `${this.malePercentage / 4}%`;
}
// 返回样式对象
return {
width: widthStyle,
'min-width': '40%',
'max-width': '50%',
'margin-left': marginLeftStyle,
};
},
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>