mirror of
https://github.com/home-assistant/frontend.git
synced 2026-11-30 16:49:04 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75aad938d7 | ||
|
|
1265060c89 |
No files matched your search
@@ -1048,22 +1048,24 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
return [primaryId, ...(customLegendItem?.secondaryIds || [])];
|
||||
}
|
||||
|
||||
// Parses the options structure and adds all ids of unselected legend items to hiddenDatasets.
|
||||
// No known need to remove items at this time.
|
||||
// Hides the datasets of legend items selected as false and shows those selected as true.
|
||||
private _updateHiddenStatsFromOptions(options: HaECOption | undefined) {
|
||||
if (!options) return;
|
||||
const legend = ensureArray(this.options?.legend || [])[0] as
|
||||
LegendComponentOption | undefined;
|
||||
const hiddenCount = this._hiddenDatasets.size;
|
||||
let changed = false;
|
||||
Object.entries(legend?.selected || {}).forEach(([stat, selected]) => {
|
||||
if (selected === false) {
|
||||
this._getAllIdsFromLegend(options, stat).forEach((id) =>
|
||||
this._hiddenDatasets.add(id)
|
||||
);
|
||||
}
|
||||
this._getAllIdsFromLegend(options, stat).forEach((id) => {
|
||||
if (selected === false && !this._hiddenDatasets.has(id)) {
|
||||
this._hiddenDatasets.add(id);
|
||||
changed = true;
|
||||
} else if (selected === true && this._hiddenDatasets.delete(id)) {
|
||||
changed = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
// Flagging this rebuilds and downsamples every series, so only when needed
|
||||
if (this._hiddenDatasets.size !== hiddenCount) {
|
||||
if (changed) {
|
||||
this.requestUpdate("_hiddenDatasets");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,12 @@ import "./ha-svg-icon";
|
||||
*/
|
||||
@customElement("ha-filter-pane-chip")
|
||||
export class HaFilterPaneChip extends LitElement {
|
||||
// A dropdown returns focus to its trigger, which is this host
|
||||
static override shadowRootOptions: ShadowRootInit = {
|
||||
...LitElement.shadowRootOptions,
|
||||
delegatesFocus: true,
|
||||
};
|
||||
|
||||
@property() public label = "";
|
||||
|
||||
/** SVG path of the leading icon. */
|
||||
@@ -21,10 +27,15 @@ export class HaFilterPaneChip extends LitElement {
|
||||
|
||||
@property({ type: Boolean }) public disabled = false;
|
||||
|
||||
/** Shows only the icon and moves the label to the tooltip. */
|
||||
@property({ type: Boolean, reflect: true, attribute: "hide-label" })
|
||||
public hideLabel = false;
|
||||
|
||||
protected render() {
|
||||
return html`
|
||||
<ha-assist-chip
|
||||
.label=${this.label}
|
||||
.label=${this.hideLabel ? "" : this.label}
|
||||
.title=${this.hideLabel ? this.label : ""}
|
||||
.active=${this.active}
|
||||
.disabled=${this.disabled}
|
||||
>
|
||||
@@ -45,6 +56,11 @@ export class HaFilterPaneChip extends LitElement {
|
||||
--ha-assist-chip-container-shape: 10px;
|
||||
}
|
||||
|
||||
:host([hide-label]) {
|
||||
--ha-assist-chip-icon-label-space: 0;
|
||||
--ha-assist-chip-trailing-space: 8px;
|
||||
}
|
||||
|
||||
.badge {
|
||||
position: absolute;
|
||||
top: -4px;
|
||||
|
||||
@@ -2,7 +2,8 @@ import type { HassEntities } from "home-assistant-js-websocket";
|
||||
import type { PropertyValues } from "lit";
|
||||
import { css, html, LitElement, nothing } from "lit";
|
||||
import { customElement, state } from "lit/decorators";
|
||||
import { mdiChartDonut, mdiChartBar } from "@mdi/js";
|
||||
import "@home-assistant/webawesome/dist/components/divider/divider";
|
||||
import { mdiChartBar, mdiChartDonut, mdiFormatListChecks } from "@mdi/js";
|
||||
import { classMap } from "lit/directives/class-map";
|
||||
import memoizeOne from "memoize-one";
|
||||
import type { BarSeriesOption, PieSeriesOption } from "echarts/charts";
|
||||
@@ -48,6 +49,10 @@ import type { LovelaceCard } from "../../types";
|
||||
import type { EnergyDevicesGraphCardConfig } from "../types";
|
||||
import type { HaECOption } from "../../../../resources/echarts/echarts";
|
||||
import "../../../../components/ha-card";
|
||||
import "../../../../components/ha-dropdown";
|
||||
import type { HaDropdownSelectEvent } from "../../../../components/ha-dropdown";
|
||||
import "../../../../components/ha-dropdown-item";
|
||||
import "../../../../components/ha-filter-pane-chip";
|
||||
import type { HASSDomEvent } from "../../../../common/dom/fire_event";
|
||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||
import { measureTextWidth } from "../../../../util/text";
|
||||
@@ -56,6 +61,9 @@ import { storage } from "../../../../common/decorators/storage";
|
||||
import { getEnergyColor } from "./common/color";
|
||||
import type { CustomLegendOption } from "../../../../components/chart/ha-chart-base";
|
||||
|
||||
const SELECT_ALL = "__select_all__";
|
||||
const SELECT_NONE = "__select_none__";
|
||||
|
||||
@customElement("hui-energy-devices-graph-card")
|
||||
export class HuiEnergyDevicesGraphCard
|
||||
extends MobileAwareMixin(LitElement)
|
||||
@@ -169,26 +177,38 @@ export class HuiEnergyDevicesGraphCard
|
||||
}
|
||||
|
||||
const modes = this._getAllowedModes();
|
||||
const shownCount = this._legendData.filter(
|
||||
(d) => !this._hiddenStats.includes(d.id!)
|
||||
).length;
|
||||
const rows =
|
||||
this._chartType === "bar" ? shownCount : this._legendData.length;
|
||||
|
||||
return html`
|
||||
<ha-card>
|
||||
<div class="card-header">
|
||||
<span>${this._config.title ? this._config.title : nothing}</span>
|
||||
${
|
||||
modes.length > 1
|
||||
? html`
|
||||
<ha-icon-button
|
||||
.path=${
|
||||
this._chartType === "pie" ? mdiChartBar : mdiChartDonut
|
||||
}
|
||||
.label=${this._i18n.localize(
|
||||
"ui.panel.lovelace.cards.energy.energy_devices_graph.change_chart_type"
|
||||
)}
|
||||
@click=${this._handleChartTypeChange}
|
||||
></ha-icon-button>
|
||||
`
|
||||
: nothing
|
||||
}
|
||||
<div class="actions">
|
||||
${
|
||||
this._chartType === "bar" && this._legendData.length
|
||||
? this._renderDeviceSelect(shownCount)
|
||||
: nothing
|
||||
}
|
||||
${
|
||||
modes.length > 1
|
||||
? html`
|
||||
<ha-icon-button
|
||||
.path=${
|
||||
this._chartType === "pie" ? mdiChartBar : mdiChartDonut
|
||||
}
|
||||
.label=${this._i18n.localize(
|
||||
"ui.panel.lovelace.cards.energy.energy_devices_graph.change_chart_type"
|
||||
)}
|
||||
@click=${this._handleChartTypeChange}
|
||||
></ha-icon-button>
|
||||
`
|
||||
: nothing
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="content ${classMap({
|
||||
@@ -202,7 +222,7 @@ export class HuiEnergyDevicesGraphCard
|
||||
this._chartType,
|
||||
this._legendData
|
||||
)}
|
||||
.height=${`${Math.max(modes.includes("pie") ? 300 : 100, (this._legendData?.length || 0) * 28 + 50)}px`}
|
||||
.height=${`${Math.max(modes.includes("pie") ? 300 : 100, rows * 28 + 50)}px`}
|
||||
.sonificationLabelFormatter=${this._sonificationLabel}
|
||||
.extraComponents=${[PieChart]}
|
||||
.expandLegend=${this._config.expand_legend}
|
||||
@@ -217,6 +237,45 @@ export class HuiEnergyDevicesGraphCard
|
||||
`;
|
||||
}
|
||||
|
||||
private _renderDeviceSelect(shownCount: number) {
|
||||
const filtered = shownCount < this._legendData.length;
|
||||
return html`
|
||||
<ha-dropdown
|
||||
placement="bottom-end"
|
||||
@wa-select=${this._handleDeviceSelect}
|
||||
>
|
||||
<ha-filter-pane-chip
|
||||
slot="trigger"
|
||||
hide-label
|
||||
.label=${this._i18n.localize(
|
||||
"ui.panel.lovelace.cards.energy.energy_devices_graph.select_devices"
|
||||
)}
|
||||
.path=${mdiFormatListChecks}
|
||||
.count=${filtered ? shownCount : 0}
|
||||
.active=${filtered}
|
||||
></ha-filter-pane-chip>
|
||||
<ha-dropdown-item value=${SELECT_ALL}>
|
||||
${this._i18n.localize("ui.components.subpage-data-table.select_all")}
|
||||
</ha-dropdown-item>
|
||||
<ha-dropdown-item value=${SELECT_NONE}>
|
||||
${this._i18n.localize("ui.components.subpage-data-table.select_none")}
|
||||
</ha-dropdown-item>
|
||||
<wa-divider></wa-divider>
|
||||
${this._legendData.map(
|
||||
(d) => html`
|
||||
<ha-dropdown-item
|
||||
type="checkbox"
|
||||
.value=${d.id!}
|
||||
.checked=${!this._hiddenStats.includes(d.id!)}
|
||||
>
|
||||
${d.name}
|
||||
</ha-dropdown-item>
|
||||
`
|
||||
)}
|
||||
</ha-dropdown>
|
||||
`;
|
||||
}
|
||||
|
||||
private _renderTooltip = (params: any) => {
|
||||
const deviceName = this._getDeviceName(params.name);
|
||||
const area = computeEnergyStatisticArea(this._registries, params.name);
|
||||
@@ -334,12 +393,11 @@ export class HuiEnergyDevicesGraphCard
|
||||
type: "custom",
|
||||
show: true,
|
||||
data: legendData,
|
||||
selected: legendData
|
||||
.filter((d) => d.id && this._hiddenStats.includes(d.id))
|
||||
.reduce((acc, d) => {
|
||||
acc[d.id!] = false;
|
||||
return acc;
|
||||
}, {}),
|
||||
selected: Object.fromEntries(
|
||||
legendData
|
||||
.filter((d) => d.id)
|
||||
.map((d) => [d.id, !this._hiddenStats.includes(d.id!)])
|
||||
),
|
||||
};
|
||||
}
|
||||
return options;
|
||||
@@ -698,6 +756,25 @@ export class HuiEnergyDevicesGraphCard
|
||||
this._getStatistics(this._data!);
|
||||
}
|
||||
|
||||
private _handleDeviceSelect(ev: HaDropdownSelectEvent) {
|
||||
ev.preventDefault(); // keep the dropdown open to pick several devices
|
||||
const { value, checked } = ev.detail.item;
|
||||
const ids = this._legendData.map((d) => d.id!);
|
||||
if (value === SELECT_ALL) {
|
||||
this._hiddenStats = this._hiddenStats.filter((id) => !ids.includes(id));
|
||||
} else if (value === SELECT_NONE) {
|
||||
this._hiddenStats = [
|
||||
...this._hiddenStats,
|
||||
...ids.filter((id) => !this._hiddenStats.includes(id)),
|
||||
];
|
||||
} else if (checked) {
|
||||
this._hiddenStats = this._hiddenStats.filter((id) => id !== value);
|
||||
} else {
|
||||
this._hiddenStats = [...this._hiddenStats, value];
|
||||
}
|
||||
this._getStatistics(this._data!);
|
||||
}
|
||||
|
||||
private _datasetHidden(ev: CustomEvent<{ id: string }>) {
|
||||
this._hiddenStats = [...this._hiddenStats, ev.detail.id];
|
||||
this._getStatistics(this._data!);
|
||||
@@ -717,6 +794,11 @@ export class HuiEnergyDevicesGraphCard
|
||||
align-items: center;
|
||||
padding-bottom: 0;
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--ha-space-2);
|
||||
}
|
||||
.content {
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
@@ -10677,6 +10677,7 @@
|
||||
"previous_energy_usage": "Previous energy",
|
||||
"total_energy_usage": "Total",
|
||||
"change_chart_type": "Change chart type",
|
||||
"select_devices": "Select devices",
|
||||
"untracked_consumption": "Untracked consumption",
|
||||
"untracked": "untracked"
|
||||
},
|
||||
|
||||
@@ -66,7 +66,7 @@ describe("ha-chart-base updates", () => {
|
||||
expect(dispose).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("only flags the hidden datasets when the legend hides a new one", () => {
|
||||
it("only flags the hidden datasets when the legend changes one", () => {
|
||||
const { element, internals } = createChart();
|
||||
const requestUpdate = vi.spyOn(element as any, "requestUpdate");
|
||||
const hiddenUpdates = () =>
|
||||
@@ -84,5 +84,10 @@ describe("ha-chart-base updates", () => {
|
||||
|
||||
internals._updateHiddenStatsFromOptions(internals.options);
|
||||
expect(hiddenUpdates()).toBe(1);
|
||||
|
||||
internals.options = { legend: { selected: { power: true } } };
|
||||
internals._updateHiddenStatsFromOptions(internals.options);
|
||||
expect(hiddenUpdates()).toBe(2);
|
||||
expect(internals._hiddenDatasets.has("power")).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user