smart-switch-ui/src/views/dashboard/DailyBillReport.vue
2024-09-11 21:23:07 +08:00

147 lines
3.1 KiB
Vue

<template>
<div :class="className" :style="{height:height,width:width}" v-loading="loading"/>
</template>
<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { getTimeArray } from '@/utils'
const animationDuration = 6000
export default {
name: 'dailyBillReport',
mixins: [resize],
props: {
className: {
type: String,
default: 'chart'
},
width: {
type: String,
default: '100%'
},
height: {
type: String,
default: '300px'
},
billData: {
type: Array,
default: () => {
return []
}
}
},
data() {
return {
loading: false,
chart: null,
// x轴配置
x: {
start: 0,
end: 22,
step: 2
},
}
},
watch: {
billData:{
deep: true,
handler(nv, ov) {
this.$nextTick(() => {
this.setOption();
})
}
}
},
mounted() {
this.$nextTick(() => {
this.initChart()
})
},
beforeDestroy() {
if (!this.chart) {
return
}
this.chart.dispose()
this.chart = null
},
methods: {
// 获取分时数据
getHourData(data, key) {
if (data == null) {
return [];
}
// 将数据分组求和
let list = [];
for(let i = this.x.start; i <= this.x.end; i += this.x.step) {
let sum = 0;
data.filter(item => item.createHour >= i && item.createHour < i + this.x.step).forEach(item => {
sum += item[key] == null ? 0 : item[key];
})
list.push(sum.toFixed(2));
}
return list;
},
// 初始化图表
initChart() {
this.chart = echarts.init(this.$el, 'macarons')
this.setOption();
},
// 设置数据
setOption() {
this.chart.setOption({
tooltip: {
trigger: 'axis',
axisPointer: { // 坐标轴指示器,坐标轴触发有效
type: 'shadow' // 默认为直线,可选为:'line' | 'shadow'
}
},
grid: {
top: 10,
left: '2%',
right: '2%',
bottom: '3%',
containLabel: true
},
xAxis: [{
type: 'category',
data: getTimeArray(this.x.start, this.x.end, this.x.step),
axisTick: {
alignWithLabel: true
}
}],
yAxis: [{
type: 'value',
axisTick: {
show: false
}
}],
series: [{
name: '充值(元)',
type: 'line',
data: this.getHourData(this.billData, 'recharge'),
animationDuration,
itemStyle: {
normal: {
color: '#246EFF',
}
},
}, {
name: '提现(元)',
type: 'line',
data: this.getHourData(this.billData, 'withdraw'),
animationDuration,
itemStyle: {
normal: {
color: '#81E2FF',
}
},
}]
})
}
}
}
</script>