Skip to content

Commit f38a18e

Browse files
authored
feat: Support for 12-hour format (#55)
Fixes #48, Fixes #53
1 parent cf9b63a commit f38a18e

File tree

5 files changed

+48
-9
lines changed

5 files changed

+48
-9
lines changed

.devcontainer/ui-lovelace.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,12 +365,17 @@ views:
365365
# apex_config:
366366
# dataLabels:
367367
# enabled: true
368+
hours_12: true
368369
header:
369370
show: true
370371
title: Light Brightness (attribute test)
371372
series:
372373
- entity: light.kitchen_lights
373374
attribute: brightness
375+
apex_config:
376+
tooltip:
377+
x:
378+
format: MMM
374379

375380
- type: custom:apexcharts-card
376381
header:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ The card stricly validates all the options available (but not for the `apex_conf
122122
| `graph_span` | string | `24h` | v1.1.0 | The span of the graph as a time interval. Valid values are any time string, eg: `1h`, `12min`, `1d`, `1h25`, `10sec`, ... |
123123
| `span` | object | | v1.2.0 | See [span](#span-options) |
124124
| `show` | object | | v1.0.0 | See [show](#main-show-options) |
125+
| `hours_12` | boolean | `false` | NEXT_VERSION | Display time in 12h format instead of the default 24h format |
125126
| `cache` | boolean | `true` | v1.0.0 | Use in-browser data caching to reduce the load on Home Assistant's server |
126127
| `stacked` | boolean | `false` | v1.0.0 | Enable if you want the data to be stacked on the graph |
127128
| `layout` | string | | v1.0.0 | See [layouts](#layouts) |

src/apex-layouts.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export function getLayoutConfig(config: ChartCardConfig, hass: HomeAssistant | u
5252
// range: getMilli(config.hours_to_show),
5353
labels: {
5454
datetimeUTC: false,
55+
datetimeFormatter: getDateTimeFormatter(config.hours_12),
5556
},
5657
}
5758
: {},
@@ -62,15 +63,7 @@ export function getLayoutConfig(config: ChartCardConfig, hass: HomeAssistant | u
6263
},
6364
tooltip: {
6465
x: {
65-
formatter:
66-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
67-
parse(config.graph_span!)! < HOUR_24 && !config.span?.offset
68-
? function (val) {
69-
return moment(new Date(val)).format('HH:mm:ss');
70-
}
71-
: function (val) {
72-
return moment(new Date(val)).format('MMM Do, HH:mm:ss');
73-
},
66+
formatter: getXTooltipFormatter(config),
7467
},
7568
y: {
7669
formatter: function (value, opts, conf = config, hass2 = hass) {
@@ -243,3 +236,41 @@ export function getLayoutConfig(config: ChartCardConfig, hass: HomeAssistant | u
243236

244237
return config.apex_config ? mergeDeep(mergeDeep(def, conf), config.apex_config) : mergeDeep(def, conf);
245238
}
239+
240+
function getDateTimeFormatter(hours12: boolean | undefined): unknown {
241+
if (!hours12) {
242+
return {
243+
year: 'yyyy',
244+
month: "MMM 'yy",
245+
day: 'dd MMM',
246+
hour: 'HH:mm',
247+
minute: 'HH:mm:ss',
248+
};
249+
} else {
250+
return {
251+
year: 'yyyy',
252+
month: "MMM 'yy",
253+
day: 'dd MMM',
254+
hour: 'hh:mm tt',
255+
minute: 'hh:mm:ss tt',
256+
};
257+
}
258+
}
259+
260+
function getXTooltipFormatter(config: ChartCardConfig): ((val: number) => string) | undefined {
261+
if (config.apex_config?.tooltip?.x?.format) return undefined;
262+
let hours = 'HH:mm:ss';
263+
let days = 'MMM Do, HH:mm:ss';
264+
if (config.hours_12) {
265+
hours = 'hh:mm:ss a';
266+
days = 'MMM Do, hh:mm:ss';
267+
}
268+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
269+
return parse(config.graph_span)! < HOUR_24 && !config.span?.offset
270+
? function (val) {
271+
return moment(new Date(val)).format(hours);
272+
}
273+
: function (val) {
274+
return moment(new Date(val)).format(days);
275+
};
276+
}

src/types-config-ti.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const ChartCardExternalConfig = t.iface([], {
1111
"update_delay": t.opt("string"),
1212
"series": t.array("ChartCardSeriesExternalConfig"),
1313
"graph_span": t.opt("string"),
14+
"hours_12": t.opt("boolean"),
1415
"span": t.opt("ChartCardSpanExtConfig"),
1516
"y_axis_precision": t.opt("number"),
1617
"now": t.opt(t.iface([], {

src/types-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export interface ChartCardExternalConfig {
55
update_delay?: string;
66
series: ChartCardSeriesExternalConfig[];
77
graph_span?: string;
8+
hours_12?: boolean;
89
span?: ChartCardSpanExtConfig;
910
y_axis_precision?: number;
1011
now?: {

0 commit comments

Comments
 (0)