Skip to content

A local-first personal finance app

[AI] Color balance forecast line by zero crossing (#7850)

Sebastián Maluk90a1e9bdd35a
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-1-chromium-linux.png
index 79d7060777..3b57fafb0a 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-2-chromium-linux.png
index d5a91d8038..e5d193083f 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-3-chromium-linux.png
index b9fce82bf1..c56d2cc2bd 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-loads-balance-forecast-report-with-monthly-granularity-3-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-1-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-1-chromium-linux.png
index e988a889a3..118b5fd110 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-1-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-1-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-2-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-2-chromium-linux.png
index def18bc90f..46be38e635 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-2-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-2-chromium-linux.png differ
diff --git a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-3-chromium-linux.png b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-3-chromium-linux.png
index 0fda15b824..4928eefbf8 100644
Binary files a/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-3-chromium-linux.png and b/packages/desktop-client/e2e/reports.test.ts-snapshots/Reports-balance-forecast-switches-to-daily-granularity-3-chromium-linux.png differ
diff --git a/packages/desktop-client/src/components/reports/reports/BalanceForecast.tsx b/packages/desktop-client/src/components/reports/reports/BalanceForecast.tsx
index 6e330ebae7..78c8cec302 100644
--- a/packages/desktop-client/src/components/reports/reports/BalanceForecast.tsx
+++ b/packages/desktop-client/src/components/reports/reports/BalanceForecast.tsx
@@ -46,6 +46,7 @@ import { useDispatch } from '#redux';
import {
buildBalanceForecastChartData,
countForecastScheduledOccurrences,
+ getZeroCrossingGradientOffset,
} from './balanceForecastChartData';
export function BalanceForecast() {
@@ -280,6 +281,7 @@ function BalanceForecastInner({ widget }: BalanceForecastInnerProps) {
const lowestPoint = forecastData?.lowestBalance;
const hasNegativeBalance = chartData.some(d => d.balance < 0);
+ const zeroCrossingGradientOffset = getZeroCrossingGradientOffset(chartData);
const todayReferenceDate =
granularity === 'Daily'
? monthUtils.currentDay()
@@ -384,6 +386,37 @@ function BalanceForecastInner({ widget }: BalanceForecastInnerProps) {
data={chartData}
margin={{ top: 10, right: 10, left: 5, bottom: 10 }}
>
+ <defs>
+ <linearGradient
+ id="balance-forecast-line-gradient"
+ x1="0"
+ y1="0"
+ x2="0"
+ y2="1"
+ >
+ {zeroCrossingGradientOffset == null ? (
+ <stop
+ offset="0%"
+ stopColor={
+ hasNegativeBalance
+ ? theme.errorText
+ : theme.noticeText
+ }
+ />
+ ) : (
+ <>
+ <stop
+ offset={`${zeroCrossingGradientOffset}%`}
+ stopColor={theme.noticeText}
+ />
+ <stop
+ offset={`${zeroCrossingGradientOffset}%`}
+ stopColor={theme.errorText}
+ />
+ </>
+ )}
+ </linearGradient>
+ </defs>
<CartesianGrid strokeDasharray="3 3" />
<XAxis
dataKey="date"
@@ -461,14 +494,13 @@ function BalanceForecastInner({ widget }: BalanceForecastInnerProps) {
}}
/>
)}
+ {hasNegativeBalance && (
+ <ReferenceLine y={0} stroke={theme.pageTextSubdued} />
+ )}
<Line
type="monotone"
dataKey="balance"
- stroke={
- hasNegativeBalance
- ? theme.errorText
- : theme.noticeText
- }
+ stroke="url(#balance-forecast-line-gradient)"
strokeWidth={2}
dot={false}
activeDot={{ r: 6 }}
diff --git a/packages/desktop-client/src/components/reports/reports/BalanceForecastCard.tsx b/packages/desktop-client/src/components/reports/reports/BalanceForecastCard.tsx
index 3bfd573101..a3d0d25d00 100644
--- a/packages/desktop-client/src/components/reports/reports/BalanceForecastCard.tsx
+++ b/packages/desktop-client/src/components/reports/reports/BalanceForecastCard.tsx
@@ -33,6 +33,7 @@ import { useFormat } from '#hooks/useFormat';
import {
buildBalanceForecastChartData,
countForecastScheduledOccurrences,
+ getZeroCrossingGradientOffset,
} from './balanceForecastChartData';
type BalanceForecastCardProps = {
@@ -123,6 +124,9 @@ export function BalanceForecastCard({
end: chartRange.end,
granularity: 'Monthly',
});
+ const hasNegativeBalance = chartData.some(d => d.balance < 0);
+ const zeroCrossingGradientOffset = getZeroCrossingGradientOffset(chartData);
+ const gradientId = `balance-forecast-card-line-gradient-${widgetId}`;
const isUpdatingForecast = isFetching && isPlaceholderData;
const todayReferenceDate = monthUtils.currentMonth();
const showsTodayReferenceLine = chartData.some(
@@ -234,6 +238,37 @@ export function BalanceForecastCard({
data={chartData}
margin={{ top: 5, right: 5, left: 5, bottom: 5 }}
>
+ <defs>
+ <linearGradient
+ id={gradientId}
+ x1="0"
+ y1="0"
+ x2="0"
+ y2="1"
+ >
+ {zeroCrossingGradientOffset == null ? (
+ <stop
+ offset="0%"
+ stopColor={
+ hasNegativeBalance
+ ? theme.errorText
+ : theme.noticeText
+ }
+ />
+ ) : (
+ <>
+ <stop
+ offset={`${zeroCrossingGradientOffset}%`}
+ stopColor={theme.noticeText}
+ />
+ <stop
+ offset={`${zeroCrossingGradientOffset}%`}
+ stopColor={theme.errorText}
+ />
+ </>
+ )}
+ </linearGradient>
+ </defs>
<Tooltip
isAnimationActive={false}
content={({ active, payload }) => {
@@ -272,10 +307,13 @@ export function BalanceForecastCard({
strokeDasharray="4 4"
/>
)}
+ {hasNegativeBalance && (
+ <ReferenceLine y={0} stroke={theme.pageTextSubdued} />
+ )}
<Line
type="monotone"
dataKey="balance"
- stroke={hasNegative ? theme.errorText : theme.noticeText}
+ stroke={`url(#${gradientId})`}
strokeWidth={2}
dot={false}
activeDot={{ r: 4 }}
diff --git a/packages/desktop-client/src/components/reports/reports/balanceForecastChartData.test.ts b/packages/desktop-client/src/components/reports/reports/balanceForecastChartData.test.ts
index 89a6f91ad8..1ab528f337 100644
--- a/packages/desktop-client/src/components/reports/reports/balanceForecastChartData.test.ts
+++ b/packages/desktop-client/src/components/reports/reports/balanceForecastChartData.test.ts
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest';
import {
buildBalanceForecastChartData,
countForecastScheduledOccurrences,
+ getZeroCrossingGradientOffset,
} from './balanceForecastChartData';
describe('buildBalanceForecastChartData', () => {
@@ -257,3 +258,23 @@ describe('countForecastScheduledOccurrences', () => {
expect(count).toBe(2);
});
});
+
+describe('getZeroCrossingGradientOffset', () => {
+ it('returns the zero threshold offset when balances cross zero', () => {
+ expect(
+ getZeroCrossingGradientOffset([
+ { date: '2024-03', balance: 100 },
+ { date: '2024-04', balance: -100 },
+ ]),
+ ).toBe(50);
+ });
+
+ it('returns null when balances do not cross zero', () => {
+ expect(
+ getZeroCrossingGradientOffset([
+ { date: '2024-03', balance: 100 },
+ { date: '2024-04', balance: 50 },
+ ]),
+ ).toBeNull();
+ });
+});
diff --git a/packages/desktop-client/src/components/reports/reports/balanceForecastChartData.ts b/packages/desktop-client/src/components/reports/reports/balanceForecastChartData.ts
index b11d162f75..532e853868 100644
--- a/packages/desktop-client/src/components/reports/reports/balanceForecastChartData.ts
+++ b/packages/desktop-client/src/components/reports/reports/balanceForecastChartData.ts
@@ -128,3 +128,19 @@ export function countForecastScheduledOccurrences(
return occurrenceKeys.size;
}
+
+export function getZeroCrossingGradientOffset(chartData: ChartDataPoint[]) {
+ if (chartData.length === 0) {
+ return null;
+ }
+
+ const balances = chartData.map(point => point.balance);
+ const minBalance = Math.min(...balances);
+ const maxBalance = Math.max(...balances);
+
+ if (minBalance >= 0 || maxBalance <= 0) {
+ return null;
+ }
+
+ return (maxBalance / (maxBalance - minBalance)) * 100;
+}
diff --git a/upcoming-release-notes/7850.md b/upcoming-release-notes/7850.md
new file mode 100644
index 0000000000..941afed20f
--- /dev/null
+++ b/upcoming-release-notes/7850.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [samaluk]
+---
+
+Color the Balance Forecast line by zero-balance crossing.