Fix bar chart data order when using the legend (#25832)

* Fix bar chart data order when using the legend

* type fix
This commit is contained in:
Petar Petrov 2025-06-19 18:49:58 +03:00 committed by GitHub
parent 8ee80586a8
commit f7634c45c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 43 deletions

View File

@ -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() {

View File

@ -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();