mirror of
https://github.com/home-assistant/frontend.git
synced 2026-04-02 00:27:49 +01:00
Extract computeStatMidpoint helper to deduplicate compare midpoint logic
This commit is contained in:
@@ -382,6 +382,21 @@ export function fillLineGaps(datasets: LineSeriesOption[]) {
|
||||
return datasets;
|
||||
}
|
||||
|
||||
export function computeStatMidpoint(
|
||||
start: number,
|
||||
end: number,
|
||||
compareTransform?: (ts: Date) => Date
|
||||
): number {
|
||||
if (compareTransform) {
|
||||
return (
|
||||
(compareTransform(new Date(start)).getTime() +
|
||||
compareTransform(new Date(end)).getTime()) /
|
||||
2
|
||||
);
|
||||
}
|
||||
return (start + end) / 2;
|
||||
}
|
||||
|
||||
export function getCompareTransform(start: Date, compareStart?: Date) {
|
||||
if (!compareStart) {
|
||||
return (ts: Date) => ts;
|
||||
|
||||
@@ -32,6 +32,7 @@ import type { LovelaceCard } from "../../types";
|
||||
import type { EnergyDevicesDetailGraphCardConfig } from "../types";
|
||||
import { hasConfigChanged } from "../../common/has-changed";
|
||||
import {
|
||||
computeStatMidpoint,
|
||||
fillDataGapsAndRoundCaps,
|
||||
getCommonOptions,
|
||||
getCompareTransform,
|
||||
@@ -502,15 +503,14 @@ export class HuiEnergyDevicesDetailGraphCard
|
||||
});
|
||||
|
||||
const dataPoint = [
|
||||
(point.start + point.end) / 2,
|
||||
computeStatMidpoint(
|
||||
point.start,
|
||||
point.end,
|
||||
compare ? compareTransform : undefined
|
||||
),
|
||||
point.change - sumChildren,
|
||||
point.start,
|
||||
];
|
||||
if (compare) {
|
||||
const s = compareTransform(new Date(point.start));
|
||||
const e = compareTransform(new Date(point.end));
|
||||
dataPoint[0] = (s.getTime() + e.getTime()) / 2;
|
||||
}
|
||||
consumptionData.push(dataPoint);
|
||||
prevStart = point.start;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ import type { LovelaceCard } from "../../types";
|
||||
import type { EnergyGasGraphCardConfig } from "../types";
|
||||
import { hasConfigChanged } from "../../common/has-changed";
|
||||
import {
|
||||
computeStatMidpoint,
|
||||
fillDataGapsAndRoundCaps,
|
||||
getCommonOptions,
|
||||
getCompareTransform,
|
||||
@@ -286,15 +287,14 @@ export class HuiEnergyGasGraphCard
|
||||
continue;
|
||||
}
|
||||
const dataPoint: (Date | string | number)[] = [
|
||||
(point.start + point.end) / 2,
|
||||
computeStatMidpoint(
|
||||
point.start,
|
||||
point.end,
|
||||
compare ? compareTransform : undefined
|
||||
),
|
||||
point.change,
|
||||
point.start,
|
||||
];
|
||||
if (compare) {
|
||||
const s = compareTransform(new Date(point.start));
|
||||
const e = compareTransform(new Date(point.end));
|
||||
dataPoint[0] = (s.getTime() + e.getTime()) / 2;
|
||||
}
|
||||
gasConsumptionData.push(dataPoint);
|
||||
prevStart = point.start;
|
||||
}
|
||||
|
||||
@@ -30,6 +30,7 @@ import type { LovelaceCard } from "../../types";
|
||||
import type { EnergySolarGraphCardConfig } from "../types";
|
||||
import { hasConfigChanged } from "../../common/has-changed";
|
||||
import {
|
||||
computeStatMidpoint,
|
||||
fillDataGapsAndRoundCaps,
|
||||
getCommonOptions,
|
||||
getCompareTransform,
|
||||
@@ -309,15 +310,14 @@ export class HuiEnergySolarGraphCard
|
||||
continue;
|
||||
}
|
||||
const dataPoint: (Date | string | number)[] = [
|
||||
(point.start + point.end) / 2,
|
||||
computeStatMidpoint(
|
||||
point.start,
|
||||
point.end,
|
||||
compare ? compareTransform : undefined
|
||||
),
|
||||
point.change,
|
||||
point.start,
|
||||
];
|
||||
if (compare) {
|
||||
const s = compareTransform(new Date(point.start));
|
||||
const e = compareTransform(new Date(point.end));
|
||||
dataPoint[0] = (s.getTime() + e.getTime()) / 2;
|
||||
}
|
||||
solarProductionData.push(dataPoint);
|
||||
prevStart = point.start;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ import type { LovelaceCard } from "../../types";
|
||||
import type { EnergyWaterGraphCardConfig } from "../types";
|
||||
import { hasConfigChanged } from "../../common/has-changed";
|
||||
import {
|
||||
computeStatMidpoint,
|
||||
fillDataGapsAndRoundCaps,
|
||||
getCommonOptions,
|
||||
getCompareTransform,
|
||||
@@ -286,15 +287,14 @@ export class HuiEnergyWaterGraphCard
|
||||
continue;
|
||||
}
|
||||
const dataPoint: (Date | string | number)[] = [
|
||||
(point.start + point.end) / 2,
|
||||
computeStatMidpoint(
|
||||
point.start,
|
||||
point.end,
|
||||
compare ? compareTransform : undefined
|
||||
),
|
||||
point.change,
|
||||
point.start,
|
||||
];
|
||||
if (compare) {
|
||||
const s = compareTransform(new Date(point.start));
|
||||
const e = compareTransform(new Date(point.end));
|
||||
dataPoint[0] = (s.getTime() + e.getTime()) / 2;
|
||||
}
|
||||
waterConsumptionData.push(dataPoint);
|
||||
prevStart = point.start;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user