From 7f7e6935475272ad1b7e29f082a56487fba2276c Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Thu, 19 Jun 2025 18:49:58 +0300 Subject: [PATCH] Fix bar chart data order when using the legend (#25832) * Fix bar chart data order when using the legend * type fix --- src/components/chart/ha-chart-base.ts | 77 ++++++++++--------- .../hui-energy-devices-detail-graph-card.ts | 11 ++- 2 files changed, 45 insertions(+), 43 deletions(-) diff --git a/src/components/chart/ha-chart-base.ts b/src/components/chart/ha-chart-base.ts index b40256b60b..fdaad85c83 100644 --- a/src/components/chart/ha-chart-base.ts +++ b/src/components/chart/ha-chart-base.ts @@ -9,6 +9,7 @@ import type { LegendComponentOption, XAXisOption, YAXisOption, + LineSeriesOption, } from "echarts/types/dist/shared"; import type { PropertyValues } from "lit"; import { css, html, LitElement, nothing } from "lit"; @@ -642,44 +643,46 @@ export class HaChartBase extends LitElement { const yAxis = (this.options?.yAxis?.[0] ?? this.options?.yAxis) as | YAXisOption | undefined; - const series = ensureArray(this.data) - .filter((d) => !this._hiddenDatasets.has(String(d.name ?? d.id))) - .map((s) => { - if (s.type === "line") { - if (yAxis?.type === "log") { - // set <=0 values to null so they render as gaps on a log graph - return { - ...s, - data: s.data?.map((v) => - Array.isArray(v) - ? [ - v[0], - typeof v[1] !== "number" || v[1] > 0 ? v[1] : null, - ...v.slice(2), - ] - : v - ), - }; - } - if (s.sampling === "minmax") { - const minX = - xAxis?.min && typeof xAxis.min === "number" - ? xAxis.min - : undefined; - const maxX = - xAxis?.max && typeof xAxis.max === "number" - ? xAxis.max - : undefined; - return { - ...s, - sampling: undefined, - data: downSampleLineData(s.data, this.clientWidth, minX, maxX), - }; - } + const series = ensureArray(this.data).map((s) => { + const data = this._hiddenDatasets.has(String(s.name ?? s.id)) + ? undefined + : s.data; + if (data && s.type === "line") { + if (yAxis?.type === "log") { + // set <=0 values to null so they render as gaps on a log graph + return { + ...s, + data: (data as LineSeriesOption["data"])!.map((v) => + Array.isArray(v) + ? [ + v[0], + typeof v[1] !== "number" || v[1] > 0 ? v[1] : null, + ...v.slice(2), + ] + : v + ), + }; } - return s; - }); - return series; + if (s.sampling === "minmax") { + const minX = + xAxis?.min && typeof xAxis.min === "number" ? xAxis.min : undefined; + const maxX = + xAxis?.max && typeof xAxis.max === "number" ? xAxis.max : undefined; + return { + ...s, + sampling: undefined, + data: downSampleLineData( + data as LineSeriesOption["data"], + this.clientWidth, + minX, + maxX + ), + }; + } + } + return { ...s, data }; + }); + return series as ECOption["series"]; } private _getDefaultHeight() { diff --git a/src/panels/lovelace/cards/energy/hui-energy-devices-detail-graph-card.ts b/src/panels/lovelace/cards/energy/hui-energy-devices-detail-graph-card.ts index 917d9e5950..c4ef01c62a 100644 --- a/src/panels/lovelace/cards/energy/hui-energy-devices-detail-graph-card.ts +++ b/src/panels/lovelace/cards/energy/hui-energy-devices-detail-graph-card.ts @@ -81,7 +81,6 @@ export class HuiEnergyDevicesDetailGraphCard key: this._config?.collection_key, }).subscribe((data) => { this._data = data; - this._processStatistics(); }), ]; } @@ -103,10 +102,7 @@ export class HuiEnergyDevicesDetailGraphCard } protected willUpdate(changedProps: PropertyValues) { - if ( - (changedProps.has("_hiddenStats") || changedProps.has("_config")) && - this._data - ) { + if (changedProps.has("_config") || changedProps.has("_data")) { this._processStatistics(); } } @@ -206,7 +202,10 @@ export class HuiEnergyDevicesDetailGraphCard ); private _processStatistics() { - const energyData = this._data!; + if (!this._data) { + return; + } + const energyData = this._data; this._start = energyData.start; this._end = energyData.end || endOfToday();