From bf028915ecae4b89cbb0ce4b02839baff401a50c Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Tue, 5 Mar 2024 15:13:51 +0100 Subject: [PATCH] Add clickForMoreInfo to statistics graph card (#19178) * add clickForMoreInfo * only show more info on graphs when mouse is used * disable clickForMoreInfo already in more-info popup * check if not isExternalStatistic * Apply suggestions from code review Co-authored-by: Bram Kragten --------- Co-authored-by: Bram Kragten --- src/components/chart/statistics-chart.ts | 31 +++++++++++++++++++ src/dialogs/more-info/ha-more-info-history.ts | 1 + 2 files changed, 32 insertions(+) diff --git a/src/components/chart/statistics-chart.ts b/src/components/chart/statistics-chart.ts index 0c5597878c..81c34f896b 100644 --- a/src/components/chart/statistics-chart.ts +++ b/src/components/chart/statistics-chart.ts @@ -16,6 +16,7 @@ import { customElement, property, state, query } from "lit/decorators"; import memoizeOne from "memoize-one"; import { getGraphColorByIndex } from "../../common/color/colors"; import { isComponentLoaded } from "../../common/config/is_component_loaded"; +import { fireEvent } from "../../common/dom/fire_event"; import { formatNumber, numberFormatToLocale, @@ -25,6 +26,7 @@ import { getDisplayUnit, getStatisticLabel, getStatisticMetadata, + isExternalStatistic, Statistics, statisticsHaveType, StatisticsMetaData, @@ -79,6 +81,8 @@ export class StatisticsChart extends LitElement { @property({ type: Boolean }) public isLoadingData = false; + @property({ type: Boolean }) public clickForMoreInfo = true; + @property() public period?: string; @state() private _chartData: ChartData = { datasets: [] }; @@ -273,6 +277,33 @@ export class StatisticsChart extends LitElement { }, // @ts-expect-error locale: numberFormatToLocale(this.hass.locale), + onClick: (e: any) => { + if ( + !this.clickForMoreInfo || + !(e.native instanceof MouseEvent) || + (e.native instanceof PointerEvent && e.native.pointerType !== "mouse") + ) { + return; + } + + const chart = e.chart; + + const points = chart.getElementsAtEventForMode( + e, + "nearest", + { intersect: true }, + true + ); + + if (points.length) { + const firstPoint = points[0]; + const statisticId = this._statisticIds[firstPoint.datasetIndex]; + if (!isExternalStatistic(statisticId)) { + fireEvent(this, "hass-more-info", { entityId: statisticId }); + chart.canvas.dispatchEvent(new Event("mouseout")); // to hide tooltip + } + } + }, }; } diff --git a/src/dialogs/more-info/ha-more-info-history.ts b/src/dialogs/more-info/ha-more-info-history.ts index dc5c32f8a1..8917054a13 100644 --- a/src/dialogs/more-info/ha-more-info-history.ts +++ b/src/dialogs/more-info/ha-more-info-history.ts @@ -93,6 +93,7 @@ export class MoreInfoHistory extends LitElement { .names=${this._statNames} hideLegend .showNames=${false} + .clickForMoreInfo=${false} >` : html`