Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix statistics bar chart time axis #18450

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 15 additions & 1 deletion 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,12 @@ export class StatisticsChart extends LitElement {
}

public willUpdate(changedProps: PropertyValues) {
if (!this.hasUpdated || changedProps.has("unit")) {
if (
!this.hasUpdated ||
changedProps.has("unit") ||
changedProps.has("period") ||
changedProps.has("chartType")
) {
this._createOptions();
}
if (
Expand Down Expand Up @@ -160,6 +167,7 @@ export class StatisticsChart extends LitElement {
},
},
ticks: {
source: this.chartType === "bar" ? "data" : undefined,
maxRotation: 0,
sampleSize: 5,
autoSkipPadding: 20,
Expand All @@ -173,6 +181,12 @@ export class StatisticsChart extends LitElement {
},
time: {
tooltipFormat: "datetime",
unit:
this.chartType === "bar" &&
this.period &&
["hour", "day", "week", "month"].includes(this.period)
? this.period
: undefined,
},
},
y: {
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
Loading