mirror of
https://github.com/home-assistant/frontend.git
synced 2025-08-02 14:07:55 +00:00
Address review comments
This commit is contained in:
parent
131957fbb3
commit
0f534423a4
@ -30,6 +30,15 @@ import {
|
||||
import type { HomeAssistant } from "../../types";
|
||||
import "./ha-chart-base";
|
||||
|
||||
export type ExtendedStatisticType = StatisticType | "state" | "sum_rel";
|
||||
|
||||
export const statTypeMap: Record<ExtendedStatisticType, StatisticType> = {
|
||||
mean: "mean",
|
||||
min: "min",
|
||||
max: "max",
|
||||
sum: "sum",
|
||||
sum_rel: "sum",
|
||||
};
|
||||
@customElement("statistics-chart")
|
||||
class StatisticsChart extends LitElement {
|
||||
@property({ attribute: false }) public hass!: HomeAssistant;
|
||||
@ -42,7 +51,7 @@ class StatisticsChart extends LitElement {
|
||||
|
||||
@property({ attribute: false }) public endTime?: Date;
|
||||
|
||||
@property({ type: Array }) public statTypes: Array<StatisticType> = [
|
||||
@property({ type: Array }) public statTypes: Array<ExtendedStatisticType> = [
|
||||
"sum",
|
||||
"min",
|
||||
"mean",
|
||||
@ -307,7 +316,7 @@ class StatisticsChart extends LitElement {
|
||||
: this.statTypes;
|
||||
|
||||
sortedTypes.forEach((type) => {
|
||||
if (statisticsHaveType(stats, type)) {
|
||||
if (statisticsHaveType(stats, statTypeMap[type])) {
|
||||
const band = drawBands && (type === "min" || type === "max");
|
||||
statTypes.push(type);
|
||||
statDataSets.push({
|
||||
@ -335,8 +344,14 @@ class StatisticsChart extends LitElement {
|
||||
|
||||
let prevDate: Date | null = null;
|
||||
// Process chart data.
|
||||
let initVal: number | null = null;
|
||||
let prevSum: number | null = null;
|
||||
const initVal: Record<"sum" | "sum_rel", number | null> = {
|
||||
sum: null,
|
||||
sum_rel: null,
|
||||
};
|
||||
const prevSum: Record<"sum" | "sum_rel", number | null> = {
|
||||
sum: null,
|
||||
sum_rel: null,
|
||||
};
|
||||
stats.forEach((stat) => {
|
||||
const date = new Date(stat.start);
|
||||
if (prevDate === date) {
|
||||
@ -347,11 +362,11 @@ class StatisticsChart extends LitElement {
|
||||
statTypes.forEach((type) => {
|
||||
let val: number | null;
|
||||
if (type === "sum" || type === "sum_rel") {
|
||||
if (initVal === null) {
|
||||
initVal = val = type === "sum_rel" ? 0 : stat.sum || 0;
|
||||
prevSum = stat.sum;
|
||||
if (initVal[type] === null) {
|
||||
initVal[type] = val = type === "sum_rel" ? 0 : stat.sum || 0;
|
||||
prevSum[type] = stat.sum;
|
||||
} else {
|
||||
val = initVal + ((stat.sum || 0) - prevSum!);
|
||||
val = initVal[type]! + ((stat.sum || 0) - prevSum[type]!);
|
||||
}
|
||||
} else {
|
||||
val = stat[type];
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { computeStateName } from "../common/entity/compute_state_name";
|
||||
import { HomeAssistant } from "../types";
|
||||
|
||||
export type StatisticType = "sum" | "sum_rel" | "min" | "max" | "mean";
|
||||
export type StatisticType = "sum" | "min" | "max" | "mean";
|
||||
|
||||
export interface Statistics {
|
||||
[statisticId: string]: StatisticValue[];
|
||||
@ -201,13 +201,10 @@ export const calculateStatisticsSumGrowth = (
|
||||
export const statisticsHaveType = (
|
||||
stats: StatisticValue[],
|
||||
type: StatisticType
|
||||
) => {
|
||||
type = type === "sum_rel" ? "sum" : type;
|
||||
return stats.some((stat) => stat[type] !== null);
|
||||
};
|
||||
) => stats.some((stat) => stat[type] !== null);
|
||||
|
||||
const mean_stat_types: readonly StatisticType[] = ["mean", "min", "max"];
|
||||
const sum_stat_types: readonly StatisticType[] = ["sum", "sum_rel"];
|
||||
const sum_stat_types: readonly StatisticType[] = ["sum"];
|
||||
|
||||
export const statisticsMetaHasType = (
|
||||
metadata: StatisticsMetaData,
|
||||
|
@ -36,6 +36,7 @@ import {
|
||||
statisticsMetaHasType,
|
||||
} from "../../../../data/recorder";
|
||||
import { deepEqual } from "../../../../common/util/deep-equal";
|
||||
import { statTypeMap } from "../../../../components/chart/statistics-chart";
|
||||
|
||||
const statTypeStruct = union([
|
||||
literal("sum"),
|
||||
@ -157,7 +158,7 @@ export class HuiStatisticsGraphCardEditor
|
||||
disabled:
|
||||
!metaDatas ||
|
||||
!metaDatas?.every((metaData) =>
|
||||
statisticsMetaHasType(metaData, stat_type)
|
||||
statisticsMetaHasType(metaData, statTypeMap[stat_type])
|
||||
),
|
||||
})),
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user