Skip to content

Commit

Permalink
feat: display the chart's last update time using last_updated
Browse files Browse the repository at this point in the history
  • Loading branch information
RomRider committed May 29, 2021
1 parent 33fc474 commit 2dd84ff
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,8 @@ views:
header:
show: true
title: Soft Bounds + fixed bound
show:
last_updated: true
graph_span: 20min
all_series_config:
stroke_width: 2
Expand Down
28 changes: 27 additions & 1 deletion src/apexcharts-card.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'array-flat-polyfill';
import { LitElement, html, TemplateResult, PropertyValues, CSSResultGroup } from 'lit';
import { LitElement, html, TemplateResult, PropertyValues, CSSResultGroup, Template } from 'lit';
import { property, customElement } from 'lit/decorators.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { ClassInfo, classMap } from 'lit/directives/class-map.js';
Expand Down Expand Up @@ -149,6 +149,8 @@ class ChartsCard extends LitElement {

private _yAxisConfig?: ChartCardYAxis[];

@property({ attribute: false }) _lastUpdated: Date = new Date();

@property({ type: Boolean }) private _warning = false;

public connectedCallback() {
Expand Down Expand Up @@ -553,6 +555,7 @@ class ChartsCard extends LitElement {
${this._config.series_in_brush.length ? html`<div id="brush"></div>` : ``}
</div>
</div>
${this._renderLastUpdated()}
</ha-card>
`;
}
Expand Down Expand Up @@ -618,6 +621,28 @@ class ChartsCard extends LitElement {
`;
}

private _renderLastUpdated(): TemplateResult {
if (this._config?.show?.last_updated) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let hours12: any = undefined;
if (this._config.hours_12 !== undefined) {
hours12 = this._config.hours_12 ? { hour12: true } : { hourCycle: 'h23' };
}
const lastUpdated = new Intl.DateTimeFormat(this._config.locale || this._hass?.language || 'en', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
...hours12,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any).format(this._lastUpdated);
return html` <div id="last_updated">${lastUpdated}</div> `;
}
return html``;
}

private async _initialLoad() {
await this.updateComplete;

Expand Down Expand Up @@ -649,6 +674,7 @@ class ChartsCard extends LitElement {

const { start, end } = this._getSpanDates();
const now = new Date();
this._lastUpdated = now;
const editMode = getLovelace()?.editMode;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const caching = editMode === true ? false : this._config!.cache;
Expand Down
12 changes: 12 additions & 0 deletions src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ export const stylesApex: CSSResultGroup = css`
text-overflow: ellipsis;
}
#last_updated {
font-size: 0.63em;
font-weight: 300;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
position: absolute;
bottom: 0px;
right: 4px;
opacity: 0.5;
}
/* Apex Charts Default CSS */
.apexcharts-canvas {
position: relative;
Expand Down
1 change: 1 addition & 0 deletions src/types-config-ti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const ChartCardExternalConfig = t.iface([], {
})),
"show": t.opt(t.iface([], {
"loading": t.opt("boolean"),
"last_updated": t.opt("boolean"),
})),
"cache": t.opt("boolean"),
"stacked": t.opt("boolean"),
Expand Down
1 change: 1 addition & 0 deletions src/types-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface ChartCardExternalConfig {
};
show?: {
loading?: boolean;
last_updated?: boolean;
};
cache?: boolean;
stacked?: boolean;
Expand Down

0 comments on commit 2dd84ff

Please sign in to comment.