-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
2,090 additions
and
831 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,51 @@ | ||
import type { HassConfig } from "home-assistant-js-websocket"; | ||
import type { XAXisOption } from "echarts/types/dist/shared"; | ||
import type { FrontendLocaleData } from "../../data/translation"; | ||
import { | ||
formatDateMonth, | ||
formatDateMonthYear, | ||
formatDateVeryShort, | ||
formatDateWeekdayShort, | ||
} from "../../common/datetime/format_date"; | ||
import { formatTime } from "../../common/datetime/format_time"; | ||
import { | ||
formatTime, | ||
formatTimeWithSeconds, | ||
} from "../../common/datetime/format_time"; | ||
|
||
export function getLabelFormatter( | ||
export function formatTimeLabel( | ||
value: number | Date, | ||
locale: FrontendLocaleData, | ||
config: HassConfig, | ||
dayDifference = 0 | ||
minutesDifference: number | ||
) { | ||
return (value: number | Date) => { | ||
const date = new Date(value); | ||
if (dayDifference > 88) { | ||
return date.getMonth() === 0 | ||
? `{bold|${formatDateMonthYear(date, locale, config)}}` | ||
: formatDateMonth(date, locale, config); | ||
} | ||
if (dayDifference > 35) { | ||
return date.getDate() === 1 | ||
? `{bold|${formatDateVeryShort(date, locale, config)}}` | ||
: formatDateVeryShort(date, locale, config); | ||
} | ||
if (dayDifference > 7) { | ||
const label = formatDateVeryShort(date, locale, config); | ||
return date.getDate() === 1 ? `{bold|${label}}` : label; | ||
} | ||
if (dayDifference > 2) { | ||
return formatDateWeekdayShort(date, locale, config); | ||
} | ||
const dayDifference = minutesDifference / 60 / 24; | ||
const date = new Date(value); | ||
if (dayDifference > 88) { | ||
return date.getMonth() === 0 | ||
? `{bold|${formatDateMonthYear(date, locale, config)}}` | ||
: formatDateMonth(date, locale, config); | ||
} | ||
if (dayDifference > 35) { | ||
return date.getDate() === 1 | ||
? `{bold|${formatDateVeryShort(date, locale, config)}}` | ||
: formatDateVeryShort(date, locale, config); | ||
} | ||
if (dayDifference > 7) { | ||
const label = formatDateVeryShort(date, locale, config); | ||
return date.getDate() === 1 ? `{bold|${label}}` : label; | ||
} | ||
if (dayDifference > 2) { | ||
return formatDateWeekdayShort(date, locale, config); | ||
} | ||
if (minutesDifference && minutesDifference < 5) { | ||
return formatTimeWithSeconds(date, locale, config); | ||
} | ||
if ( | ||
date.getHours() === 0 && | ||
date.getMinutes() === 0 && | ||
date.getSeconds() === 0 | ||
) { | ||
// show only date for the beginning of the day | ||
if ( | ||
date.getHours() === 0 && | ||
date.getMinutes() === 0 && | ||
date.getSeconds() === 0 | ||
) { | ||
return `{bold|${formatDateVeryShort(date, locale, config)}}`; | ||
} | ||
return formatTime(date, locale, config); | ||
}; | ||
} | ||
|
||
export function getTimeAxisLabelConfig( | ||
locale: FrontendLocaleData, | ||
config: HassConfig, | ||
dayDifference?: number | ||
): XAXisOption["axisLabel"] { | ||
return { | ||
formatter: getLabelFormatter(locale, config, dayDifference), | ||
rich: { | ||
bold: { | ||
fontWeight: "bold", | ||
}, | ||
}, | ||
hideOverlap: true, | ||
}; | ||
return `{bold|${formatDateVeryShort(date, locale, config)}}`; | ||
} | ||
return formatTime(date, locale, config); | ||
} |
Oops, something went wrong.