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

Add clickForMoreInfo to statistics graph card #19178

Merged
Changes from 2 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
29 changes: 29 additions & 0 deletions src/components/chart/statistics-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -75,6 +76,8 @@ export class StatisticsChart extends LitElement {

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

@property({ type: Boolean }) public clickForMoreInfo = true;
mib1185 marked this conversation as resolved.
Show resolved Hide resolved

@property() public period?: string;

@state() private _chartData: ChartData = { datasets: [] };
Expand Down Expand Up @@ -245,6 +248,32 @@ 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];
fireEvent(this, "hass-more-info", {
entityId: this._statisticIds[firstPoint.datasetIndex],
mib1185 marked this conversation as resolved.
Show resolved Hide resolved
});
chart.canvas.dispatchEvent(new Event("mouseout")); // to hide tooltip
}
},
};
}

Expand Down
Loading