Compare commits

...
1 Commits
Author SHA1 Message Date
Petar Petrov da3ad9ff91 Stop drawing idle power sources on the power graph axis 2026-11-19 10:49:47 +01:00
3 changed files with 1005 additions and 1438 deletions

No files matched your search

@@ -81,6 +81,24 @@ function processData(
return { positive, negative };
}
type LineData = NonNullable<LineSeriesOption["data"]>;
// A series that stays at 0 would draw its line over the x axis, so break the
// line there and keep only the zeros next to a non-zero value.
const hideZeroRuns = (data: LineData): LineData => {
const values = data.map(
(point) =>
(typeof point === "object" && "value" in point!
? point.value
: point) as [number, number]
);
return data.map((point, i) =>
values[i][1] === 0 && !values[i - 1]?.[1] && !values[i + 1]?.[1]
? [values[i][0], null]
: point
);
};
const sourceSeriesId = (type: PowerSourceType, statRate: string) =>
`${type}-${statRate}`;
@@ -498,6 +516,9 @@ export function generatePowerSourcesGraphData(
usageData.push([(stat.start + stat.end) / 2, Math.max(0, stat.mean!)]);
}
}
for (const series of chartData) {
series.data = hideZeroRuns(series.data!);
}
chartData.push({
...commonSeriesOptions,
id: "usage",
File diff suppressed because it is too large. Load diff
@@ -474,6 +474,30 @@ describe("generatePowerSourcesGraphData with separate sources", () => {
});
});
describe("generatePowerSourcesGraphData with idle sources", () => {
it("breaks the line where a source stays at 0, keeping the slope to 0", () => {
const result = generatePowerSourcesGraphData({
localize: mockLocalize,
formatPower: (w: number) => `${w} W`,
states: {} as HassEntities,
computedStyles,
energyData: buildHomeEnergyData([solarSource], {
[RATE_IDS.solar]: hourlyStats([0, 0, 0, 2, 0, 0]),
}),
start: new Date(FIXED_EPOCH_MS),
end: new Date(FIXED_EPOCH_MS + dayMs),
now: FIXED_EPOCH_MS + dayMs + 60 * 60 * 1000,
});
const solar = result.chartData.find((series) => series.id === "solar")!;
expect(
(solar.data as ({ value: number[] } | number[])[]).map(
(p) => ("value" in p ? p.value : p)[1]
)
).toEqual([null, null, 0, 2, 0, null]);
});
});
describe("getPowerLegendValues with a home meter", () => {
const format = (w: number) => `${w} W`;
const power = (id: string, value: string) =>