From 9a9986cf17318ece53886beef69baf962c397c60 Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Mon, 19 Nov 2018 10:47:30 -0500 Subject: [PATCH] Flatline fix for Sensor Cards (#2064) * Update hui-sensor-card.js This fixes issues where the first history item was repeated many times at the start of the graph resulting in a flat line that does not represent the data. Also, the graph now has the ability to reach a 1 to 1 history graph to sensor graph point representation. Before 2 to 1 was the highest resolution possible. This was due to using history.length - 1 as the denominator in cases where the user set the accuracy larger than the total number of points in the history. * Update hui-sensor-card.js * Update hui-sensor-card.js * Update hui-sensor-card.js * Update hui-sensor-card.js * Update hui-sensor-card.js --- src/panels/lovelace/cards/hui-sensor-card.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/panels/lovelace/cards/hui-sensor-card.js b/src/panels/lovelace/cards/hui-sensor-card.js index b911390402..4f811d5e6b 100644 --- a/src/panels/lovelace/cards/hui-sensor-card.js +++ b/src/panels/lovelace/cards/hui-sensor-card.js @@ -180,13 +180,16 @@ class HuiSensorCard extends EventsMixin(LitElement) { const history = stateHistory[0]; const valArray = [history[history.length - 1]]; - let pos = history.length - 1; - const accuracy = this._config.accuracy <= pos ? this._config.accuracy : pos; + const accuracy = + this._config.accuracy <= history.length + ? this._config.accuracy + : history.length; let increment = Math.ceil(history.length / accuracy); increment = increment <= 0 ? 1 : increment; - for (let i = accuracy; i >= 2; i--) { + let pos = history.length - 1; + for (let i = accuracy; i >= 1; i--) { pos -= increment; - valArray.unshift(pos >= 0 ? history[pos] : history[0]); + if (pos >= 0) valArray.unshift(history[pos]); } this._line = this._getGraph(valArray, 500, this._config.height); }