feat:增加账本统计模块

This commit is contained in:
2025-10-30 23:02:55 +08:00
parent 9e0b8ead79
commit 355a6c256d
7 changed files with 140 additions and 6 deletions

View File

@@ -0,0 +1,24 @@
import { ILedgerRecord } from '@/types/ledger.ts'
import { IStatsChart } from 'vue3-common/types'
export const statsByField = (records: ILedgerRecord[],
field: keyof ILedgerRecord): IStatsChart[] => {
const statsMap = new Map<string, number>()
records.forEach((record) => {
const fieldValue = record[field] as string
const filedStats = record.totalPrice as number
const currentTotal = statsMap.get(fieldValue) || 0
// 计算当前字段对应的统计数据
statsMap.set(fieldValue, currentTotal + filedStats)
})
return Array.from(statsMap.entries())
.map(([name, value]) => ({
name,
value: Number(value.toFixed(2))
}))
.sort((a, b) => b.value - a.value)
.slice(0, 5)
}