Skip to content

Commit

Permalink
Fix statistics bar chart time axis
Browse files Browse the repository at this point in the history
  • Loading branch information
karwosts committed Oct 27, 2023
1 parent c3743b5 commit 2b45265
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/chart/chart-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions src/components/chart/statistics-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand All @@ -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 (
Expand Down Expand Up @@ -160,6 +166,7 @@ export class StatisticsChart extends LitElement {
},
},
ticks: {
source: this.chartType === "bar" ? "data" : undefined,
maxRotation: 0,
sampleSize: 5,
autoSkipPadding: 20,
Expand All @@ -173,6 +180,11 @@ export class StatisticsChart extends LitElement {
},
time: {
tooltipFormat: "datetime",
unit:
this.chartType === "bar" &&
["hour", "day", "week", "month"].includes(this.period)

Check failure on line 185 in src/components/chart/statistics-chart.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
? this.period
: undefined,
},
},
y: {
Expand Down Expand Up @@ -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);

Check failure on line 445 in src/components/chart/statistics-chart.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Argument of type 'string | null | undefined' is not assignable to parameter of type 'string | undefined'.
}

Expand Down
1 change: 1 addition & 0 deletions src/panels/lovelace/cards/hui-statistics-graph-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down

0 comments on commit 2b45265

Please sign in to comment.