Skip to content

Commit

Permalink
Handle clicking on a label of a timeline history graph card and displ…
Browse files Browse the repository at this point in the history
…ay more info for the displayed entity
  • Loading branch information
k3a authored and Mario Hros committed Sep 22, 2023
1 parent 4b5c702 commit a297077
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/components/chart/ha-chart-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { styleMap } from "lit/directives/style-map";
import { clamp } from "../../common/number/clamp";
import { computeRTL } from "../../common/util/compute_rtl";
import { HomeAssistant } from "../../types";
import { fireEvent } from "../../common/dom/fire_event";

export const MIN_TIME_BETWEEN_UPDATES = 60 * 5 * 1000;

Expand Down Expand Up @@ -271,6 +272,46 @@ export class HaChartBase extends LitElement {
};
}

private _findLabel(evt): { label: string; index: number } | null {
let res: { label: string; index: number } | null = null;

const scales = this.chart?.scales ?? [];

const scaleDetails = Object.values(scales).map((s: any) => ({
scaleId: s.id,
labels: s?._labelItems?.map(
(e, i) =>
({
x: e.options.translation[0] - (s?._labelSizes?.widths[i] ?? 0),
x2: e.options.translation[0] + (s?._labelSizes?.widths[i] ?? 0) / 2,
y: e.options.translation[1] - (s?._labelSizes?.heights[i] ?? 0) / 2,
y2:
e.options.translation[1] + (s?._labelSizes?.heights[i] ?? 0) / 2,
label: e.label,
index: i,
}) ?? []
),
}));

scaleDetails.forEach((l) => {
l.labels.forEach((label, index) => {
if (
evt.x > label.x &&
evt.x < label.x2 &&
evt.y > label.y &&
evt.y < label.y2
) {
res = {
label: label.label,
index,
};
}
});
});

return res;
}

private _createPlugins() {
return [
...(this.plugins || []),
Expand All @@ -288,6 +329,25 @@ export class HaChartBase extends LitElement {
display: false,
},
},
{
id: "labelClickHandler",
afterEvent: (chart: Chart, event) => {

Check failure on line 334 in src/components/chart/ha-chart-base.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

'chart' is declared but its value is never read.
const evt = event.event;

if (evt.type !== "click" || event.inChartArea) {
return;
}

const labelInfo = this._findLabel(evt);

if (labelInfo) {
fireEvent(this, "label-click", {
label: labelInfo.label,
index: labelInfo.index,
});
}
},
},
];
}

Expand Down Expand Up @@ -451,4 +511,10 @@ declare global {
interface HTMLElementTagNameMap {
"ha-chart-base": HaChartBase;
}
interface HASSDomEvents {
"label-click": {
label: string;
index: number;
};
}
}
14 changes: 14 additions & 0 deletions src/components/chart/state-history-charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
queryAll,
state,
} from "lit/decorators";
import { fireEvent } from "../../common/dom/fire_event";
import { isComponentLoaded } from "../../common/config/is_component_loaded";
import { restoreScroll } from "../../common/decorators/restore-scroll";
import {
Expand Down Expand Up @@ -198,6 +199,7 @@ export class StateHistoryCharts extends LitElement {
.paddingYAxis=${this._maxYWidth}
.chartIndex=${index}
@y-width-changed=${this._yWidthChanged}
@label-click=${this._labelClicked}
></state-history-chart-timeline>
</div> `;
};
Expand Down Expand Up @@ -236,6 +238,18 @@ export class StateHistoryCharts extends LitElement {
this._maxYWidth = Math.max(...Object.values(this._childYWidths), 0);
}

private _labelClicked(e: CustomEvent<HASSDomEvents["label-click"]>) {
const timeline = this.historyData?.timeline;

if (timeline) {
const entityId = timeline[e.detail.index]?.entity_id;

if (entityId) {
fireEvent(this, "hass-more-info", { entityId });
}
}
}

private _isHistoryEmpty(): boolean {
const historyDataEmpty =
!this.historyData ||
Expand Down

0 comments on commit a297077

Please sign in to comment.