transitCharts.vue
5.4 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
<template>
<div class="dashboard-editor-container">
<el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
<line-chart :chart-data="lineChartData" />
</el-row>
<el-row :gutter="8">
<el-col :xs="{span: 6}" :sm="{span: 6}" :md="{span: 6}" :lg="{span: 6}" :xl="{span: 6}" style="margin-bottom:30px;">
<todo-list />
</el-col>
<el-col :xs="{span: 18}" :sm="{span: 18}" :md="{span: 18}" :lg="{span: 18}" :xl="{span: 18}" style="margin-bottom:30px;background:#fff">
<week-line-chart :chart-data="lineChartData" />
</el-col>
</el-row>
</div>
</template>
<script>
import LineChart from './dashboard/YearLineChart'
import WeekLineChart from './dashboard/WeekLineChart'
import TodoList from './dashboard/TodoList'
import {analysisAPI} from "@/api/trn";
export default {
name: 'transDashboard',
components: {
LineChart,
TodoList,
WeekLineChart
},
data() {
return {
lineChartData: {
lastWeek:{totalCount:[],totalWeight:[]},
thisWeek:{totalCount:[],totalWeight:[]},
lastYear:{totalCount:[],totalWeight:[]},
thisYear:{totalCount:[],totalWeight:[]}
}
}
},
methods: {
getAnalysis:function () {
const loading = this.$loading({
lock: true,
text: '正在加载统计数据',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.6)'
});
analysisAPI().then(res =>{
const result = res.data;
if (result.code === '200') {
this.lineChartData = this.formatAnalysisData(result)
this.$message.success('获取数据成功')
} else {
this.$message.error('获取数据成功失败,请稍后重试')
}
}).catch(err => {
this.$message.error(err.toString())
}).finally(()=>{
loading.close()
})
},
formatAnalysisData:function transformData(originalData) {
const analysisData = {
thisWeek: { totalWeight: [], totalCount: [] },
lastWeek: { totalWeight: [], totalCount: [] },
lastYear: { totalWeight: [], totalCount: [] },
thisYear: { totalWeight: [], totalCount: [] },
};
originalData.data.forEach(item => {
switch (item.type) {
case 'thisWeek':
analysisData.thisWeek.totalWeight.push(item.totalWeight);
analysisData.thisWeek.totalCount.push(item.totalCount);
break;
case 'lastWeek':
analysisData.lastWeek.totalWeight.push(item.totalWeight);
analysisData.lastWeek.totalCount.push(item.totalCount);
break;
case 'lastYear':
analysisData.lastYear.totalWeight.push(item.totalWeight);
analysisData.lastYear.totalCount.push(item.totalCount);
break;
case 'thisYear':
analysisData.thisYear.totalWeight.push(item.totalWeight);
analysisData.thisYear.totalCount.push(item.totalCount);
break;
default:
console.warn(`Unknown type encountered: ${item.type}`);
}
});
// Ensure that each array in 'lastYear' and 'thisYear' has 12 elements for consistency.
// Fill missing months with zeros if necessary.
// const months = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
// months.forEach(month => {
// if (!analysisData.lastYear[month]) {
// analysisData.lastYear[month] = { totalWeight: Array(12).fill(0), totalCount: Array(12).fill(0) };
// }
// if (!analysisData.thisYear[month]) {
// analysisData.thisYear[month] = { totalWeight: Array(12).fill(0), totalCount: Array(12).fill(0) };
// }
// });
console.log(JSON.stringify(analysisData))
return analysisData;
}
},
mounted:function () {
this.getAnalysis();
}
}
</script>
<style lang="scss" scoped>
.dashboard-editor-container {
padding: 32px;
background-color: rgb(240, 242, 245);
position: relative;
.github-corner {
position: absolute;
top: 0px;
border: 0;
right: 0;
}
.chart-wrapper {
background: #fff;
padding: 16px 16px 0;
margin-bottom: 32px;
}
}
@media (max-width:1024px) {
.chart-wrapper {
padding: 6px;
}
}
</style>