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

Timeline history graph - react to click to display entity more info #17994

Closed
wants to merge 2 commits into from
Closed
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
19 changes: 19 additions & 0 deletions src/components/chart/state-history-chart-timeline.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { ChartData, ChartDataset, ChartOptions } from "chart.js";
import { getRelativePosition } from "chart.js/helpers";
import { css, CSSResultGroup, html, LitElement, PropertyValues } from "lit";
import { customElement, property, query, state } from "lit/decorators";
import { formatDateTimeWithSeconds } from "../../common/datetime/format_date_time";
Expand Down Expand Up @@ -32,6 +33,8 @@ export class StateHistoryChartTimeline extends LitElement {

@property({ type: Boolean }) public showNames = true;

@property({ type: Boolean }) public inMoreInfo = false;

@property({ type: Boolean }) public chunked = false;

@property({ attribute: false }) public startTime!: Date;
Expand Down Expand Up @@ -220,6 +223,22 @@ export class StateHistoryChartTimeline extends LitElement {
},
// @ts-expect-error
locale: numberFormatToLocale(this.hass.locale),
onClick: (e: any) => {
if (this.inMoreInfo) {
return;
}

const chart = e.chart;
const canvasPosition = getRelativePosition(e, chart);

const index = Math.abs(
chart.scales.y.getValueForPixel(canvasPosition.y)
);
fireEvent(this, "hass-more-info", {
// @ts-ignore
entityId: this._chartData?.datasets[index]?.label,
});
},
};
}

Expand Down
3 changes: 3 additions & 0 deletions src/components/chart/state-history-charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export class StateHistoryCharts extends LitElement {

@property({ type: Boolean }) public showNames = true;

@property({ type: Boolean }) public inMoreInfo = false;

@property({ type: Boolean }) public isLoadingData = false;

@state() private _computedStartTime!: Date;
Expand Down Expand Up @@ -197,6 +199,7 @@ export class StateHistoryCharts extends LitElement {
.chunked=${this.virtualize}
.paddingYAxis=${this._maxYWidth}
.chartIndex=${index}
.inMoreInfo=${this.inMoreInfo}
@y-width-changed=${this._yWidthChanged}
></state-history-chart-timeline>
</div> `;
Expand Down
1 change: 1 addition & 0 deletions src/dialogs/more-info/ha-more-info-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class MoreInfoHistory extends LitElement {
.historyData=${this._stateHistory}
.isLoadingData=${!this._stateHistory}
.showNames=${false}
inMoreInfo
></state-history-charts>`}`
: ""}`;
}
Expand Down
Loading