Fix duplicate legend items when comparing energy data (#25610)

This commit is contained in:
Petar Petrov 2025-05-27 21:43:32 +03:00 committed by GitHub
parent 189067d14b
commit 7c5bf26240
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 14 deletions

View File

@ -220,11 +220,11 @@ export class HaChartBase extends LitElement {
return nothing;
}
const datasets = ensureArray(this.data);
const items = (legend.data ||
datasets
const items: LegendComponentOption["data"] =
legend.data ||
((datasets
.filter((d) => (d.data as any[])?.length && (d.id || d.name))
.map((d) => d.name ?? d.id) ||
[]) as string[];
.map((d) => d.name ?? d.id) || []) as string[]);
const isMobile = window.matchMedia(
"all and (max-width: 450px), all and (max-height: 500px)"
@ -239,20 +239,32 @@ export class HaChartBase extends LitElement {
})}
>
<ul>
${items.map((item: string, index: number) => {
${items.map((item, index) => {
if (!this.expandLegend && index >= overflowLimit) {
return nothing;
}
const dataset = datasets.find(
(d) => d.id === item || d.name === item
);
const color = dataset?.color as string;
const borderColor = dataset?.itemStyle?.borderColor as string;
let itemStyle: Record<string, any> = {};
let name = "";
if (typeof item === "string") {
name = item;
const dataset = datasets.find(
(d) => d.id === item || d.name === item
);
itemStyle = {
color: dataset?.color as string,
...(dataset?.itemStyle as { borderColor?: string }),
};
} else {
name = item.name ?? "";
itemStyle = item.itemStyle ?? {};
}
const color = itemStyle?.color as string;
const borderColor = itemStyle?.borderColor as string;
return html`<li
.name=${item}
.name=${name}
@click=${this._legendClick}
class=${classMap({ hidden: this._hiddenDatasets.has(item) })}
.title=${item}
class=${classMap({ hidden: this._hiddenDatasets.has(name) })}
.title=${name}
>
<div
class="bullet"
@ -261,7 +273,7 @@ export class HaChartBase extends LitElement {
borderColor: borderColor || color,
})}
></div>
<div class="label">${item}</div>
<div class="label">${name}</div>
</li>`;
})}
${items.length > overflowLimit

View File

@ -6,6 +6,7 @@ import { customElement, property, state } from "lit/decorators";
import { classMap } from "lit/directives/class-map";
import memoizeOne from "memoize-one";
import type { BarSeriesOption } from "echarts/charts";
import type { LegendComponentOption } from "echarts/components";
import { getGraphColorByIndex } from "../../../../common/color/colors";
import { getEnergyColor } from "./common/color";
import "../../../../components/ha-card";
@ -54,6 +55,8 @@ export class HuiEnergyDevicesDetailGraphCard
@state() private _data?: EnergyData;
@state() private _legendData?: LegendComponentOption["data"];
@state() private _start = startOfToday();
@state() private _end = endOfToday();
@ -185,6 +188,7 @@ export class HuiEnergyDevicesDetailGraphCard
legend: {
show: true,
type: "custom",
data: this._legendData,
selected: this._hiddenStats.reduce((acc, stat) => {
acc[stat] = false;
return acc;
@ -310,6 +314,13 @@ export class HuiEnergyDevicesDetailGraphCard
);
datasets.push(...processedData);
this._legendData = processedData.map((d) => ({
name: d.name as string,
itemStyle: {
color: d.color as string,
borderColor: d.itemStyle?.borderColor as string,
},
}));
if (showUntracked) {
const untrackedData = this._processUntracked(
@ -319,6 +330,13 @@ export class HuiEnergyDevicesDetailGraphCard
false
);
datasets.push(untrackedData);
this._legendData.push({
name: untrackedData.name as string,
itemStyle: {
color: untrackedData.color as string,
borderColor: untrackedData.itemStyle?.borderColor as string,
},
});
}
fillDataGapsAndRoundCaps(datasets);