Skip to content

Commit

Permalink
add clickForMoreInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 committed Dec 29, 2023
1 parent 5e27940 commit daab881
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 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;

@property() public period?: string;

@state() private _chartData: ChartData = { datasets: [] };
Expand Down Expand Up @@ -245,6 +248,28 @@ export class StatisticsChart extends LitElement {
},
// @ts-expect-error
locale: numberFormatToLocale(this.hass.locale),
onClick: (e: any) => {
if (!this.clickForMoreInfo) {
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],
});
chart.canvas.dispatchEvent(new Event("mouseout")); // to hide tooltip
}
},
};
}

Expand Down

0 comments on commit daab881

Please sign in to comment.