feat:增加账本统计模块
This commit is contained in:
24
src/utils/home/ledgerUtil.ts
Normal file
24
src/utils/home/ledgerUtil.ts
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user