-
-
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.
Signed-off-by: Johannes Löthberg <[email protected]>
- Loading branch information
Showing
8 changed files
with
521 additions
and
65 deletions.
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
packages/desktop-client/src/components/reports/CategorySpending.js
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 |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import React, { useState, useEffect, useMemo } from 'react'; | ||
import { connect } from 'react-redux'; | ||
|
||
import * as d from 'date-fns'; | ||
import { bindActionCreators } from 'redux'; | ||
|
||
import * as actions from 'loot-core/src/client/actions'; | ||
import { send } from 'loot-core/src/platform/client/fetch'; | ||
import * as monthUtils from 'loot-core/src/shared/months'; | ||
import { integerToCurrency } from 'loot-core/src/shared/util'; | ||
|
||
import { colors, styles } from '../../style'; | ||
import { AlignedText, Block, Text, View } from '../common'; | ||
|
||
import categorySpendingSpreadsheet from './graphs/category-spending-spreadsheet'; | ||
import CategorySpendingGraph from './graphs/CategorySpendingGraph'; | ||
import Header from './Header'; | ||
import useReport from './useReport'; | ||
import { fromDateRepr } from './util'; | ||
|
||
function CategoryAverage({ getCategories }) { | ||
const [categoryGroups, setCategoryGroups] = useState([]); | ||
|
||
const [allMonths, setAllMonths] = useState(null); | ||
|
||
const [start, setStart] = useState( | ||
monthUtils.subMonths(monthUtils.currentMonth(), 5), | ||
); | ||
const [end, setEnd] = useState(monthUtils.currentMonth()); | ||
|
||
const [numberOfMonthsAverage, setNumberOfMonthsAverage] = useState(3); | ||
|
||
const [selectedCategoryId, setSelectedCategoryId] = useState(null); | ||
|
||
const getData = useMemo( | ||
() => | ||
categorySpendingSpreadsheet( | ||
start, | ||
end, | ||
numberOfMonthsAverage, | ||
selectedCategoryId, | ||
), | ||
[start, end, numberOfMonthsAverage, selectedCategoryId], | ||
); | ||
const graphData = useReport('category_spending', getData); | ||
|
||
useEffect(() => { | ||
getCategories().then(categories => setCategoryGroups(categories.grouped)); | ||
}, []); | ||
|
||
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 || !graphData) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<View style={[styles.page, { minWidth: 650, overflow: 'hidden' }]}> | ||
<Header | ||
title="Category Spending" | ||
allMonths={allMonths} | ||
start={start} | ||
end={end} | ||
onChangeDates={onChangeDates} | ||
category={selectedCategoryId} | ||
categoryGroups={categoryGroups} | ||
onChangeCategory={setSelectedCategoryId} | ||
numberOfMonths={numberOfMonthsAverage} | ||
numberOfMonthsOptions={[1, 3, 6, 12]} | ||
onChangeNumberOfMonths={setNumberOfMonthsAverage} | ||
/> | ||
|
||
<View | ||
style={{ | ||
backgroundColor: 'white', | ||
padding: 30, | ||
paddingTop: 0, | ||
overflow: 'auto', | ||
}} | ||
> | ||
<View | ||
style={{ | ||
paddingTop: 20, | ||
paddingRight: 20, | ||
flexShrink: 0, | ||
alignItems: 'flex-end', | ||
color: colors.n3, | ||
}} | ||
> | ||
<AlignedText | ||
style={{ marginBottom: 5, minWidth: 160 }} | ||
left={<Block>Budgeted:</Block>} | ||
right={ | ||
<Text style={{ fontWeight: 600 }}> | ||
{integerToCurrency( | ||
graphData.length === 0 | ||
? 0 | ||
: graphData.reduce( | ||
(total, month) => total + month.budgeted, | ||
0, | ||
), | ||
)} | ||
</Text> | ||
} | ||
/> | ||
<AlignedText | ||
style={{ marginBottom: 5, minWidth: 160 }} | ||
left={<Block>Spent:</Block>} | ||
right={ | ||
<Text style={{ fontWeight: 600 }}> | ||
{integerToCurrency( | ||
graphData.length === 0 | ||
? 0 | ||
: graphData.reduce( | ||
(total, month) => total + month.total, | ||
0, | ||
), | ||
)} | ||
</Text> | ||
} | ||
/> | ||
<AlignedText | ||
style={{ marginBottom: 5, minWidth: 160 }} | ||
left={<Block>Average:</Block>} | ||
right={ | ||
<Text style={{ fontWeight: 600 }}> | ||
{integerToCurrency( | ||
Math.round( | ||
graphData.length === 0 | ||
? 0 | ||
: graphData.reduce( | ||
(total, month) => total + month.total, | ||
0, | ||
) / graphData.length, | ||
), | ||
)} | ||
</Text> | ||
} | ||
/> | ||
</View> | ||
|
||
<CategorySpendingGraph start={start} end={end} graphData={graphData} /> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
export default connect( | ||
// Deleting this leads to "Uncaught TypeError: dispatch is not a function" | ||
state => ({}), | ||
dispatch => bindActionCreators(actions, dispatch), | ||
)(CategoryAverage); |
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
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
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
76 changes: 76 additions & 0 deletions
76
packages/desktop-client/src/components/reports/graphs/CategorySpendingGraph.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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import * as d from 'date-fns'; | ||
import { | ||
VictoryArea, | ||
VictoryAxis, | ||
VictoryChart, | ||
VictoryVoronoiContainer, | ||
} from 'victory'; | ||
|
||
import theme from '../chart-theme'; | ||
import Container from '../Container'; | ||
import Tooltip from '../Tooltip'; | ||
|
||
import { type CategorySpendingGraphData } from './category-spending-spreadsheet'; | ||
import { Area } from './common'; | ||
|
||
type CategorySpendingGraphProps = { | ||
start: string; | ||
end: string; | ||
graphData: CategorySpendingGraphData; | ||
}; | ||
function CategorySpendingGraph({ | ||
start, | ||
end, | ||
graphData, | ||
}: CategorySpendingGraphProps) { | ||
return ( | ||
<Container> | ||
{(width, height, portalHost) => ( | ||
<VictoryChart | ||
scale={{ x: 'time', y: 'linear' }} | ||
theme={theme} | ||
domainPadding={{ x: 0, y: 10 }} | ||
width={width} | ||
height={height} | ||
containerComponent={<VictoryVoronoiContainer voronoiDimension="x" />} | ||
> | ||
<Area start={start} end={end} /> | ||
<VictoryArea | ||
data={graphData} | ||
labelComponent={<Tooltip portalHost={portalHost} />} | ||
labels={item => item.premadeLabel} | ||
style={{ | ||
data: { | ||
clipPath: 'url(#positive)', | ||
fill: 'url(#positive-gradient)', | ||
}, | ||
}} | ||
/> | ||
<VictoryArea | ||
data={graphData} | ||
style={{ | ||
data: { | ||
clipPath: 'url(#negative)', | ||
fill: 'url(#negative-gradient)', | ||
stroke: theme.colors.red, | ||
strokeLinejoin: 'round', | ||
}, | ||
}} | ||
/> | ||
<VictoryAxis | ||
style={{ ticks: { stroke: 'red' } }} | ||
// eslint-disable-next-line rulesdir/typography | ||
tickFormat={x => d.format(x, "MMM ''yy")} | ||
tickValues={graphData.map(item => item.x)} | ||
tickCount={Math.max(1, Math.min(5, graphData.length))} | ||
offsetY={50} | ||
orientation="bottom" | ||
/> | ||
<VictoryAxis dependentAxis crossAxis={false} /> | ||
</VictoryChart> | ||
)} | ||
</Container> | ||
); | ||
} | ||
|
||
export default CategorySpendingGraph; |
Oops, something went wrong.