From 3e03718f13134c33ec90ab74587ed7a52c4ffcbc Mon Sep 17 00:00:00 2001
From: Neil <55785687+carkom@users.noreply.github.com>
Date: Mon, 13 May 2024 16:00:51 +0100
Subject: [PATCH] Fix spending report error (#2745)

* spending error

* fixes

* notes
---
 .../reports/graphs/SpendingGraph.tsx           | 18 ++++++++++--------
 upcoming-release-notes/2745.md                 |  6 ++++++
 2 files changed, 16 insertions(+), 8 deletions(-)
 create mode 100644 upcoming-release-notes/2745.md

diff --git a/packages/desktop-client/src/components/reports/graphs/SpendingGraph.tsx b/packages/desktop-client/src/components/reports/graphs/SpendingGraph.tsx
index e74f64630..616315fd2 100644
--- a/packages/desktop-client/src/components/reports/graphs/SpendingGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/SpendingGraph.tsx
@@ -145,8 +145,12 @@ export function SpendingGraph({
             : b,
         ).months[lastMonth][balanceTypeOp];
   const maxYAxis = selectionMax > thisMonthMax;
-  const dataMax = Math.max(...data.intervalData.map(i => i[balanceTypeOp]));
-  const dataMin = Math.min(...data.intervalData.map(i => i[balanceTypeOp]));
+  const dataMax = Math.max(
+    ...data.intervalData.map(i => i.months[thisMonth].cumulative),
+  );
+  const dataMin = Math.min(
+    ...data.intervalData.map(i => i.months[thisMonth].cumulative),
+  );
 
   const tickFormatter = tick => {
     if (!privacyMode) return `${amountToCurrencyNoDecimal(tick)}`; // Formats the tick values as strings with commas
@@ -154,18 +158,16 @@ export function SpendingGraph({
   };
 
   const gradientOffset = () => {
-    if (dataMax <= 0) {
+    if (!dataMax || dataMax <= 0) {
       return 0;
     }
-    if (dataMin >= 0) {
+    if (!dataMin || dataMin >= 0) {
       return 1;
     }
 
     return dataMax / (dataMax - dataMin);
   };
 
-  const off = gradientOffset();
-
   const getVal = (obj, month) => {
     if (month === 'average') {
       return obj[month] && -1 * obj[month];
@@ -246,7 +248,7 @@ export function SpendingGraph({
                     y2="1"
                   >
                     <stop
-                      offset={off}
+                      offset={gradientOffset()}
                       stopColor={theme.reportsGreen}
                       stopOpacity={0.2}
                     />
@@ -259,7 +261,7 @@ export function SpendingGraph({
                     y2="1"
                   >
                     <stop
-                      offset={off}
+                      offset={gradientOffset()}
                       stopColor={theme.reportsGreen}
                       stopOpacity={1}
                     />
diff --git a/upcoming-release-notes/2745.md b/upcoming-release-notes/2745.md
new file mode 100644
index 000000000..7898425d7
--- /dev/null
+++ b/upcoming-release-notes/2745.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [carkom]
+---
+
+Fixes a NaN error in spending report.
-- 
GitLab