效果图:
父组件向子组件传入data数据:
(以下数据是用的随机数)
list: [
"aa",
"bb",
"cc",
"dd",
"ee",
"ff",
"gg231224342",
].map((e) => ({
address: e,
data: ["吃饭", "睡觉", "耍手机"].map((i, idx) => ({
name: i,
num: Math.floor(Math.random() * 25),
color: ["#ee736f", "#ffd137", "#1cd0f6"][idx],
})),
})),
核心子组件:
<template>
<div>
<div class="top">标题:</div>
<div class="unit" v-for="(i, idx) in data" :key="idx">
<div class="label" :title="i.address">{{ i.address }}</div>
<div class="box">
<div
class="process_unit"
v-for="(t, index) in i.data"
:key="index"
:style="`flex: 0 0 ${(t.num / unitWidth) * 100}%;`"
>
<span>{{ t.num }}</span>
<div
class="p_u"
:style="`background: linear-gradient(to right,${hexToRgba(
t.color,
100
)},${hexToRgba(t.color, 10)});`"
></div>
</div>
</div>
</div>
<div class="footer">
<div v-for="(item, index) in data[0].data" :key="index">
<div class="flag" :style="`background: ${item.color}`"></div>
<span>{{ item.name }}</span>
</div>
</div>
</div>
</template>
<script>
import hexToRgba from "hex-rgba";//颜色格式 hex 转 rgba
export default {
name: "List",
props: {
data: {
type: Array,
required: true,
},
},
created() {
this.hexToRgba = hexToRgba;
},
computed: {
unitWidth() {
let maxTotal = 0;
this.data.map((i) => {
let result = i.data.reduce(
(total, curr) => total + Number(curr.num),
0
);
if (result > maxTotal) {
maxTotal = result;
}
});
return maxTotal;
},
},
mounted() {
console.log("---------------", this.data);
},
};
</script>
<style lang="scss" scoped>
.top {
margin-bottom: 15px;
}
.unit {
display: flex;
align-items: center;
height: 38px;
.label {
flex: 0 0 100px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.box {
display: flex;
flex: 1;
}
.process_unit {
position: relative;
span {
position: absolute;
top: -8px;
left: 50%;
transform: translateY(-50%);
}
.p_u {
height: 12px;
}
}
}
.footer {
display: flex;
justify-content: flex-end;
& > div {
display: flex;
align-items: center;
margin-right: 15px;
.flag {
margin-right: 4px;
height: 6px;
width: 6px;
}
}
}
</style>
以上是不知名大佬写的,我仅记录学习一哈。