From 6a65962d3329d87fceb7b7e6896b9172ac15403c Mon Sep 17 00:00:00 2001 From: Onne Gorter Date: Sun, 30 Jun 2024 16:52:16 +0200 Subject: [PATCH 1/3] Fixes last point of graphs. Especially for items that don't often change state, the last state could be essentially be ignored for quite a while, using the average of the last series of entries instead. Also for detailed graphs, the initial values could be off, because the initial last value would be computed on bad data, resulting in NaN. And no longer adds a double entry at end if graph has only one point. --- .../lovelace/common/graph/coordinates.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/panels/lovelace/common/graph/coordinates.ts b/src/panels/lovelace/common/graph/coordinates.ts index 56f525196502..d075835410cf 100644 --- a/src/panels/lovelace/common/graph/coordinates.ts +++ b/src/panels/lovelace/common/graph/coordinates.ts @@ -22,9 +22,16 @@ const calcPoints = ( let xRatio = width / (hours - (detail === 1 ? 1 : 0)); xRatio = isFinite(xRatio) ? xRatio : width; - const first = history.filter(Boolean)[0]; + let first = history.filter(Boolean)[0]; + if (detail > 1) { + first = first.filter(Boolean)[0]; + } let last = [average(first), lastValue(first)]; + const getY = (value: number): number => { + return height + strokeWidth / 2 - (value - min) / yRatio; + }; + const getCoords = (item: any[], i: number, offset = 0, depth = 1) => { if (depth > 1 && item) { return item.forEach((subItem, index) => @@ -37,8 +44,7 @@ const calcPoints = ( if (item) { last = [average(item), lastValue(item)]; } - const y = - height + strokeWidth / 2 - ((item ? last[0] : last[1]) - min) / yRatio; + const y = getY(item ? last[0] : last[1]); return coords.push([x, y]); }; @@ -46,11 +52,7 @@ const calcPoints = ( getCoords(history[i], i, 0, detail); } - if (coords.length === 1) { - coords[1] = [width, coords[0][1]]; - } - - coords.push([width, coords[coords.length - 1][1]]); + coords.push([width, getY(last[1])]); return coords; }; From ce6983152e63c8e5d6d0649742e96a5e4434fb77 Mon Sep 17 00:00:00 2001 From: Onne Gorter Date: Fri, 19 Jul 2024 23:17:11 +0200 Subject: [PATCH 2/3] update according to linter --- src/panels/lovelace/common/graph/coordinates.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/panels/lovelace/common/graph/coordinates.ts b/src/panels/lovelace/common/graph/coordinates.ts index d075835410cf..240e805a6ed0 100644 --- a/src/panels/lovelace/common/graph/coordinates.ts +++ b/src/panels/lovelace/common/graph/coordinates.ts @@ -28,9 +28,7 @@ const calcPoints = ( } let last = [average(first), lastValue(first)]; - const getY = (value: number): number => { - return height + strokeWidth / 2 - (value - min) / yRatio; - }; + const getY = (value: number): number => height + strokeWidth / 2 - (value - min) / yRatio; const getCoords = (item: any[], i: number, offset = 0, depth = 1) => { if (depth > 1 && item) { From cae90e76711e627b3ecb9ef4e5311c6f0716e28f Mon Sep 17 00:00:00 2001 From: Simon Lamon <32477463+silamon@users.noreply.github.com> Date: Sat, 20 Jul 2024 10:09:12 +0200 Subject: [PATCH 3/3] Prettier --- src/panels/lovelace/common/graph/coordinates.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/panels/lovelace/common/graph/coordinates.ts b/src/panels/lovelace/common/graph/coordinates.ts index 240e805a6ed0..02226d2bab1f 100644 --- a/src/panels/lovelace/common/graph/coordinates.ts +++ b/src/panels/lovelace/common/graph/coordinates.ts @@ -28,7 +28,8 @@ const calcPoints = ( } let last = [average(first), lastValue(first)]; - const getY = (value: number): number => height + strokeWidth / 2 - (value - min) / yRatio; + const getY = (value: number): number => + height + strokeWidth / 2 - (value - min) / yRatio; const getCoords = (item: any[], i: number, offset = 0, depth = 1) => { if (depth > 1 && item) {