mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 11:16:35 +00:00
Improvements to stat charts (#14427)
This commit is contained in:
parent
f95a3c75f6
commit
8b817b35b0
@ -197,6 +197,7 @@ class StatisticsChart extends LitElement {
|
|||||||
elements: {
|
elements: {
|
||||||
line: {
|
line: {
|
||||||
tension: 0.4,
|
tension: 0.4,
|
||||||
|
cubicInterpolationMode: "monotone",
|
||||||
borderWidth: 1.5,
|
borderWidth: 1.5,
|
||||||
},
|
},
|
||||||
bar: { borderWidth: 1.5, borderRadius: 4 },
|
bar: { borderWidth: 1.5, borderRadius: 4 },
|
||||||
@ -280,33 +281,38 @@ class StatisticsChart extends LitElement {
|
|||||||
|
|
||||||
// array containing [value1, value2, etc]
|
// array containing [value1, value2, etc]
|
||||||
let prevValues: Array<number | null> | null = null;
|
let prevValues: Array<number | null> | null = null;
|
||||||
|
let prevEndTime: Date | undefined;
|
||||||
|
|
||||||
// The datasets for the current statistic
|
// The datasets for the current statistic
|
||||||
const statDataSets: ChartDataset<"line">[] = [];
|
const statDataSets: ChartDataset<"line">[] = [];
|
||||||
|
|
||||||
const pushData = (
|
const pushData = (
|
||||||
timestamp: Date,
|
start: Date,
|
||||||
|
end: Date,
|
||||||
dataValues: Array<number | null> | null
|
dataValues: Array<number | null> | null
|
||||||
) => {
|
) => {
|
||||||
if (!dataValues) return;
|
if (!dataValues) return;
|
||||||
if (timestamp > endTime) {
|
if (start > end) {
|
||||||
// Drop data points that are after the requested endTime. This could happen if
|
// Drop data points that are after the requested endTime. This could happen if
|
||||||
// endTime is "now" and client time is not in sync with server time.
|
// endTime is "now" and client time is not in sync with server time.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
statDataSets.forEach((d, i) => {
|
statDataSets.forEach((d, i) => {
|
||||||
if (dataValues[i] === null && prevValues && prevValues[i] !== null) {
|
if (
|
||||||
// null data values show up as gaps in the chart.
|
prevEndTime &&
|
||||||
// If the current value for the dataset is null and the previous
|
prevValues &&
|
||||||
// value of the data set is not null, then add an 'end' point
|
prevEndTime.getTime() !== start.getTime()
|
||||||
// to the chart for the previous value. Otherwise the gap will
|
) {
|
||||||
// be too big. It will go from the start of the previous data
|
// if the end of the previous data doesn't match the start of the current data,
|
||||||
// value until the start of the next data value.
|
// we have to draw a gap so add a value at the end time, and then an empty value.
|
||||||
d.data.push({ x: timestamp.getTime(), y: prevValues[i]! });
|
d.data.push({ x: prevEndTime.getTime(), y: prevValues[i]! });
|
||||||
|
// @ts-expect-error
|
||||||
|
d.data.push({ x: prevEndTime.getTime(), y: null });
|
||||||
}
|
}
|
||||||
d.data.push({ x: timestamp.getTime(), y: dataValues[i]! });
|
d.data.push({ x: start.getTime(), y: dataValues[i]! });
|
||||||
});
|
});
|
||||||
prevValues = dataValues;
|
prevValues = dataValues;
|
||||||
|
prevEndTime = end;
|
||||||
};
|
};
|
||||||
|
|
||||||
const color = getGraphColorByIndex(colorIndex, this._computedStyle!);
|
const color = getGraphColorByIndex(colorIndex, this._computedStyle!);
|
||||||
@ -365,11 +371,11 @@ class StatisticsChart extends LitElement {
|
|||||||
let firstSum: number | null | undefined = null;
|
let firstSum: number | null | undefined = null;
|
||||||
let prevSum: number | null | undefined = null;
|
let prevSum: number | null | undefined = null;
|
||||||
stats.forEach((stat) => {
|
stats.forEach((stat) => {
|
||||||
const date = new Date(stat.start);
|
const startDate = new Date(stat.start);
|
||||||
if (prevDate === date) {
|
if (prevDate === startDate) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
prevDate = date;
|
prevDate = startDate;
|
||||||
const dataValues: Array<number | null> = [];
|
const dataValues: Array<number | null> = [];
|
||||||
statTypes.forEach((type) => {
|
statTypes.forEach((type) => {
|
||||||
let val: number | null | undefined;
|
let val: number | null | undefined;
|
||||||
@ -396,7 +402,7 @@ class StatisticsChart extends LitElement {
|
|||||||
: null
|
: null
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
pushData(date, dataValues);
|
pushData(startDate, new Date(stat.end), dataValues);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Concat two arrays
|
// Concat two arrays
|
||||||
|
@ -85,13 +85,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||||||
if (!this.hasUpdated) {
|
if (!this.hasUpdated) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._getStatistics();
|
this._setFetchStatisticsTimer();
|
||||||
// statistics are created every hour
|
|
||||||
clearInterval(this._interval);
|
|
||||||
this._interval = window.setInterval(
|
|
||||||
() => this._getStatistics(),
|
|
||||||
this._intervalTimeout
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public getCardSize(): number {
|
public getCardSize(): number {
|
||||||
@ -138,10 +132,7 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||||||
|
|
||||||
public willUpdate(changedProps: PropertyValues) {
|
public willUpdate(changedProps: PropertyValues) {
|
||||||
super.willUpdate(changedProps);
|
super.willUpdate(changedProps);
|
||||||
if (
|
if (!this._config || !changedProps.has("_config")) {
|
||||||
!this._config ||
|
|
||||||
(!changedProps.has("_config") && !changedProps.has("_metadata"))
|
|
||||||
) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -153,17 +144,24 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||||||
changedProps.has("_config") &&
|
changedProps.has("_config") &&
|
||||||
oldConfig?.entities !== this._config.entities
|
oldConfig?.entities !== this._config.entities
|
||||||
) {
|
) {
|
||||||
this._getStatisticsMetaData(this._entities);
|
this._getStatisticsMetaData(this._entities).then(() => {
|
||||||
|
this._setFetchStatisticsTimer();
|
||||||
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
changedProps.has("_metadata") ||
|
changedProps.has("_config") &&
|
||||||
(changedProps.has("_config") &&
|
|
||||||
(oldConfig?.entities !== this._config.entities ||
|
(oldConfig?.entities !== this._config.entities ||
|
||||||
oldConfig?.days_to_show !== this._config.days_to_show ||
|
oldConfig?.days_to_show !== this._config.days_to_show ||
|
||||||
oldConfig?.period !== this._config.period ||
|
oldConfig?.period !== this._config.period ||
|
||||||
oldConfig?.unit !== this._config.unit))
|
oldConfig?.unit !== this._config.unit)
|
||||||
) {
|
) {
|
||||||
|
this._setFetchStatisticsTimer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private _setFetchStatisticsTimer() {
|
||||||
this._getStatistics();
|
this._getStatistics();
|
||||||
// statistics are created every hour
|
// statistics are created every hour
|
||||||
clearInterval(this._interval);
|
clearInterval(this._interval);
|
||||||
@ -172,7 +170,6 @@ export class HuiStatisticsGraphCard extends LitElement implements LovelaceCard {
|
|||||||
this._intervalTimeout
|
this._intervalTimeout
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
if (!this.hass || !this._config) {
|
if (!this.hass || !this._config) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user