优化记录页趋势分析展示

This commit is contained in:
2026-07-29 14:46:38 +08:00
parent 19b0c4d4c8
commit fca3e32d04
8 changed files with 283 additions and 6 deletions
+55
View File
@@ -115,12 +115,65 @@ const getTrendData = (recordGroups, mode, now = new Date()) => {
})
}
const getBestTrendData = (recordGroups, mode, now = new Date()) => {
const peaks = {}
const groups = recordGroups && typeof recordGroups === 'object' ? recordGroups : {}
Object.values(groups).forEach((records) => {
if (!Array.isArray(records)) return
records.forEach((record) => {
const date = _dateOnly(record && record.date)
if (!date) return
const key = mode === 'month' ? date.substring(0, 7) : date
peaks[key] = Math.max(peaks[key] || 0, _normalizeDuration(record.duration))
})
})
const today = new Date(now)
const weekdays = ['日', '一', '二', '三', '四', '五', '六']
const pad = (n) => String(n).padStart(2, '0')
const count = mode === 'month' ? 6 : 7
return Array.from({ length: count }, (_, index) => {
const offset = count - 1 - index
const date = mode === 'month'
? new Date(today.getFullYear(), today.getMonth() - offset, 1)
: new Date(today.getFullYear(), today.getMonth(), today.getDate() - offset)
const key = mode === 'month'
? `${date.getFullYear()}-${pad(date.getMonth() + 1)}`
: `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}`
const value = peaks[key] || 0
return {
label: mode === 'month' ? `${date.getMonth() + 1}` : offset === 0 ? '今天' : weekdays[date.getDay()],
value,
valueText: value > 0 ? _formatTrendDuration(value) : '',
highlight: offset === 0
}
})
}
const getBarPercent = (value, max) => {
const amount = _normalizeDuration(value)
const maximum = _normalizeDuration(max)
return maximum > 0 ? amount / maximum * 100 : 0
}
const getTrendSummary = (trendData, mode) => {
const items = Array.isArray(trendData) ? trendData : []
const total = items.reduce((sum, item) => sum + _normalizeDuration(item && item.value), 0)
const activeItems = items.filter(item => _normalizeDuration(item && item.value) > 0)
const peak = activeItems.reduce((current, item) => {
return !current || _normalizeDuration(item.value) > _normalizeDuration(current.value) ? item : current
}, null)
const isMonth = mode === 'month'
return {
totalText: _formatTrendDuration(total),
activeCount: activeItems.length,
activeLabel: isMonth ? '活跃月份' : '训练天数',
peakTitle: isMonth ? '最高训练月' : '最高训练日',
peakText: peak ? `${peak.label} · ${_formatTrendDuration(peak.value)}` : '暂无训练数据'
}
}
/**
* Convert a local file path (typically `wxfile://...` from chooseAvatar) to a
* base64 data URI. Used to make temporary file URLs survive the WeChat
@@ -169,6 +222,8 @@ module.exports = {
formatDate,
dateOnly: _dateOnly,
getTrendData,
getBestTrendData,
getTrendSummary,
getBarPercent,
fileToDataURI
}