transitCharts.vue 5.4 KB
<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>