修复趋势图表加载与比例展示
This commit is contained in:
@@ -168,46 +168,7 @@ Page({
|
||||
* 返回 [{ label, value, valueText, highlight }] 供 trend-chart 渲染。
|
||||
*/
|
||||
_computeTrend(mode) {
|
||||
const allRecords = Object.values(storage.getRecords()).flat()
|
||||
const today = new Date()
|
||||
const weekdays = ['日', '一', '二', '三', '四', '五', '六']
|
||||
const pad = (n) => String(n).padStart(2, '0')
|
||||
|
||||
if (mode === 'month') {
|
||||
const data = []
|
||||
for (let i = 5; i >= 0; i--) {
|
||||
const d = new Date(today.getFullYear(), today.getMonth() - i, 1)
|
||||
const monthKey = `${d.getFullYear()}-${pad(d.getMonth() + 1)}`
|
||||
const monthRecords = storage.getRecordsByMonth(monthKey) || []
|
||||
const total = monthRecords.reduce((s, r) => s + (Number(r.duration) || 0), 0)
|
||||
data.push({
|
||||
label: `${d.getMonth() + 1}月`,
|
||||
value: total,
|
||||
valueText: total > 0 ? util.formatDuration(total) : '',
|
||||
highlight: i === 0
|
||||
})
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// default: week (近 7 天)
|
||||
const data = []
|
||||
for (let i = 6; i >= 0; i--) {
|
||||
const d = new Date(today)
|
||||
d.setDate(d.getDate() - i)
|
||||
const dateStr = `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`
|
||||
const total = allRecords
|
||||
.filter((r) => util.dateOnly(r.date) === dateStr)
|
||||
.reduce((s, r) => s + (Number(r.duration) || 0), 0)
|
||||
const isToday = i === 0
|
||||
data.push({
|
||||
label: isToday ? '今天' : weekdays[d.getDay()],
|
||||
value: total,
|
||||
valueText: total > 0 ? util.formatDuration(total) : '',
|
||||
highlight: isToday
|
||||
})
|
||||
}
|
||||
return data
|
||||
return util.getTrendData(storage.getRecords(), mode)
|
||||
},
|
||||
|
||||
onDayTap(e) {
|
||||
|
||||
Reference in New Issue
Block a user