-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactored cash-flow report from victory to recharts (#2260)
- Loading branch information
1 parent
a6e38ad
commit a4e97e0
Showing
8 changed files
with
216 additions
and
136 deletions.
There are no files selected for viewing
Binary file modified
BIN
+3.59 KB
(100%)
...snapshots/Reports-loads-cash-flow-graph-and-checks-visuals-1-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.2 KB
(100%)
...snapshots/Reports-loads-cash-flow-graph-and-checks-visuals-2-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-134 Bytes
(100%)
...js-snapshots/Reports-loads-net-worth-and-cash-flow-reports-1-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-143 Bytes
(100%)
...js-snapshots/Reports-loads-net-worth-and-cash-flow-reports-2-chromium-linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
202 changes: 150 additions & 52 deletions
202
packages/desktop-client/src/components/reports/graphs/CashFlowGraph.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,164 @@ | ||
// @ts-strict-ignore | ||
import React from 'react'; | ||
import React, { useState } from 'react'; | ||
|
||
import * as d from 'date-fns'; | ||
import { css } from 'glamor'; | ||
import { | ||
VictoryChart, | ||
VictoryBar, | ||
VictoryLine, | ||
VictoryAxis, | ||
VictoryVoronoiContainer, | ||
VictoryGroup, | ||
} from 'victory'; | ||
Bar, | ||
CartesianGrid, | ||
ComposedChart, | ||
Line, | ||
ReferenceLine, | ||
ResponsiveContainer, | ||
Tooltip, | ||
XAxis, | ||
YAxis, | ||
type TooltipProps, | ||
} from 'recharts'; | ||
|
||
import { usePrivacyMode } from 'loot-core/src/client/privacy'; | ||
import { | ||
amountToCurrency, | ||
amountToCurrencyNoDecimal, | ||
} from 'loot-core/src/shared/util'; | ||
|
||
import { theme } from '../../../style'; | ||
import { AlignedText } from '../../common/AlignedText'; | ||
import { chartTheme } from '../chart-theme'; | ||
import { Container } from '../Container'; | ||
import { Tooltip } from '../Tooltip'; | ||
|
||
type CashFlowGraphProps = { | ||
graphData: { expenses; income; balances }; | ||
const MAX_BAR_SIZE = 50; | ||
const ANIMATION_DURATION = 1000; // in ms | ||
|
||
type CustomTooltipProps = TooltipProps<number, 'date'> & { | ||
isConcise: boolean; | ||
}; | ||
export function CashFlowGraph({ graphData, isConcise }: CashFlowGraphProps) { | ||
|
||
function CustomTooltip({ active, payload, isConcise }: CustomTooltipProps) { | ||
if (!active || !payload) { | ||
return null; | ||
} | ||
|
||
const [{ payload: data }] = payload; | ||
|
||
return ( | ||
<Container> | ||
{(width, height, portalHost) => | ||
graphData && ( | ||
<VictoryChart | ||
scale={{ x: 'time', y: 'linear' }} | ||
theme={chartTheme} | ||
domainPadding={10} | ||
width={width} | ||
height={height} | ||
containerComponent={ | ||
<VictoryVoronoiContainer voronoiDimension="x" /> | ||
<div | ||
className={`${css({ | ||
pointerEvents: 'none', | ||
borderRadius: 2, | ||
boxShadow: '0 1px 6px rgba(0, 0, 0, .20)', | ||
backgroundColor: theme.menuBackground, | ||
color: theme.menuItemText, | ||
padding: 10, | ||
})}`} | ||
> | ||
<div> | ||
<div style={{ marginBottom: 10 }}> | ||
<strong> | ||
{d.format(data.date, isConcise ? 'MMMM yyyy' : 'MMMM dd, yyyy')} | ||
</strong> | ||
</div> | ||
<div style={{ lineHeight: 1.5 }}> | ||
<AlignedText left="Income:" right={amountToCurrency(data.income)} /> | ||
<AlignedText | ||
left="Expenses:" | ||
right={amountToCurrency(data.expenses)} | ||
/> | ||
<AlignedText | ||
left="Change:" | ||
right={ | ||
<strong>{amountToCurrency(data.income + data.expenses)}</strong> | ||
} | ||
> | ||
<VictoryGroup> | ||
<VictoryBar | ||
data={graphData.expenses} | ||
style={{ data: { fill: chartTheme.colors.red } }} | ||
/> | ||
<VictoryBar data={graphData.income} /> | ||
</VictoryGroup> | ||
<VictoryLine | ||
data={graphData.balances} | ||
labelComponent={<Tooltip portalHost={portalHost} />} | ||
labels={x => x.premadeLabel} | ||
style={{ | ||
data: { stroke: theme.pageTextLight }, | ||
}} | ||
/> | ||
{data.transfers !== 0 && ( | ||
<AlignedText | ||
left="Transfers:" | ||
right={amountToCurrency(data.transfers)} | ||
/> | ||
<VictoryAxis | ||
// eslint-disable-next-line rulesdir/typography | ||
tickFormat={x => d.format(x, isConcise ? "MMM ''yy" : 'MMM d')} | ||
tickValues={graphData.balances.map(item => item.x)} | ||
tickCount={Math.min(5, graphData.balances.length)} | ||
offsetY={50} | ||
/> | ||
<VictoryAxis dependentAxis crossAxis={false} /> | ||
</VictoryChart> | ||
) | ||
} | ||
</Container> | ||
)} | ||
<AlignedText left="Balance:" right={amountToCurrency(data.balance)} /> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
type CashFlowGraphProps = { | ||
graphData: { | ||
expenses: { x: Date; y: number }[]; | ||
income: { x: Date; y: number }[]; | ||
balances: { x: Date; y: number }[]; | ||
transfers: { x: Date; y: number }[]; | ||
}; | ||
isConcise: boolean; | ||
}; | ||
export function CashFlowGraph({ graphData, isConcise }: CashFlowGraphProps) { | ||
const privacyMode = usePrivacyMode(); | ||
const [yAxisIsHovered, setYAxisIsHovered] = useState(false); | ||
|
||
const data = graphData.expenses.map((row, idx) => ({ | ||
date: row.x, | ||
expenses: row.y, | ||
income: graphData.income[idx].y, | ||
balance: graphData.balances[idx].y, | ||
transfers: graphData.transfers[idx].y, | ||
})); | ||
|
||
return ( | ||
<ResponsiveContainer width="100%" height={300}> | ||
<ComposedChart stackOffset="sign" data={data}> | ||
<CartesianGrid strokeDasharray="3 3" vertical={false} /> | ||
<XAxis | ||
dataKey="date" | ||
tick={{ fill: theme.reportsLabel }} | ||
tickFormatter={x => { | ||
// eslint-disable-next-line rulesdir/typography | ||
return d.format(x, isConcise ? "MMM ''yy" : 'MMM d'); | ||
}} | ||
minTickGap={50} | ||
/> | ||
<YAxis | ||
tick={{ fill: theme.reportsLabel }} | ||
tickCount={8} | ||
tickFormatter={value => | ||
privacyMode && !yAxisIsHovered | ||
? '...' | ||
: amountToCurrencyNoDecimal(value) | ||
} | ||
onMouseEnter={() => setYAxisIsHovered(true)} | ||
onMouseLeave={() => setYAxisIsHovered(false)} | ||
/> | ||
<Tooltip | ||
labelFormatter={x => { | ||
// eslint-disable-next-line rulesdir/typography | ||
return d.format(x, isConcise ? "MMM ''yy" : 'MMM d'); | ||
}} | ||
content={<CustomTooltip isConcise={isConcise} />} | ||
isAnimationActive={false} | ||
/> | ||
|
||
<ReferenceLine y={0} stroke="#000" /> | ||
<Bar | ||
dataKey="income" | ||
stackId="a" | ||
fill={chartTheme.colors.blue} | ||
maxBarSize={MAX_BAR_SIZE} | ||
animationDuration={ANIMATION_DURATION} | ||
/> | ||
<Bar | ||
dataKey="expenses" | ||
stackId="a" | ||
fill={chartTheme.colors.red} | ||
maxBarSize={MAX_BAR_SIZE} | ||
animationDuration={ANIMATION_DURATION} | ||
/> | ||
<Line | ||
type="monotone" | ||
dataKey="balance" | ||
dot={false} | ||
stroke={theme.pageTextLight} | ||
strokeWidth={2} | ||
animationDuration={ANIMATION_DURATION} | ||
/> | ||
</ComposedChart> | ||
</ResponsiveContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.