Skip to content

Commit 6e7b4f4

Browse files
author
sheepzh
committed
Optimie time unit of habit
1 parent e61bb37 commit 6e7b4f4

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

src/app/components/habit/component/chart/wrapper.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,38 @@ function formatXAxis(ts: number) {
6666

6767
function formatTimeOfEchart(params: any, averageByDate: boolean): string {
6868
const format = params instanceof Array ? params[0] : params
69-
const { value } = format
69+
const { value, data } = format
70+
const milliseconds = data?.[4] || 0
7071
// If average, don't show the date
7172
const start = formatTime(value[2], averageByDate ? '{h}:{i}' : '{m}-{d} {h}:{i}')
7273
const end = formatTime(value[3], '{h}:{i}')
73-
return `${formatPeriodCommon(value[1] * 1000)}<br/>${start}-${end}`
74+
return `${formatPeriodCommon(Math.floor(milliseconds))}<br/>${start}-${end}`
7475
}
7576

7677
const TITLE = t(msg => msg.habit.chart.title)
77-
const Y_AXIAS_NAME = t(msg => msg.habit.chart.yAxisName)
78+
const Y_AXIAS_MIN = t(msg => msg.habit.chart.yAxisMin)
79+
const Y_AXIAS_HOUR = t(msg => msg.habit.chart.yAxisHour)
80+
81+
function getYAxiasName(periodSize: number) {
82+
return periodSize === 8 ? Y_AXIAS_HOUR : Y_AXIAS_MIN
83+
}
84+
85+
function getYAxiasValue(milliseconds: number, periodSize: number) {
86+
const seconds = Math.floor(milliseconds / 1000)
87+
const minutes = Number.parseFloat((seconds / 60).toFixed(1))
88+
const hours = Number.parseFloat((minutes / 60).toFixed(1))
89+
return periodSize === 8 ? hours : minutes
90+
}
91+
7892
function generateOptions(data: timer.period.Row[], averageByDate: boolean, periodSize: number): EcOption {
7993
const periodData: timer.period.Row[] = averageByDate ? averageByDay(data, periodSize) : data
8094
const valueData: any[] = []
8195
periodData.forEach((item) => {
8296
const startTime = item.startTime.getTime()
8397
const endTime = item.endTime.getTime()
84-
const seconds = Math.floor(item.milliseconds / 1000)
8598
const x = (startTime + endTime) / 2
86-
valueData.push([x, seconds, startTime, endTime])
99+
const milliseconds = item.milliseconds
100+
valueData.push([x, getYAxiasValue(milliseconds, periodSize), startTime, endTime, milliseconds])
87101
})
88102

89103
const xAxisMin = periodData[0].startTime.getTime()
@@ -123,7 +137,7 @@ function generateOptions(data: timer.period.Row[], averageByDate: boolean, perio
123137
max: xAxisMax
124138
},
125139
yAxis: {
126-
name: Y_AXIAS_NAME,
140+
name: getYAxiasName(periodSize),
127141
nameTextStyle: { color: textColor },
128142
type: 'value',
129143
axisLabel: { color: textColor },

src/app/locale/components/habit.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ export type HabitMessage = {
2828
chart: {
2929
title: string
3030
saveAsImageTitle: string
31-
yAxisName: string
31+
yAxisMin: string
32+
yAxisHour: string
3233
}
3334
}
3435

@@ -54,7 +55,8 @@ const _default: Messages<HabitMessage> = {
5455
chart: {
5556
title: '上网习惯统计',
5657
saveAsImageTitle: '保存',
57-
yAxisName: '浏览时长 / 秒'
58+
yAxisMin: '浏览时长 / 分钟',
59+
yAxisHour: '浏览时长 / 小时'
5860
}
5961
},
6062
zh_TW: {
@@ -78,7 +80,8 @@ const _default: Messages<HabitMessage> = {
7880
chart: {
7981
title: '上網習慣統計',
8082
saveAsImageTitle: '保存',
81-
yAxisName: '瀏覽時長 / 秒'
83+
yAxisMin: '瀏覽時長 / 分鐘',
84+
yAxisHour: '瀏覽時長 / 小時'
8285
}
8386
},
8487
en: {
@@ -102,7 +105,8 @@ const _default: Messages<HabitMessage> = {
102105
chart: {
103106
title: 'Time-phased Statistics of Browsing Time',
104107
saveAsImageTitle: 'Snapshot',
105-
yAxisName: 'Browsing Time / second'
108+
yAxisMin: 'Browsing Time / minute',
109+
yAxisHour: 'Browsing Time / hour'
106110
}
107111
},
108112
ja: {
@@ -126,7 +130,8 @@ const _default: Messages<HabitMessage> = {
126130
chart: {
127131
title: '時系列の統計を閲覧する',
128132
saveAsImageTitle: 'ダウンロード',
129-
yAxisName: '閲覧時間/秒'
133+
yAxisMin: '閲覧時間/分',
134+
yAxisHour: '閲覧時間/時間'
130135
}
131136
}
132137
}

0 commit comments

Comments
 (0)