Skip to content

Commit

Permalink
Handle clicking on a state history chart and display more info for th…
Browse files Browse the repository at this point in the history
…e displayed entity
  • Loading branch information
Mario Hros committed Sep 26, 2023
1 parent 4b5c702 commit 7930c84
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/components/chart/state-history-chart-line.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 (
Expand All @@ -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;
}
Expand Down Expand Up @@ -242,6 +266,7 @@ export class StateHistoryChartLine extends LitElement {
pointRadius: 0,
data: [],
});
entityIds.push(states.entity_id);
};

if (
Expand Down Expand Up @@ -493,6 +518,7 @@ export class StateHistoryChartLine extends LitElement {
this._chartData = {
datasets,
};
this._entityIds = entityIds;
}
}
customElements.define("state-history-chart-line", StateHistoryChartLine);
Expand Down
18 changes: 18 additions & 0 deletions src/components/chart/state-history-chart-timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Check failure on line 231 in src/components/chart/state-history-chart-timeline.ts

View workflow job for this annotation

GitHub Actions / Lint and check format

Cannot find name 'getRelativePosition'.

const index = Math.abs(
chart.scales.y.getValueForPixel(canvasPosition.y)
);
fireEvent(this, "hass-more-info", {
// @ts-ignore
entityId: this._chartData?.datasets[index]?.label,
});
},
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/components/chart/state-history-charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}
></state-history-chart-line>
</div> `;
Expand All @@ -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}
></state-history-chart-timeline>
</div> `;
Expand Down
1 change: 1 addition & 0 deletions src/dialogs/more-info/ha-more-info-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class MoreInfoHistory extends LitElement {
.historyData=${this._stateHistory}
.isLoadingData=${!this._stateHistory}
.showNames=${false}
.clickForMoreInfo=${false}
></state-history-charts>`}`
: ""}`;
}
Expand Down

0 comments on commit 7930c84

Please sign in to comment.