Skip to content

A local-first personal finance app

[AI] feat(budget analysis report): Add Balance & Category selector (#8162)

tabedzki37ac0adea0a6
diff --git a/packages/desktop-client/src/components/reports/graphs/BudgetAnalysisGraph.test.tsx b/packages/desktop-client/src/components/reports/graphs/BudgetAnalysisGraph.test.tsx
new file mode 100644
index 0000000000..84c42436d6
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/graphs/BudgetAnalysisGraph.test.tsx
@@ -0,0 +1,97 @@
+import React from 'react';
+
+import { render } from '@testing-library/react';
+import { describe, expect, it } from 'vitest';
+
+import { TestProviders } from '#mocks';
+
+import { BudgetAnalysisGraph } from './BudgetAnalysisGraph';
+
+const sampleData = {
+ intervalData: [
+ {
+ date: '2024-01',
+ budgeted: 50000,
+ spent: -30000,
+ balance: 20000,
+ overspendingAdjustment: 0,
+ },
+ {
+ date: '2024-02',
+ budgeted: 50000,
+ spent: -40000,
+ balance: 30000,
+ overspendingAdjustment: 0,
+ },
+ ],
+};
+
+describe('BudgetAnalysisGraph – balanceOnly', () => {
+ it('renders the balance series label when balanceOnly is true', () => {
+ render(
+ <TestProviders>
+ <BudgetAnalysisGraph
+ data={sampleData}
+ graphType="Line"
+ balanceOnly
+ showBalance={false}
+ />
+ </TestProviders>,
+ );
+ // The LineChart should render without throwing; the balance dataKey should be present
+ // We verify by checking the container renders successfully
+ expect(document.querySelector('svg')).toBeInTheDocument();
+ });
+
+ it('renders all series when balanceOnly is false', () => {
+ render(
+ <TestProviders>
+ <BudgetAnalysisGraph
+ data={sampleData}
+ graphType="Line"
+ balanceOnly={false}
+ showBalance
+ />
+ </TestProviders>,
+ );
+ expect(document.querySelector('svg')).toBeInTheDocument();
+ });
+
+ it('renders a bar chart when graphType is Bar and balanceOnly is false', () => {
+ render(
+ <TestProviders>
+ <BudgetAnalysisGraph
+ data={sampleData}
+ graphType="Bar"
+ balanceOnly={false}
+ showBalance
+ />
+ </TestProviders>,
+ );
+ expect(document.querySelector('svg')).toBeInTheDocument();
+ });
+
+ it('renders a line chart (balance-only) even when graphType is Bar', () => {
+ // When balanceOnly=true the component always renders a LineChart
+ render(
+ <TestProviders>
+ <BudgetAnalysisGraph data={sampleData} graphType="Bar" balanceOnly />
+ </TestProviders>,
+ );
+ expect(document.querySelector('svg')).toBeInTheDocument();
+ });
+
+ it('hides balance line when showBalance is false and balanceOnly is false', () => {
+ render(
+ <TestProviders>
+ <BudgetAnalysisGraph
+ data={sampleData}
+ graphType="Line"
+ balanceOnly={false}
+ showBalance={false}
+ />
+ </TestProviders>,
+ );
+ expect(document.querySelector('svg')).toBeInTheDocument();
+ });
+});
diff --git a/packages/desktop-client/src/components/reports/graphs/BudgetAnalysisGraph.tsx b/packages/desktop-client/src/components/reports/graphs/BudgetAnalysisGraph.tsx
index 16cbfe11c2..81b954fb61 100644
--- a/packages/desktop-client/src/components/reports/graphs/BudgetAnalysisGraph.tsx
+++ b/packages/desktop-client/src/components/reports/graphs/BudgetAnalysisGraph.tsx
@@ -56,6 +56,7 @@ type BudgetAnalysisGraphProps = {
};
graphType?: 'Line' | 'Bar';
showBalance?: boolean;
+ balanceOnly?: boolean;
isConcise?: boolean;
};
@@ -65,6 +66,7 @@ type CustomTooltipProps = {
isConcise: boolean;
format: (value: unknown, type?: FormatType) => string;
showBalance: boolean;
+ balanceOnly: boolean;
};
function CustomTooltip({
@@ -73,6 +75,7 @@ function CustomTooltip({
isConcise,
format,
showBalance,
+ balanceOnly,
}: CustomTooltipProps) {
const locale = useLocale();
const { t } = useTranslation();
@@ -105,65 +108,71 @@ function CustomTooltip({
</strong>
</div>
<div style={{ lineHeight: 1.5 }}>
- <AlignedText
- left={
- <span style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
- <span
- style={{
- width: 10,
- height: 10,
- backgroundColor: theme.reportsNumberPositive,
- display: 'inline-block',
- }}
- />
- <Trans>Budgeted:</Trans>
- </span>
- }
- right={
- <FinancialText>
- {format(data.budgeted, 'financial')}
- </FinancialText>
- }
- />
- <AlignedText
- left={
- <span style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
- <span
- style={{
- width: 10,
- height: 10,
- backgroundColor: theme.reportsNumberNegative,
- display: 'inline-block',
- }}
- />
- <Trans>Spent:</Trans>
- </span>
- }
- right={
- <FinancialText>{format(data.spent, 'financial')}</FinancialText>
- }
- />
- <AlignedText
- left={
- <span style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
- <span
- style={{
- width: 10,
- height: 10,
- backgroundColor: theme.templateNumberUnderFunded,
- display: 'inline-block',
- }}
- />
- {t('Overspending Adjustment:')}
- </span>
- }
- right={
- <FinancialText>
- {format(data.overspendingAdjustment, 'financial')}
- </FinancialText>
- }
- />
- {showBalance && (
+ {!balanceOnly && (
+ <AlignedText
+ left={
+ <span style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
+ <span
+ style={{
+ width: 10,
+ height: 10,
+ backgroundColor: theme.reportsNumberPositive,
+ display: 'inline-block',
+ }}
+ />
+ <Trans>Budgeted:</Trans>
+ </span>
+ }
+ right={
+ <FinancialText>
+ {format(data.budgeted, 'financial')}
+ </FinancialText>
+ }
+ />
+ )}
+ {!balanceOnly && (
+ <AlignedText
+ left={
+ <span style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
+ <span
+ style={{
+ width: 10,
+ height: 10,
+ backgroundColor: theme.reportsNumberNegative,
+ display: 'inline-block',
+ }}
+ />
+ <Trans>Spent:</Trans>
+ </span>
+ }
+ right={
+ <FinancialText>{format(data.spent, 'financial')}</FinancialText>
+ }
+ />
+ )}
+ {!balanceOnly && (
+ <AlignedText
+ left={
+ <span style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
+ <span
+ style={{
+ width: 10,
+ height: 10,
+ backgroundColor: theme.templateNumberUnderFunded,
+ display: 'inline-block',
+ }}
+ />
+ {t('Overspending Adjustment:')}
+ </span>
+ }
+ right={
+ <FinancialText>
+ {format(data.overspendingAdjustment, 'financial')}
+ </FinancialText>
+ }
+ />
+ )}
+ {(showBalance || balanceOnly) && (
<AlignedText
left={
<span style={{ display: 'flex', alignItems: 'center', gap: 5 }}>
@@ -196,6 +205,7 @@ export function BudgetAnalysisGraph({
data,
graphType = 'Line',
showBalance = true,
+ balanceOnly = false,
isConcise = true,
}: BudgetAnalysisGraphProps) {
const { t } = useTranslation();
@@ -244,8 +254,8 @@ export function BudgetAnalysisGraph({
{(width, height) => {
const chartProps = { ...commonProps, width, height };
- return graphType === 'Bar' ? (
- <ComposedChart {...chartProps}>
+ const sharedAxes = (
+ <>
<CartesianGrid strokeDasharray="3 3" stroke={theme.pillBorder} />
<XAxis
dataKey="date"
@@ -272,10 +282,17 @@ export function BudgetAnalysisGraph({
isConcise={isConcise}
format={format}
showBalance={showBalance}
+ balanceOnly={balanceOnly}
/>
}
isAnimationActive={false}
/>
+ </>
+ );
+
+ return graphType === 'Bar' && !balanceOnly ? (
+ <ComposedChart {...chartProps}>
+ {sharedAxes}
<Bar
dataKey="budgeted"
fill={theme.reportsNumberPositive}
@@ -308,64 +325,41 @@ export function BudgetAnalysisGraph({
</ComposedChart>
) : (
<LineChart {...chartProps}>
- <CartesianGrid strokeDasharray="3 3" stroke={theme.pillBorder} />
- <XAxis
- dataKey="date"
- tick={{ fill: theme.reportsLabel }}
- tickFormatter={formatDate}
- minTickGap={50}
- />
- <YAxis
- tick={{ fill: theme.reportsLabel }}
- tickCount={8}
- tickFormatter={value =>
- privacyMode && !yAxisIsHovered
- ? '...'
- : format(value, 'financial-no-decimals')
- }
- onMouseEnter={() => setYAxisIsHovered(true)}
- onMouseLeave={() => setYAxisIsHovered(false)}
- stroke={theme.pageTextSubdued}
- />
- <Tooltip
- cursor={{ fill: 'transparent' }}
- content={
- <CustomTooltip
- isConcise={isConcise}
- format={format}
- showBalance={showBalance}
- />
- }
- isAnimationActive={false}
- />
- <Line
- type="monotone"
- dataKey="budgeted"
- stroke={theme.reportsNumberPositive}
- strokeWidth={2}
- name={budgetedLabel}
- dot={false}
- animationDuration={1000}
- />
- <Line
- type="monotone"
- dataKey="spent"
- stroke={theme.reportsNumberNegative}
- strokeWidth={2}
- name={spentLabel}
- dot={false}
- animationDuration={1000}
- />
- <Line
- type="monotone"
- dataKey="overspendingAdjustment"
- stroke={theme.templateNumberUnderFunded}
- strokeWidth={2}
- name={overspendingLabel}
- dot={false}
- animationDuration={1000}
- />
- {showBalance && (
+ {sharedAxes}
+ {!balanceOnly && (
+ <Line
+ type="monotone"
+ dataKey="budgeted"
+ stroke={theme.reportsNumberPositive}
+ strokeWidth={2}
+ name={budgetedLabel}
+ dot={false}
+ animationDuration={1000}
+ />
+ )}
+ {!balanceOnly && (
+ <Line
+ type="monotone"
+ dataKey="spent"
+ stroke={theme.reportsNumberNegative}
+ strokeWidth={2}
+ name={spentLabel}
+ dot={false}
+ animationDuration={1000}
+ />
+ )}
+ {!balanceOnly && (
+ <Line
+ type="monotone"
+ dataKey="overspendingAdjustment"
+ stroke={theme.templateNumberUnderFunded}
+ strokeWidth={2}
+ name={overspendingLabel}
+ dot={false}
+ animationDuration={1000}
+ />
+ )}
+ {(showBalance || balanceOnly) && (
<Line
type="monotone"
dataKey="balance"
diff --git a/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx b/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
index c36f68afa0..f7a7c6b572 100644
--- a/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
+++ b/packages/desktop-client/src/components/reports/reports/BudgetAnalysis.tsx
@@ -8,6 +8,7 @@ import { Button } from '@actual-app/components/button';
import { useResponsive } from '@actual-app/components/hooks/useResponsive';
import { SvgChart, SvgChartBar } from '@actual-app/components/icons/v1';
import { Paragraph } from '@actual-app/components/paragraph';
+import { Select } from '@actual-app/components/select';
import { theme } from '@actual-app/components/theme';
import { Tooltip } from '@actual-app/components/tooltip';
import { View } from '@actual-app/components/view';
@@ -43,6 +44,11 @@ import { addNotification } from '#notifications/notificationsSlice';
import { useDispatch } from '#redux';
import { useUpdateDashboardWidgetMutation } from '#reports/mutations';
+type BalanceMode =
+ | 'balance-only'
+ | 'balance-and-categories'
+ | 'categories-only';
+
export function BudgetAnalysis() {
const params = useParams();
const { data: widget, isPending } = useDashboardWidget<BudgetAnalysisWidget>({
@@ -93,6 +99,9 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
const [showBalance, setShowBalance] = useState(
widget?.meta?.showBalance ?? true,
);
+ const [balanceOnly, setBalanceOnly] = useState(
+ widget?.meta?.balanceOnly ?? false,
+ );
const [latestTransaction, setLatestTransaction] = useState('');
const [isConcise, setIsConcise] = useState(() => {
// Default to concise (monthly) view until we load the actual date range
@@ -225,6 +234,7 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
},
graphType,
showBalance,
+ balanceOnly,
},
},
},
@@ -269,6 +279,17 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
});
};
+ const balanceMode: BalanceMode = balanceOnly
+ ? 'balance-only'
+ : showBalance
+ ? 'balance-and-categories'
+ : 'categories-only';
+
+ function onBalanceModeChange(newMode: BalanceMode) {
+ setBalanceOnly(newMode === 'balance-only');
+ setShowBalance(newMode !== 'categories-only');
+ }
+
return (
<Page
header={
@@ -337,9 +358,15 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
}
>
<View style={{ flexDirection: 'row', gap: 10 }}>
- <Button onPress={() => setShowBalance(state => !state)}>
- {showBalance ? t('Hide balance') : t('Show balance')}
- </Button>
+ <Select<BalanceMode>
+ value={balanceMode}
+ onChange={onBalanceModeChange}
+ options={[
+ ['balance-only', t('Balance only')],
+ ['balance-and-categories', t('Balance + Categories')],
+ ['categories-only', t('Categories only')],
+ ]}
+ />
{widget && (
<Button variant="primary" onPress={onSaveWidget}>
@@ -442,23 +469,21 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
</FinancialText>
}
/>
- {showBalance && (
- <AlignedText
- style={{ marginBottom: 5, minWidth: 210 }}
- left={
- <Block>
- <Trans>Ending balance:</Trans>
- </Block>
- }
- right={
- <FinancialText style={{ fontWeight: 600 }}>
- <PrivacyFilter>
- <Change amount={endingBalance} />
- </PrivacyFilter>
- </FinancialText>
- }
- />
- )}
+ <AlignedText
+ style={{ marginBottom: 5, minWidth: 210 }}
+ left={
+ <Block>
+ <Trans>Ending balance:</Trans>
+ </Block>
+ }
+ right={
+ <FinancialText style={{ fontWeight: 600 }}>
+ <PrivacyFilter>
+ <Change amount={endingBalance} />
+ </PrivacyFilter>
+ </FinancialText>
+ }
+ />
</>
)}
</View>
@@ -469,6 +494,7 @@ function BudgetAnalysisInternal({ widget }: BudgetAnalysisInternalProps) {
data={data}
graphType={graphType}
showBalance={showBalance}
+ balanceOnly={balanceOnly}
isConcise={isConcise}
/>
<View style={{ marginTop: 30 }}>
diff --git a/packages/desktop-client/src/components/reports/reports/BudgetAnalysisCard.tsx b/packages/desktop-client/src/components/reports/reports/BudgetAnalysisCard.tsx
index 6fd593e391..e2768a5e16 100644
--- a/packages/desktop-client/src/components/reports/reports/BudgetAnalysisCard.tsx
+++ b/packages/desktop-client/src/components/reports/reports/BudgetAnalysisCard.tsx
@@ -154,6 +154,7 @@ export function BudgetAnalysisCard({
data={data}
graphType={meta?.graphType || 'Bar'}
showBalance={meta?.showBalance ?? true}
+ balanceOnly={meta?.balanceOnly ?? false}
/>
) : (
<LoadingIndicator />
diff --git a/packages/loot-core/src/types/models/dashboard.ts b/packages/loot-core/src/types/models/dashboard.ts
index eae96ffed0..3fee0ed68f 100644
--- a/packages/loot-core/src/types/models/dashboard.ts
+++ b/packages/loot-core/src/types/models/dashboard.ts
@@ -94,6 +94,7 @@ export type BudgetAnalysisWidget = AbstractWidget<
interval?: 'Daily' | 'Weekly' | 'Monthly' | 'Yearly';
graphType?: 'Line' | 'Bar';
showBalance?: boolean;
+ balanceOnly?: boolean;
} | null
>;
export type CustomReportWidget = AbstractWidget<
diff --git a/upcoming-release-notes/ai-feat-budget-analysis-report-add-balance-category-selector.md b/upcoming-release-notes/ai-feat-budget-analysis-report-add-balance-category-selector.md
new file mode 100644
index 0000000000..e8f60be335
--- /dev/null
+++ b/upcoming-release-notes/ai-feat-budget-analysis-report-add-balance-category-selector.md
@@ -0,0 +1,6 @@
+---
+category: Enhancements
+authors: [tabedzki]
+---
+
+[AI] feat(budget analysis report): Add Balance & Category selector