Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Year comparison feature. #2806

Merged
merged 30 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e6e8818
Added Year comparsion feature. Also fixed bug with greater than 28 days.
Crazypkr1099 May 29, 2024
0a955e6
Removed comments and console.logs
Crazypkr1099 May 29, 2024
3b7f6fe
Create 2806.md
Crazypkr1099 May 29, 2024
c3d5202
Cleaned up code
Crazypkr1099 May 29, 2024
1d96a6e
Merge branch 'spendingGraphYear' of https://github.com/Crazypkr1099/a…
Crazypkr1099 May 29, 2024
0455cf9
Merge remote-tracking branch 'origin/master' into spendingGraphYear
Crazypkr1099 May 30, 2024
4b9fbb0
Hide's graph if no data, and hides average, last month or last year i…
Crazypkr1099 Jun 4, 2024
8d0d64a
Apply suggestions from code review
Crazypkr1099 Jun 4, 2024
2b38a0d
Fixed spent MTD and last MTD. Added in all suggestions from carkom.
Crazypkr1099 Jun 4, 2024
8ebda59
Update 2806.md
Crazypkr1099 Jun 4, 2024
1d3855d
Added changes required by carkom #2
Crazypkr1099 Jun 6, 2024
2aea8bf
Merge branch 'spendingGraphYear' of https://github.com/Crazypkr1099/a…
Crazypkr1099 Jun 6, 2024
77be690
Couple more fixes, only show graph if have data for last month (as re…
Crazypkr1099 Jun 6, 2024
944dcb8
Removed console.log that was mistakenly added.
Crazypkr1099 Jun 6, 2024
9450758
removed useEffect
Crazypkr1099 Jun 7, 2024
3a49f36
Add files via upload
Crazypkr1099 Jun 7, 2024
186178e
Merge branch 'master' into spendingGraphYear
Crazypkr1099 Jun 7, 2024
f79b116
Remove async function
carkom Jun 7, 2024
5f4aadd
lint fix
carkom Jun 7, 2024
8f842bb
fixed carkom requests & added in fix for YAxis issues
Crazypkr1099 Jun 8, 2024
00ce7e1
Fixed couple of mistakes. Removed Y Axis fix (new PR will be created)
Crazypkr1099 Jun 9, 2024
72e299f
Merge branch 'master' into spendingGraphYear
Crazypkr1099 Jun 9, 2024
7ef3230
Cleanup code
Crazypkr1099 Jun 9, 2024
31fb2ec
Merge branch 'spendingGraphYear' of https://github.com/Crazypkr1099/a…
Crazypkr1099 Jun 9, 2024
13224fa
Fix mode buttons
Crazypkr1099 Jun 9, 2024
d490a10
Removed console.log...
Crazypkr1099 Jun 9, 2024
197bf21
Update showAverage Logic
carkom Jun 10, 2024
be55a08
Update switch logic
carkom Jun 10, 2024
3d4da58
Add Math.abs
carkom Jun 10, 2024
7c366e6
lint fix
carkom Jun 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type CustomTooltipProps = {
payload?: PayloadItem[];
balanceTypeOp?: string;
thisMonth?: string;
lastYear?: string;
selection?: string;
};

Expand All @@ -53,6 +54,7 @@ const CustomTooltip = ({
payload,
balanceTypeOp,
thisMonth,
lastYear,
selection,
}: CustomTooltipProps) => {
if (active && payload && payload.length) {
Expand Down Expand Up @@ -92,7 +94,13 @@ const CustomTooltip = ({
)}
{['cumulative'].includes(balanceTypeOp) && (
<AlignedText
left={selection === 'average' ? 'Average' : 'Last month:'}
left={
selection === 'average'
? 'Average'
: selection === lastYear
? 'Last year'
: 'Last month'
}
right={amountToCurrency(comparison)}
/>
)}
Expand Down Expand Up @@ -129,7 +137,15 @@ export function SpendingGraph({
const balanceTypeOp = 'cumulative';
const thisMonth = monthUtils.currentMonth();
const lastMonth = monthUtils.subMonths(monthUtils.currentMonth(), 1);
const selection = mode.toLowerCase() === 'average' ? 'average' : lastMonth;
const lastYear = monthUtils.prevYear(monthUtils.currentMonth());
let selection;
if (mode.toLowerCase() === 'average') {
selection = 'average';
} else if (mode.toLowerCase() === 'last month') {
selection = lastMonth;
} else {
selection = lastYear;
}
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
const thisMonthMax = data.intervalData.reduce((a, b) =>
a.months[thisMonth][balanceTypeOp] < b.months[thisMonth][balanceTypeOp]
? a
Expand All @@ -139,11 +155,15 @@ export function SpendingGraph({
selection === 'average'
? data.intervalData[27].average
: data.intervalData.reduce((a, b) =>
a.months[lastMonth][balanceTypeOp] <
b.months[lastMonth][balanceTypeOp]
a.months[selection === lastMonth ? lastMonth : lastYear][
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
balanceTypeOp
] <
b.months[selection === lastMonth ? lastMonth : lastYear][
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
balanceTypeOp
]
? a
: b,
).months[lastMonth][balanceTypeOp];
).months[selection === lastMonth ? lastMonth : lastYear][balanceTypeOp];
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
const maxYAxis = selectionMax > thisMonthMax;
const dataMax = Math.max(
...data.intervalData.map(i => i.months[thisMonth].cumulative),
Expand Down Expand Up @@ -233,6 +253,7 @@ export function SpendingGraph({
<CustomTooltip
balanceTypeOp={balanceTypeOp}
thisMonth={thisMonth}
lastYear={lastYear}
selection={selection}
/>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ export function Spending() {
conditions: filters,
conditionsOp,
setDataCheck,
mode,
});
}, [categories, filters, conditionsOp]);
}, [categories, filters, conditionsOp, mode]);

const data = useReport('default', getGraphData);
const navigate = useNavigate();
const { isNarrowWidth } = useResponsive();

if (!data) {
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
Expand Down Expand Up @@ -175,8 +175,8 @@ export function Spending() {
{amountToCurrency(
Math.abs(
data.intervalData[
monthUtils.getDay(monthUtils.currentDay()) >= 29
? 28
monthUtils.getDay(monthUtils.currentDay()) >= 28
? 27
: monthUtils.getDay(monthUtils.currentDay()) -
1
].thisMonth,
Expand All @@ -194,8 +194,8 @@ export function Spending() {
{amountToCurrency(
Math.abs(
data.intervalData[
monthUtils.getDay(monthUtils.currentDay()) >= 29
? 28
monthUtils.getDay(monthUtils.currentDay()) >= 28
? 27
: monthUtils.getDay(monthUtils.currentDay()) -
1
].lastMonth,
Expand All @@ -215,8 +215,8 @@ export function Spending() {
Math.abs(
data.intervalData[
monthUtils.getDay(monthUtils.currentDay()) >=
29
? 28
28
? 27
: monthUtils.getDay(
monthUtils.currentDay(),
) - 1
Expand Down Expand Up @@ -249,6 +249,12 @@ export function Spending() {
>
Last month
</ModeButton>
<ModeButton
selected={mode === 'Last year'}
onSelect={() => setMode('Last year')}
>
Last year
</ModeButton>
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
{showAverage && (
<ModeButton
selected={mode === 'Average'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ export function SpendingCard() {
}, [categories]);

const data = useReport('default', getGraphData);
const todayDate = monthUtils.getDay(monthUtils.currentDay());
const difference =
data &&
data.intervalData[monthUtils.getDay(monthUtils.currentDay()) - 1].average -
data.intervalData[monthUtils.getDay(monthUtils.currentDay()) - 1]
.thisMonth;
data.intervalData[todayDate >= 28 ? 27 : todayDate - 1].average -
data.intervalData[todayDate >= 28 ? 27 : todayDate - 1].thisMonth;

return (
<ReportCard flex="1" to="/reports/spending">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-strict-ignore

import keyBy from 'lodash/keyBy';

import { runQuery } from 'loot-core/src/client/query-helpers';
Expand All @@ -26,15 +25,18 @@ type createSpendingSpreadsheetProps = {
conditions?: RuleConditionEntity[];
conditionsOp?: string;
setDataCheck?: (value: boolean) => void;
mode?: string;
};

export function createSpendingSpreadsheet({
categories,
conditions = [],
conditionsOp,
setDataCheck,
mode,
}: createSpendingSpreadsheetProps) {
const [startDate, endDate] = getSpecificRange(3, null, 'Months');
const [startDate2, endDate2] = getSpecificRange(13, 1, 'Months');
const interval = 'Daily';

return async (
Expand All @@ -45,7 +47,6 @@ export function createSpendingSpreadsheet({
conditions: conditions.filter(cond => !cond.customName),
});
const conditionsOpKey = conditionsOp === 'or' ? '$or' : '$and';

const [assets, debts] = await Promise.all([
runQuery(
makeQuery(
Expand All @@ -70,8 +71,38 @@ export function createSpendingSpreadsheet({
),
).then(({ data }) => data),
]);
if (mode === 'Last year') {
const [assetsLastYear, debtsLastYear] = await Promise.all([
runQuery(
makeQuery(
'assets',
startDate2,
endDate2,
interval,
categories.list,
conditionsOpKey,
filters,
),
).then(({ data }) => data),
runQuery(
makeQuery(
'debts',
startDate2,
endDate2,
interval,
categories.list,
conditionsOpKey,
filters,
),
).then(({ data }) => data),
]);
// Add data from last year to existing arrays
assets.push(...assetsLastYear);
debts.push(...debtsLastYear);
}

const intervals = monthUtils.dayRangeInclusive(startDate, endDate);
intervals.push(...monthUtils.dayRangeInclusive(startDate2, endDate2));
const days = [...Array(29).keys()]
.filter(f => f > 0)
.map(n => n.toString().padStart(2, '0'));
Expand All @@ -85,6 +116,12 @@ export function createSpendingSpreadsheet({
return { month, perMonthAssets: 0, perMonthDebts: 0 };
});

months.unshift({
month: monthUtils.prevYear(monthUtils.currentMonth()),
perMonthAssets: 0,
perMonthDebts: 0,
});

const intervalData = days.map(day => {
let averageSum = 0;
let monthCount = 0;
Expand All @@ -96,7 +133,6 @@ export function createSpendingSpreadsheet({
: intervalItem.substring(8, 10);
let perIntervalAssets = 0;
let perIntervalDebts = 0;

if (
month.month === monthUtils.getMonth(intervalItem) &&
day === offsetDay
Expand Down Expand Up @@ -126,7 +162,10 @@ export function createSpendingSpreadsheet({
}
return null;
});
if (month.month !== monthUtils.currentMonth()) {
if (
month.month !== monthUtils.currentMonth() &&
month.month !== monthUtils.prevYear(monthUtils.currentMonth())
) {
averageSum += cumulativeAssets + cumulativeDebts;
monthCount += 1;
}
Expand Down Expand Up @@ -165,6 +204,7 @@ export function createSpendingSpreadsheet({
average: integerToAmount(averageSum) / monthCount,
thisMonth: dayData[3].cumulative,
lastMonth: dayData[2].cumulative,
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
lastYear: dayData[0].cumulative,
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
};
});

Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/2806.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Features
authors: [Crazypkr1099]
---

Add Year Spending Comparison Feature & Bug Fixes
Crazypkr1099 marked this conversation as resolved.
Show resolved Hide resolved
Loading