Skip to content

A local-first personal finance app

Sankey with Recharts (#1739)

Shaan Khosla5008eb022f86
diff --git a/packages/desktop-client/src/components/reports/Overview.js b/packages/desktop-client/src/components/reports/Overview.js
index 10e6aad5ed..0758f187a1 100644
--- a/packages/desktop-client/src/components/reports/Overview.js
+++ b/packages/desktop-client/src/components/reports/Overview.js
@@ -24,6 +24,8 @@ import categorySpendingSpreadsheet from './graphs/category-spending-spreadsheet'
import CategorySpendingGraph from './graphs/CategorySpendingGraph';
import netWorthSpreadsheet from './graphs/net-worth-spreadsheet';
import NetWorthGraph from './graphs/NetWorthGraph';
+import sankeySpreadsheet from './graphs/sankey-spreadsheet';
+import SankeyGraph from './graphs/SankeyGraph';
import Tooltip from './Tooltip';
import useReport from './useReport';
@@ -325,10 +327,46 @@ function CategorySpendingCard() {
);
}
+function SankeyCard() {
+ const { grouped: categoryGroups } = useCategories();
+ const end = monthUtils.currentMonth();
+ const start = monthUtils.subMonths(end, 5);
+
+ const params = useMemo(
+ () => sankeySpreadsheet(start, end, categoryGroups),
+ [start, end, categoryGroups],
+ );
+ const data = useReport('sankey', params);
+
+ return (
+ <Card flex={1} to="/reports/sankey">
+ <View style={{ flexDirection: 'row', padding: 20 }}>
+ <View style={{ flex: 1 }}>
+ <Block
+ style={{ ...styles.mediumText, fontWeight: 500, marginBottom: 5 }}
+ role="heading"
+ >
+ Sankey
+ </Block>
+ <DateRange start={start} end={end} />
+ </View>
+ </View>
+ <View style={{ flex: 1 }}>
+ {data ? (
+ <SankeyGraph data={data} compact={true} />
+ ) : (
+ <LoadingIndicator />
+ )}
+ </View>
+ </Card>
+ );
+}
+
export default function Overview() {
let categorySpendingReportFeatureFlag = useFeatureFlag(
'categorySpendingReport',
);
+ let sankeyFeatureFlag = useFeatureFlag('sankeyReport');
let accounts = useSelector(state => state.queries.accounts);
return (
@@ -348,16 +386,21 @@ export default function Overview() {
<CashFlowCard />
</View>
- {categorySpendingReportFeatureFlag && (
+ {(sankeyFeatureFlag || categorySpendingReportFeatureFlag) && (
<View
style={{
flex: '0 0 auto',
flexDirection: 'row',
}}
>
- <CategorySpendingCard />
- <div style={{ flex: 1 }} />
- <div style={{ flex: 1 }} />
+ {categorySpendingReportFeatureFlag && <CategorySpendingCard />}
+ {sankeyFeatureFlag && <SankeyCard />}
+ {(!categorySpendingReportFeatureFlag || !sankeyFeatureFlag) && (
+ <>
+ <div style={{ flex: 1 }} />
+ <div style={{ flex: 1 }} />
+ </>
+ )}
</View>
)}
</View>
diff --git a/packages/desktop-client/src/components/reports/ReportRouter.js b/packages/desktop-client/src/components/reports/ReportRouter.js
index 58a52a0958..868889e348 100644
--- a/packages/desktop-client/src/components/reports/ReportRouter.js
+++ b/packages/desktop-client/src/components/reports/ReportRouter.js
@@ -5,6 +5,7 @@ import CashFlow from './CashFlow';
import CategorySpending from './CategorySpending';
import NetWorth from './NetWorth';
import Overview from './Overview';
+import Sankey from './Sankey';
export function ReportRouter() {
return (
@@ -13,6 +14,7 @@ export function ReportRouter() {
<Route path="/net-worth" element={<NetWorth />} />
<Route path="/cash-flow" element={<CashFlow />} />
<Route path="/category-spending" element={<CategorySpending />} />
+ <Route path="/sankey" element={<Sankey />} />
</Routes>
);
}
diff --git a/packages/desktop-client/src/components/reports/Sankey.js b/packages/desktop-client/src/components/reports/Sankey.js
new file mode 100644
index 0000000000..849559c23f
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/Sankey.js
@@ -0,0 +1,136 @@
+import React, { useState, useEffect, useMemo } from 'react';
+
+import * as d from 'date-fns';
+
+import { send } from 'loot-core/src/platform/client/fetch';
+import * as monthUtils from 'loot-core/src/shared/months';
+
+import useCategories from '../../hooks/useCategories';
+import useFilters from '../../hooks/useFilters';
+import { theme, styles } from '../../style';
+import Paragraph from '../common/Paragraph';
+import View from '../common/View';
+
+import sankeySpreadsheet from './graphs/sankey-spreadsheet';
+import SankeyGraph from './graphs/SankeyGraph';
+import Header from './Header';
+import useReport from './useReport';
+import { fromDateRepr } from './util';
+
+export default function Sankey() {
+ const { grouped: categoryGroups } = useCategories();
+ const {
+ filters,
+ saved,
+ conditionsOp,
+ onApply: onApplyFilter,
+ onDelete: onDeleteFilter,
+ onUpdate: onUpdateFilter,
+ onCondOpChange,
+ } = useFilters();
+
+ const [allMonths, setAllMonths] = useState(null);
+ const [start, setStart] = useState(
+ monthUtils.subMonths(monthUtils.currentMonth(), 5),
+ );
+ const [end, setEnd] = useState(monthUtils.currentMonth());
+
+ const params = useMemo(
+ () => sankeySpreadsheet(start, end, categoryGroups, filters, conditionsOp),
+ [start, end, categoryGroups, filters, conditionsOp],
+ );
+ const data = useReport('sankey', params);
+ useEffect(() => {
+ async function run() {
+ const trans = await send('get-earliest-transaction');
+ const currentMonth = monthUtils.currentMonth();
+ let earliestMonth = trans
+ ? monthUtils.monthFromDate(d.parseISO(fromDateRepr(trans.date)))
+ : currentMonth;
+
+ // Make sure the month selects are at least populates with a
+ // year's worth of months. We can undo this when we have fancier
+ // date selects.
+ const yearAgo = monthUtils.subMonths(monthUtils.currentMonth(), 12);
+ if (earliestMonth > yearAgo) {
+ earliestMonth = yearAgo;
+ }
+
+ const allMonths = monthUtils
+ .rangeInclusive(earliestMonth, monthUtils.currentMonth())
+ .map(month => ({
+ name: month,
+ pretty: monthUtils.format(month, 'MMMM, yyyy'),
+ }))
+ .reverse();
+
+ setAllMonths(allMonths);
+ }
+ run();
+ }, []);
+
+ function onChangeDates(start, end) {
+ setStart(start);
+ setEnd(end);
+ }
+
+ if (!allMonths || !data) {
+ return null;
+ }
+
+ return (
+ <View style={{ ...styles.page, minWidth: 650, overflow: 'hidden' }}>
+ <Header
+ title="Sankey"
+ allMonths={allMonths}
+ start={start}
+ end={end}
+ onChangeDates={onChangeDates}
+ filters={filters}
+ saved={saved}
+ onApply={onApplyFilter}
+ onUpdateFilter={onUpdateFilter}
+ onDeleteFilter={onDeleteFilter}
+ conditionsOp={conditionsOp}
+ onCondOpChange={onCondOpChange}
+ />
+
+ <View
+ style={{
+ backgroundColor: theme.tableBackground,
+ padding: 30,
+ paddingTop: 0,
+ overflow: 'auto',
+ flexGrow: 1,
+ }}
+ >
+ <View
+ style={{
+ textAlign: 'right',
+ paddingTop: 20,
+ paddingRight: 20,
+ flexShrink: 0,
+ }}
+ >
+ <View
+ style={{ ...styles.largeText, fontWeight: 400, marginBottom: 5 }}
+ />
+ </View>
+
+ <SankeyGraph style={{ flexGrow: 1 }} data={data} />
+
+ <View style={{ marginTop: 30 }}>
+ <Paragraph>
+ <strong>What is a Sankey plot?</strong>
+ </Paragraph>
+ <Paragraph>
+ A Sankey plot visualizes the flow of quantities between multiple
+ categories, emphasizing the distribution and proportional
+ relationships of data streams. If you hover over the graph, you can
+ see detailed flow values between categories.
+ </Paragraph>
+ </View>
+ </View>
+ </View>
+ );
+}
diff --git a/packages/desktop-client/src/components/reports/graphs/SankeyGraph.tsx b/packages/desktop-client/src/components/reports/graphs/SankeyGraph.tsx
new file mode 100644
index 0000000000..182b0fc7d4
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/graphs/SankeyGraph.tsx
@@ -0,0 +1,145 @@
+import React from 'react';
+
+import {
+ Sankey,
+ Tooltip,
+ Rectangle,
+ Layer,
+ ResponsiveContainer,
+} from 'recharts';
+
+import Container from '../Container';
+
+type SankeyProps = {
+ style;
+ data;
+ compact: boolean;
+};
+type PotentialNumber = number | string | undefined | null;
+
+const numberFormatterTooltip = (value: PotentialNumber): number | null => {
+ if (typeof value === 'number') {
+ return Math.round(value);
+ }
+ return null; // or some default value for other cases
+};
+
+function SankeyNode({ x, y, width, height, index, payload, containerWidth }) {
+ const isOut = x + width + 6 > containerWidth;
+ let payloadValue = Math.round(payload.value / 1000).toString();
+ if (payload.value < 1000) {
+ payloadValue = '<1k';
+ } else {
+ payloadValue = payloadValue + 'k';
+ }
+ return (
+ <Layer key={`CustomNode${index}`}>
+ <Rectangle
+ x={x}
+ y={y}
+ width={width}
+ height={height}
+ fill="#5192ca"
+ fillOpacity="1"
+ />
+ <text
+ textAnchor={isOut ? 'end' : 'start'}
+ x={isOut ? x - 6 : x + width + 6}
+ y={y + height / 2}
+ fontSize="13"
+ >
+ {payload.name}
+ </text>
+ <text
+ textAnchor={isOut ? 'end' : 'start'}
+ x={isOut ? x - 6 : x + width + 6}
+ y={y + height / 2 + 13}
+ fontSize="9"
+ strokeOpacity="0.5"
+ >
+ {payloadValue}
+ </text>
+ </Layer>
+ );
+}
+
+function convertToCondensed(data) {
+ const budgetNodeIndex = data.nodes.findIndex(node => node.name === 'Budget');
+
+ // Calculate total income (links going into the "Budget" node)
+ const totalIncome = data.links.reduce((acc, link) => {
+ return link.target === budgetNodeIndex ? acc + link.value : acc;
+ }, 0);
+
+ // Calculate total expenses (links going out of the "Budget" node)
+ const totalExpenses = data.links.reduce((acc, link) => {
+ return link.source === budgetNodeIndex ? acc + link.value : acc;
+ }, 0);
+
+ return {
+ nodes: [{ name: 'Income' }, { name: 'Budget' }, { name: 'Expenses' }],
+ links: [
+ { source: 0, target: 1, value: totalIncome },
+ { source: 1, target: 2, value: totalExpenses },
+ ],
+ };
+}
+
+function SankeyGraph({ style, data, compact }: SankeyProps) {
+ let sankeyData = compact ? convertToCondensed(data) : data;
+
+ if (!data.links || data.links.length === 0) return null;
+ const margin = {
+ left: 0,
+ right: 0,
+ top: compact ? 0 : 10,
+ bottom: compact ? 0 : 25,
+ };
+
+ return compact ? (
+ <ResponsiveContainer>
+ <Sankey
+ data={sankeyData}
+ node={props => <SankeyNode {...props} />}
+ sort={true}
+ iterations={1000}
+ nodePadding={23}
+ margin={margin}
+ >
+ <Tooltip
+ formatter={numberFormatterTooltip}
+ isAnimationActive={false}
+ separator=": "
+ />
+ </Sankey>
+ </ResponsiveContainer>
+ ) : (
+ <Container
+ style={{
+ ...style,
+ ...(compact && { height: 'auto' }),
+ }}
+ >
+ {(width, height, portalHost) => (
+ <ResponsiveContainer>
+ <Sankey
+ data={sankeyData}
+ node={props => <SankeyNode {...props} containerWidth={width} />}
+ sort={true}
+ iterations={1000}
+ nodePadding={23}
+ margin={margin}
+ >
+ <Tooltip
+ formatter={numberFormatterTooltip}
+ isAnimationActive={false}
+ separator=": "
+ />
+ </Sankey>
+ </ResponsiveContainer>
+ )}
+ </Container>
+ );
+}
+
+export default SankeyGraph;
diff --git a/packages/desktop-client/src/components/reports/graphs/sankey-spreadsheet.tsx b/packages/desktop-client/src/components/reports/graphs/sankey-spreadsheet.tsx
new file mode 100644
index 0000000000..7946aff2a5
--- /dev/null
+++ b/packages/desktop-client/src/components/reports/graphs/sankey-spreadsheet.tsx
@@ -0,0 +1,191 @@
+import q, { runQuery } from 'loot-core/src/client/query-helpers';
+import { send } from 'loot-core/src/platform/client/fetch';
+import { integerToAmount } from 'loot-core/src/shared/util';
+
+export default function createSpreadsheet(
+ start,
+ end,
+ categories,
+ conditions = [],
+ conditionsOp,
+) {
+ return async (spreadsheet, setData) => {
+ // gather filters user has set
+ let { filters } = await send('make-filters-from-conditions', {
+ conditions: conditions.filter(cond => !cond.customName),
+ });
+ const conditionsOpKey = conditionsOp === 'or' ? '$or' : '$and';
+
+ // create list of Income subcategories
+ const allIncomeSubcategories = [].concat(
+ ...categories
+ .filter(category => category.is_income === 1)
+ .map(category => category.categories),
+ );
+
+ // retrieve sum of subcategory expenses
+ async function fetchCategoryData(categories) {
+ try {
+ return await Promise.all(
+ categories.map(async mainCategory => {
+ let subcategoryBalances = await Promise.all(
+ mainCategory.categories
+ .filter(subcategory => subcategory.is_income !== 1)
+ .map(async subcategory => {
+ const results = await runQuery(
+ q('transactions')
+ .filter({
+ [conditionsOpKey]: filters,
+ })
+ .filter({
+ $and: [
+ { date: { $gte: start + '-01' } },
+ { date: { $lte: end + '-31' } },
+ ],
+ })
+ .filter({ category: subcategory.id })
+ .calculate({ $sum: '$amount' }),
+ );
+ return {
+ subcategory: subcategory.name,
+ value: results.data * -1,
+ };
+ }),
+ );
+
+ // Here you could combine, reduce or transform the subcategoryBalances if needed
+ return {
+ name: mainCategory.name,
+ balances: subcategoryBalances,
+ };
+ }),
+ );
+ } catch (error) {
+ console.error('Error fetching category data:', error);
+ throw error; // Re-throw if you want the error to propagate
+ }
+ }
+
+ // retrieve all income subcategory payees
+ async function fetchIncomeData() {
+ // Map over allIncomeSubcategories and return an array of promises
+ const promises = allIncomeSubcategories.map(subcategory => {
+ return runQuery(
+ q('transactions')
+ .filter({
+ [conditionsOpKey]: filters,
+ })
+ .filter({
+ $and: [
+ { date: { $gte: start + '-01' } },
+ { date: { $lte: end + '-31' } },
+ ],
+ })
+ .filter({ category: subcategory.id })
+ .groupBy(['payee'])
+ .select(['payee', { amount: { $sum: '$amount' } }]),
+ );
+ });
+
+ // Use Promise.all() to wait for all queries to complete
+ const resultsArrays = await Promise.all(promises);
+
+ // unravel the results
+ let payeesDict = {};
+ resultsArrays.forEach(item => {
+ item.data.forEach(innerItem => {
+ payeesDict[innerItem.payee] = innerItem.amount;
+ });
+ });
+
+ // First, collect all unique IDs from payeesDict
+ let payeeIds = Object.keys(payeesDict);
+
+ const results = await runQuery(
+ q('payees')
+ .filter({ id: { $oneof: payeeIds } })
+ .select(['id', 'name']),
+ );
+
+ // Convert the resulting array to a payee-name-map
+ let payeeNames = {};
+ results.data.forEach(item => {
+ if (item.name && payeesDict[item.id]) {
+ payeeNames[item.name] = payeesDict[item.id];
+ }
+ });
+ return payeeNames;
+ }
+ const categoryData = await fetchCategoryData(categories);
+ const incomeData = await fetchIncomeData();
+
+ // convert retrieved data into the proper sankey format
+ setData(transformToSankeyData(categoryData, incomeData));
+ };
+}
+
+function transformToSankeyData(categoryData, incomeData) {
+ const data = { nodes: [], links: [] };
+ const nodeNames = new Set();
+
+ // Add the Budget node first.
+ data.nodes.push({ name: 'Budget' });
+ nodeNames.add('Budget');
+
+ // Handle the income sources and link them to the Budget node.
+ Object.entries(incomeData).forEach(([sourceName, value]) => {
+ if (!nodeNames.has(sourceName) && integerToAmount(value) > 0) {
+ data.nodes.push({ name: sourceName });
+ nodeNames.add(sourceName);
+ data.links.push({
+ source: sourceName,
+ target: 'Budget',
+ value: integerToAmount(value),
+ });
+ }
+ });
+
+ // add all category expenses that have valid subcategories and a balance
+ for (let mainCategory of categoryData) {
+ if (!nodeNames.has(mainCategory.name) && mainCategory.balances.length > 0) {
+ let mainCategorySum = 0;
+ for (let subCategory of mainCategory.balances) {
+ if (!nodeNames.has(subCategory.subcategory) && subCategory.value > 0) {
+ mainCategorySum += subCategory.value;
+ }
+ }
+ if (mainCategorySum === 0) {
+ continue;
+ }
+ data.nodes.push({ name: mainCategory.name });
+ nodeNames.add(mainCategory.name);
+ data.links.push({
+ source: 'Budget',
+ target: mainCategory.name,
+ value: integerToAmount(mainCategorySum),
+ });
+
+ // add the subcategories of the main category
+ for (let subCategory of mainCategory.balances) {
+ if (!nodeNames.has(subCategory.subcategory) && subCategory.value > 0) {
+ data.nodes.push({ name: subCategory.subcategory });
+ nodeNames.add(subCategory.subcategory);
+
+ data.links.push({
+ source: mainCategory.name,
+ target: subCategory.subcategory,
+ value: integerToAmount(subCategory.value),
+ });
+ }
+ }
+ }
+ }
+
+ // Map source and target in links to the index of the node
+ data.links.forEach(link => {
+ link.source = data.nodes.findIndex(node => node.name === link.source);
+ link.target = data.nodes.findIndex(node => node.name === link.target);
+ });
+
+ return data;
+}
diff --git a/packages/desktop-client/src/components/settings/Experimental.tsx b/packages/desktop-client/src/components/settings/Experimental.tsx
index 2a4a5646ee..e8e62cbf7c 100644
--- a/packages/desktop-client/src/components/settings/Experimental.tsx
+++ b/packages/desktop-client/src/components/settings/Experimental.tsx
@@ -102,6 +102,8 @@ export default function ExperimentalFeatures() {
Category spending report
</FeatureToggle>
+ <FeatureToggle flag="sankeyReport">Sankey report</FeatureToggle>
+
<ReportBudgetFeature />
<FeatureToggle flag="goalTemplatesEnabled">
diff --git a/packages/desktop-client/src/hooks/useFeatureFlag.ts b/packages/desktop-client/src/hooks/useFeatureFlag.ts
index 84571e1fcb..4a48e58578 100644
--- a/packages/desktop-client/src/hooks/useFeatureFlag.ts
+++ b/packages/desktop-client/src/hooks/useFeatureFlag.ts
@@ -4,6 +4,7 @@ import type { FeatureFlag } from 'loot-core/src/types/prefs';
const DEFAULT_FEATURE_FLAG_STATE: Record<FeatureFlag, boolean> = {
categorySpendingReport: false,
+ sankeyReport: false,
reportBudget: false,
goalTemplatesEnabled: false,
themes: false,
diff --git a/packages/loot-core/src/types/prefs.d.ts b/packages/loot-core/src/types/prefs.d.ts
index 67e870f4c3..65a04d4606 100644
--- a/packages/loot-core/src/types/prefs.d.ts
+++ b/packages/loot-core/src/types/prefs.d.ts
@@ -2,6 +2,7 @@ import { type numberFormats } from '../shared/util';
export type FeatureFlag =
| 'categorySpendingReport'
+ | 'sankeyReport'
| 'reportBudget'
| 'goalTemplatesEnabled'
| 'themes'
diff --git a/upcoming-release-notes/1739.md b/upcoming-release-notes/1739.md
new file mode 100644
index 0000000000..e7b095813d
--- /dev/null
+++ b/upcoming-release-notes/1739.md
@@ -0,0 +1,6 @@
+---
+category: Features
+authors: [shaankhosla]
+---
+
+Added a Sankey chart report as an experimental feature.
\ No newline at end of file