use short form for time units in graph axis

This commit is contained in:
Damien Elmes 2020-07-22 12:59:56 +10:00
parent 2ff1a27b04
commit 4b6033e17f
3 changed files with 51 additions and 39 deletions

View File

@ -162,3 +162,16 @@ statistics-hours-subtitle = Review success rate for each hour of the day.
# shown when graph is empty
statistics-no-data = NO DATA
statistics-calendar-title = Calendar
## An amount of elapsed time, used in the graphs to show the amount of
## time spent studying. For example, English would show "5s" for 5 seconds,
## "13.5m" for 13.5 minutes, and so on.
statistics-elapsed-time-seconds = {$amount}s
statistics-elapsed-time-minutes = {$amount}m
statistics-elapsed-time-hours = {$amount}h
statistics-elapsed-time-days = {$amount}d
statistics-elapsed-time-months = {$amount}mo
statistics-elapsed-time-years = {$amount}y
##

View File

@ -169,7 +169,7 @@ export function renderReviews(
.tickSizeOuter(0)
.tickFormat(((n: number): string => {
if (showTime) {
return timeSpan(i18n, n / 1000);
return timeSpan(i18n, n / 1000, true);
} else {
if (Math.round(n) != n) {
return "";

View File

@ -70,15 +70,6 @@ function unitAmount(unit: TimespanUnit, secs: number): number {
}
}
function unitAmountRounded(unit: TimespanUnit, secs: number): number {
const value = unitAmount(unit, secs);
if (unit === TimespanUnit.Seconds || unit === TimespanUnit.Days) {
return Math.round(value);
} else {
return value;
}
}
export function studiedToday(i18n: I18n, cards: number, secs: number): string {
const unit = naturalUnit(secs);
const amount = unitAmount(unit, secs);
@ -96,40 +87,48 @@ export function studiedToday(i18n: I18n, cards: number, secs: number): string {
});
}
function i18nKeyForUnit(i18n: I18n, unit: TimespanUnit, short: boolean): number {
if (short) {
switch (unit) {
case TimespanUnit.Seconds:
return i18n.TR.STATISTICS_ELAPSED_TIME_SECONDS;
case TimespanUnit.Minutes:
return i18n.TR.STATISTICS_ELAPSED_TIME_MINUTES;
case TimespanUnit.Hours:
return i18n.TR.STATISTICS_ELAPSED_TIME_HOURS;
case TimespanUnit.Days:
return i18n.TR.STATISTICS_ELAPSED_TIME_DAYS;
case TimespanUnit.Months:
return i18n.TR.STATISTICS_ELAPSED_TIME_MONTHS;
case TimespanUnit.Years:
return i18n.TR.STATISTICS_ELAPSED_TIME_YEARS;
}
} else {
switch (unit) {
case TimespanUnit.Seconds:
return i18n.TR.SCHEDULING_TIME_SPAN_SECONDS;
case TimespanUnit.Minutes:
return i18n.TR.SCHEDULING_TIME_SPAN_MINUTES;
case TimespanUnit.Hours:
return i18n.TR.SCHEDULING_TIME_SPAN_HOURS;
case TimespanUnit.Days:
return i18n.TR.SCHEDULING_TIME_SPAN_DAYS;
case TimespanUnit.Months:
return i18n.TR.SCHEDULING_TIME_SPAN_MONTHS;
case TimespanUnit.Years:
return i18n.TR.SCHEDULING_TIME_SPAN_YEARS;
}
}
}
/// Describe the given seconds using the largest appropriate unit.
/// If precise is true, show to two decimal places, eg
/// eg 70 seconds -> "1.17 minutes"
/// If false, seconds and days are shown without decimals.
export function timeSpan(i18n: I18n, seconds: number, precise = true): string {
export function timeSpan(i18n: I18n, seconds: number, short = false): string {
const unit = naturalUnit(seconds);
let amount: number;
if (precise) {
amount = unitAmount(unit, seconds);
} else {
amount = unitAmountRounded(unit, seconds);
}
let key: number;
switch (unit) {
case TimespanUnit.Seconds:
key = i18n.TR.SCHEDULING_TIME_SPAN_SECONDS;
break;
case TimespanUnit.Minutes:
key = i18n.TR.SCHEDULING_TIME_SPAN_MINUTES;
break;
case TimespanUnit.Hours:
key = i18n.TR.SCHEDULING_TIME_SPAN_HOURS;
break;
case TimespanUnit.Days:
key = i18n.TR.SCHEDULING_TIME_SPAN_DAYS;
break;
case TimespanUnit.Months:
key = i18n.TR.SCHEDULING_TIME_SPAN_MONTHS;
break;
case TimespanUnit.Years:
key = i18n.TR.SCHEDULING_TIME_SPAN_YEARS;
break;
}
const amount = unitAmount(unit, seconds);
const key = i18nKeyForUnit(i18n, unit, short);
return i18n.tr(key, { amount });
}