diff --git a/src/components/chart/state-history-chart-line.ts b/src/components/chart/state-history-chart-line.ts index 46243893a396..64e7feaa164b 100644 --- a/src/components/chart/state-history-chart-line.ts +++ b/src/components/chart/state-history-chart-line.ts @@ -35,6 +35,8 @@ export class StateHistoryChartLine extends LitElement { @property({ type: Boolean }) public showNames = true; + @property({ type: Boolean }) public clickForMoreInfo = true; + @property({ attribute: false }) public startTime!: Date; @property({ attribute: false }) public endTime!: Date; @@ -45,6 +47,8 @@ export class StateHistoryChartLine extends LitElement { @state() private _chartData?: ChartData<"line">; + @state() private _entityIds: string[] = []; + @state() private _chartOptions?: ChartOptions; @state() private _yWidth = 0; @@ -171,6 +175,25 @@ export class StateHistoryChartLine extends LitElement { }, // @ts-expect-error locale: numberFormatToLocale(this.hass.locale), + onClick: (e: any) => { + if (!this.clickForMoreInfo) { + return; + } + + const points = e.chart.getElementsAtEventForMode( + e, + "nearest", + { intersect: true }, + true + ); + + if (points.length) { + const firstPoint = points[0]; + fireEvent(this, "hass-more-info", { + entityId: this._entityIds[firstPoint.datasetIndex], + }); + } + }, }; } if ( @@ -191,6 +214,7 @@ export class StateHistoryChartLine extends LitElement { const computedStyles = getComputedStyle(this); const entityStates = this.data; const datasets: ChartDataset<"line">[] = []; + const entityIds: string[] = []; if (entityStates.length === 0) { return; } @@ -242,6 +266,7 @@ export class StateHistoryChartLine extends LitElement { pointRadius: 0, data: [], }); + entityIds.push(states.entity_id); }; if ( @@ -493,6 +518,7 @@ export class StateHistoryChartLine extends LitElement { this._chartData = { datasets, }; + this._entityIds = entityIds; } } customElements.define("state-history-chart-line", StateHistoryChartLine); diff --git a/src/components/chart/state-history-chart-timeline.ts b/src/components/chart/state-history-chart-timeline.ts index 8f53e0d144a0..e6d8b92b89f9 100644 --- a/src/components/chart/state-history-chart-timeline.ts +++ b/src/components/chart/state-history-chart-timeline.ts @@ -32,6 +32,8 @@ export class StateHistoryChartTimeline extends LitElement { @property({ type: Boolean }) public showNames = true; + @property({ type: Boolean }) public clickForMoreInfo = false; + @property({ type: Boolean }) public chunked = false; @property({ attribute: false }) public startTime!: Date; @@ -220,6 +222,22 @@ export class StateHistoryChartTimeline extends LitElement { }, // @ts-expect-error locale: numberFormatToLocale(this.hass.locale), + onClick: (e: any) => { + if (!this.clickForMoreInfo) { + 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, + }); + }, }; } diff --git a/src/components/chart/state-history-charts.ts b/src/components/chart/state-history-charts.ts index d9dd0d983e21..688e32b445ec 100644 --- a/src/components/chart/state-history-charts.ts +++ b/src/components/chart/state-history-charts.ts @@ -69,6 +69,8 @@ export class StateHistoryCharts extends LitElement { @property({ type: Boolean }) public showNames = true; + @property({ type: Boolean }) public clickForMoreInfo = true; + @property({ type: Boolean }) public isLoadingData = false; @state() private _computedStartTime!: Date; @@ -181,6 +183,7 @@ export class StateHistoryCharts extends LitElement { .paddingYAxis=${this._maxYWidth} .names=${this.names} .chartIndex=${index} + .clickForMoreInfo=${this.clickForMoreInfo} @y-width-changed=${this._yWidthChanged} > `; @@ -197,6 +200,7 @@ export class StateHistoryCharts extends LitElement { .chunked=${this.virtualize} .paddingYAxis=${this._maxYWidth} .chartIndex=${index} + .clickForMoreInfo=${this.clickForMoreInfo} @y-width-changed=${this._yWidthChanged} > `; diff --git a/src/dialogs/more-info/ha-more-info-history.ts b/src/dialogs/more-info/ha-more-info-history.ts index fa66dc05b649..47593f49fec0 100644 --- a/src/dialogs/more-info/ha-more-info-history.ts +++ b/src/dialogs/more-info/ha-more-info-history.ts @@ -99,6 +99,7 @@ export class MoreInfoHistory extends LitElement { .historyData=${this._stateHistory} .isLoadingData=${!this._stateHistory} .showNames=${false} + .clickForMoreInfo=${false} >`}` : ""}`; }