From 2b45265fa768576ecefeb91f0626086011137e5c Mon Sep 17 00:00:00 2001 From: karwosts Date: Fri, 27 Oct 2023 10:05:59 -0700 Subject: [PATCH 1/3] Fix statistics bar chart time axis --- src/components/chart/chart-date-adapter.ts | 2 +- src/components/chart/statistics-chart.ts | 16 ++++++++++++++-- .../lovelace/cards/hui-statistics-graph-card.ts | 1 + 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/components/chart/chart-date-adapter.ts b/src/components/chart/chart-date-adapter.ts index c9f9003e7188..fac781945383 100644 --- a/src/components/chart/chart-date-adapter.ts +++ b/src/components/chart/chart-date-adapter.ts @@ -134,7 +134,7 @@ _adapters._date.override({ this.options.config ); case "week": - return formatDate( + return formatDateVeryShort( new Date(time), this.options.locale, this.options.config diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index f518d0db733e..1be8fb2e6bce 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -73,6 +73,8 @@ export class StatisticsChart extends LitElement { @property({ type: Boolean }) public isLoadingData = false; + @property() public period?: string; + @state() private _chartData: ChartData = { datasets: [] }; @state() private _statisticIds: string[] = []; @@ -92,7 +94,11 @@ export class StatisticsChart extends LitElement { } public willUpdate(changedProps: PropertyValues) { - if (!this.hasUpdated || changedProps.has("unit")) { + if ( + !this.hasUpdated || + changedProps.has("unit") || + (this.chartType === "bar" && changedProps.has("period")) + ) { this._createOptions(); } if ( @@ -160,6 +166,7 @@ export class StatisticsChart extends LitElement { }, }, ticks: { + source: this.chartType === "bar" ? "data" : undefined, maxRotation: 0, sampleSize: 5, autoSkipPadding: 20, @@ -173,6 +180,11 @@ export class StatisticsChart extends LitElement { }, time: { tooltipFormat: "datetime", + unit: + this.chartType === "bar" && + ["hour", "day", "week", "month"].includes(this.period) + ? this.period + : undefined, }, }, y: { @@ -429,7 +441,7 @@ export class StatisticsChart extends LitElement { Array.prototype.push.apply(totalDataSets, statDataSets); }); - if (unit) { + if (unit || this.chartType === "bar") { this._createOptions(unit); } diff --git a/src/panels/lovelace/cards/hui-statistics-graph-card.ts b/src/panels/lovelace/cards/hui-statistics-graph-card.ts index 32806d002eef..691123ab7d3f 100644 --- a/src/panels/lovelace/cards/hui-statistics-graph-card.ts +++ b/src/panels/lovelace/cards/hui-statistics-graph-card.ts @@ -193,6 +193,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard { .isLoadingData=${!this._statistics} .statisticsData=${this._statistics} .metadata=${this._metadata} + .period=${this._config.period} .chartType=${this._config.chart_type || "line"} .statTypes=${this._statTypes!} .names=${this._names} From 6e75cd3b8d5d06dc29f1abd8419e1e0dbc24d8e6 Mon Sep 17 00:00:00 2001 From: karwosts Date: Fri, 27 Oct 2023 10:24:38 -0700 Subject: [PATCH 2/3] fix lint --- src/components/chart/statistics-chart.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index 1be8fb2e6bce..3bdb72427cc2 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -182,6 +182,7 @@ export class StatisticsChart extends LitElement { tooltipFormat: "datetime", unit: this.chartType === "bar" && + this.period && ["hour", "day", "week", "month"].includes(this.period) ? this.period : undefined, @@ -442,7 +443,7 @@ export class StatisticsChart extends LitElement { }); if (unit || this.chartType === "bar") { - this._createOptions(unit); + this._createOptions(unit ?? undefined); } this._chartData = { From ab7fb45ebcb7e23ee96811491fda08407d852034 Mon Sep 17 00:00:00 2001 From: karwosts Date: Fri, 27 Oct 2023 11:14:09 -0700 Subject: [PATCH 3/3] rearrange update calls --- src/components/chart/statistics-chart.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index 3bdb72427cc2..89c6905eac5f 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -97,7 +97,8 @@ export class StatisticsChart extends LitElement { if ( !this.hasUpdated || changedProps.has("unit") || - (this.chartType === "bar" && changedProps.has("period")) + changedProps.has("period") || + changedProps.has("chartType") ) { this._createOptions(); } @@ -442,8 +443,8 @@ export class StatisticsChart extends LitElement { Array.prototype.push.apply(totalDataSets, statDataSets); }); - if (unit || this.chartType === "bar") { - this._createOptions(unit ?? undefined); + if (unit) { + this._createOptions(unit); } this._chartData = {