DemuMesDataV/components/capsuleChart/index.vue

168 lines
3.1 KiB
Vue
Raw Normal View History

2018-12-05 18:31:36 +08:00
<template>
<div class="capsule-chart">
2018-12-07 15:50:25 +08:00
2019-01-16 16:56:11 +08:00
<loading v-if="!status" />
2018-12-07 15:50:25 +08:00
<template v-else>
<div class="label-column">
2019-01-16 16:56:11 +08:00
<div v-for="item in data.series" :key="item.title">{{ item.title }}</div>
2018-12-07 15:50:25 +08:00
<div>&nbsp;</div>
</div>
<div class="capsule-container">
<div class="capsule-item" v-for="(capsule, index) in capsuleData" :key="index">
2019-01-16 16:56:11 +08:00
<div :style="`width: ${capsule * 100}%; background-color: ${drawColors[index % drawColors.length]};`"></div>
2018-12-07 15:50:25 +08:00
</div>
<div class="unit-label">
<div class="unit-container">
2018-12-12 18:46:49 +08:00
<div v-for="(unit, index) in unitData" :key="unit + index">{{ unit }}</div>
2018-12-07 15:50:25 +08:00
</div>
<div class="unit-text">单位</div>
</div>
</div>
2018-12-12 18:46:49 +08:00
<div class="for-solt">
<slot></slot>
</div>
2018-12-07 15:50:25 +08:00
</template>
2018-12-05 18:31:36 +08:00
</div>
</template>
<script>
2019-01-16 16:56:11 +08:00
import colorsMixin from '../../mixins/colorsMixin.js'
2018-12-05 18:31:36 +08:00
export default {
2018-12-07 15:50:25 +08:00
name: 'CapsuleChart',
2019-01-16 16:56:11 +08:00
props: ['data', 'colors'],
mixins: [colorsMixin],
2018-12-07 15:50:25 +08:00
data () {
return {
2019-01-16 16:56:11 +08:00
status: false,
2018-12-07 15:50:25 +08:00
capsuleData: [],
unitData: []
}
},
watch: {
data () {
const { init } = this
init()
}
},
methods: {
init () {
2019-01-16 16:56:11 +08:00
const { checkData, initColors, calcCapsuleAndUnitData } = this
initColors()
checkData() && calcCapsuleAndUnitData()
},
checkData () {
const { data } = this
this.status = false
if (!data || !data.series) return false
2018-12-07 15:50:25 +08:00
2019-01-16 16:56:11 +08:00
this.status = true
2018-12-07 15:50:25 +08:00
2019-01-16 16:56:11 +08:00
return true
2018-12-07 15:50:25 +08:00
},
calcCapsuleAndUnitData () {
2019-01-16 16:56:11 +08:00
const { data: { series } } = this
2018-12-07 15:50:25 +08:00
2019-01-16 16:56:11 +08:00
const capsuleData = series.map(({ value }) => value)
2018-12-07 15:50:25 +08:00
const maxValue = Math.max(...capsuleData)
2018-12-12 18:46:49 +08:00
this.capsuleData = capsuleData.map(v => maxValue ? v / maxValue : 0)
2018-12-07 15:50:25 +08:00
const oneSixth = maxValue / 5
this.unitData = new Array(6).fill(0).map((v, i) => Math.ceil(i * oneSixth))
}
},
mounted () {
const { init } = this
init()
}
2018-12-05 18:31:36 +08:00
}
</script>
<style lang="less">
2018-12-07 15:50:25 +08:00
.capsule-chart {
position: relative;
display: flex;
flex-direction: row;
box-sizing: border-box;
padding: 10px;
2019-01-16 16:56:11 +08:00
color: #fff;
2018-12-07 15:50:25 +08:00
.label-column {
display: flex;
flex-direction: column;
justify-content: space-between;
box-sizing: border-box;
padding-right: 10px;
text-align: right;
2018-12-11 14:52:12 +08:00
font-size: 12px;
2018-12-07 15:50:25 +08:00
div {
height: 20px;
line-height: 20px;
}
}
.capsule-container {
flex: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.capsule-item {
2018-12-11 14:52:12 +08:00
box-shadow: 0 0 3px #999;
2018-12-07 15:50:25 +08:00
height: 10px;
margin: 5px 0px;
border-radius: 5px;
width: calc(~"100% - 50px");
div {
height: 8px;
margin-top: 1px;
border-radius: 5px;
transition: all 0.3s;
}
}
.unit-label {
display: flex;
flex-direction: row;
line-height: 20px;
2018-12-11 14:52:12 +08:00
font-size: 12px;
2018-12-07 15:50:25 +08:00
}
.unit-text {
width: 40px;
text-align: right;
}
.unit-container {
flex: 1;
display: flex;
flex-direction: row;
justify-content: space-between;
}
2018-12-12 18:46:49 +08:00
.for-solt {
position: absolute;
width: 100%;
height: 100%;
top: 0px;
left: 0px;
}
2018-12-07 15:50:25 +08:00
}
2018-12-05 18:31:36 +08:00
</style>