mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-28 11:46:42 +00:00
Fix duplicate legend items when comparing energy data (#25610)
This commit is contained in:
parent
189067d14b
commit
7c5bf26240
@ -220,11 +220,11 @@ export class HaChartBase extends LitElement {
|
|||||||
return nothing;
|
return nothing;
|
||||||
}
|
}
|
||||||
const datasets = ensureArray(this.data);
|
const datasets = ensureArray(this.data);
|
||||||
const items = (legend.data ||
|
const items: LegendComponentOption["data"] =
|
||||||
datasets
|
legend.data ||
|
||||||
|
((datasets
|
||||||
.filter((d) => (d.data as any[])?.length && (d.id || d.name))
|
.filter((d) => (d.data as any[])?.length && (d.id || d.name))
|
||||||
.map((d) => d.name ?? d.id) ||
|
.map((d) => d.name ?? d.id) || []) as string[]);
|
||||||
[]) as string[];
|
|
||||||
|
|
||||||
const isMobile = window.matchMedia(
|
const isMobile = window.matchMedia(
|
||||||
"all and (max-width: 450px), all and (max-height: 500px)"
|
"all and (max-width: 450px), all and (max-height: 500px)"
|
||||||
@ -239,20 +239,32 @@ export class HaChartBase extends LitElement {
|
|||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
<ul>
|
<ul>
|
||||||
${items.map((item: string, index: number) => {
|
${items.map((item, index) => {
|
||||||
if (!this.expandLegend && index >= overflowLimit) {
|
if (!this.expandLegend && index >= overflowLimit) {
|
||||||
return nothing;
|
return nothing;
|
||||||
}
|
}
|
||||||
const dataset = datasets.find(
|
let itemStyle: Record<string, any> = {};
|
||||||
(d) => d.id === item || d.name === item
|
let name = "";
|
||||||
);
|
if (typeof item === "string") {
|
||||||
const color = dataset?.color as string;
|
name = item;
|
||||||
const borderColor = dataset?.itemStyle?.borderColor as string;
|
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
|
return html`<li
|
||||||
.name=${item}
|
.name=${name}
|
||||||
@click=${this._legendClick}
|
@click=${this._legendClick}
|
||||||
class=${classMap({ hidden: this._hiddenDatasets.has(item) })}
|
class=${classMap({ hidden: this._hiddenDatasets.has(name) })}
|
||||||
.title=${item}
|
.title=${name}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
class="bullet"
|
class="bullet"
|
||||||
@ -261,7 +273,7 @@ export class HaChartBase extends LitElement {
|
|||||||
borderColor: borderColor || color,
|
borderColor: borderColor || color,
|
||||||
})}
|
})}
|
||||||
></div>
|
></div>
|
||||||
<div class="label">${item}</div>
|
<div class="label">${name}</div>
|
||||||
</li>`;
|
</li>`;
|
||||||
})}
|
})}
|
||||||
${items.length > overflowLimit
|
${items.length > overflowLimit
|
||||||
|
@ -6,6 +6,7 @@ import { customElement, property, state } from "lit/decorators";
|
|||||||
import { classMap } from "lit/directives/class-map";
|
import { classMap } from "lit/directives/class-map";
|
||||||
import memoizeOne from "memoize-one";
|
import memoizeOne from "memoize-one";
|
||||||
import type { BarSeriesOption } from "echarts/charts";
|
import type { BarSeriesOption } from "echarts/charts";
|
||||||
|
import type { LegendComponentOption } from "echarts/components";
|
||||||
import { getGraphColorByIndex } from "../../../../common/color/colors";
|
import { getGraphColorByIndex } from "../../../../common/color/colors";
|
||||||
import { getEnergyColor } from "./common/color";
|
import { getEnergyColor } from "./common/color";
|
||||||
import "../../../../components/ha-card";
|
import "../../../../components/ha-card";
|
||||||
@ -54,6 +55,8 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
|
|
||||||
@state() private _data?: EnergyData;
|
@state() private _data?: EnergyData;
|
||||||
|
|
||||||
|
@state() private _legendData?: LegendComponentOption["data"];
|
||||||
|
|
||||||
@state() private _start = startOfToday();
|
@state() private _start = startOfToday();
|
||||||
|
|
||||||
@state() private _end = endOfToday();
|
@state() private _end = endOfToday();
|
||||||
@ -185,6 +188,7 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
legend: {
|
legend: {
|
||||||
show: true,
|
show: true,
|
||||||
type: "custom",
|
type: "custom",
|
||||||
|
data: this._legendData,
|
||||||
selected: this._hiddenStats.reduce((acc, stat) => {
|
selected: this._hiddenStats.reduce((acc, stat) => {
|
||||||
acc[stat] = false;
|
acc[stat] = false;
|
||||||
return acc;
|
return acc;
|
||||||
@ -310,6 +314,13 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
);
|
);
|
||||||
|
|
||||||
datasets.push(...processedData);
|
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) {
|
if (showUntracked) {
|
||||||
const untrackedData = this._processUntracked(
|
const untrackedData = this._processUntracked(
|
||||||
@ -319,6 +330,13 @@ export class HuiEnergyDevicesDetailGraphCard
|
|||||||
false
|
false
|
||||||
);
|
);
|
||||||
datasets.push(untrackedData);
|
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);
|
fillDataGapsAndRoundCaps(datasets);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user