From a54354864e15a991a6d4a6ac31de1fa949f63a10 Mon Sep 17 00:00:00 2001 From: "Cal.W" <20716204+calw20@users.noreply.github.com> Date: Mon, 29 Jun 2020 10:15:41 +1000 Subject: [PATCH] Generate the transaction flow over time graphs --- bankView.py | 82 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/bankView.py b/bankView.py index 01c06b8..5507a8b 100644 --- a/bankView.py +++ b/bankView.py @@ -1,9 +1,13 @@ #BankView - Calculate and Build Banking Stats #Cal.W 2020 -import csv, statistics +import csv, statistics, os from datetime import datetime +import matplotlib +import matplotlib.pyplot as plt +import matplotlib.dates as mdates + TRANSACTION_TYPES = ['Other', "Tap and Pay", "Card", "Deposit"] #transDetails, transDate, transType, transAcc @@ -116,6 +120,28 @@ def saveTransactionData(transactions, fileName="transData.csv"): return + +#Load and if found update the transaction db +def loadUpdateTransactionData(dbFileName="transData.csv", importFileName="import.csv", mvImportFileName=None): + if mvImportFileName is None: mvImportFileName = './imported_'+datetime.now().strftime("%Y-%m-%d")+'.csv' + + trans = list() + if(os.path.isfile(dbFileName)): + trans = loadTransactionData(dbFileName) + + if (os.path.isfile(importFileName)): + trans = importTransactionData(importFileName, transactions=trans) + os.rename(importFileName, mvImportFileName) + saveTransactionData(trans) + + if len(trans) < 1: + raise ImportError("No transactions to use!") + + return trans + + + + def calculateStats(transactions, yearRangeVal=None, monthRangeVal=None, otherFelid=None): yearRange = lambda x: True if isinstance(yearRangeVal, type(yearRange)): yearRange = yearRangeVal @@ -145,7 +171,42 @@ def calculateStats(transactions, yearRangeVal=None, monthRangeVal=None, otherFel "lowestBalance" : float(min(periodTransactions, key=lambda x: x[4])[4]) if len(periodTransactions) > 0 else float(0), } - return stats + return stats, periodTransactions + + +def drawTransactionGraphs(trans): + stats, statTrans = calculateStats(trans) + + fig, ax = plt.subplots(2, sharex=True) + #ax[0].xaxis.set_major_formatter(mdates.DateFormatter('%m/%Y')) + #ax[0].xaxis.set_major_locator(mdates.DayLocator(interval=60)) + ax[0].fmt_xdata = mdates.DateFormatter('%Y-%m-%d') + ax[0].plot([x[1].date() for x in statTrans], [x[4] for x in statTrans]) + ax[0].set(title='Transaction History') + ax[0].tick_params( + axis='x', # changes apply to the x-axis + which='both', # both major and minor ticks are affected + bottom=False, # ticks along the bottom edge are off + top=False, # ticks along the top edge are off + labelbottom=False # labels along the bottom edge are off + ) + ax[0].grid() + + + #ax[1].xaxis.set_major_formatter(mdates.DateFormatter('%m/%Y')) + #ax[1].xaxis.set_major_locator(mdates.DayLocator()) + ax[1].fmt_xdata = mdates.DateFormatter('%Y-%m-%d') + ax[1].plot([x[1].date() for x in statTrans], [x[2] for x in statTrans]) + ax[1].set(title='Transaction Amounts Over Time') + ax[1].grid() + + fig.tight_layout() + fig.autofmt_xdate() + + fig.text(0.5, 0.05, 'Date (Month/Year)', ha='center') + fig.text(0.015, 0.5, 'Transaction Amount ($)', va='center', rotation='vertical') + #fig.savefig("test.png") + plt.show() def foo(transactions): @@ -157,18 +218,9 @@ def foo(transactions): for year in years: pass -import pprint - -#[TODO] Net balance and averages seem funny / dont line up with excell :/ if __name__ == "__main__": - trans = loadTransactionData() - y = lambda x: x[-2] == TRANSACTION_TYPES[1] - stats = calculateStats(trans, 2020, 1, y) - pprint.pprint(stats, indent=4) - - #calculateStats(trans) - - - - #[print(x) for x in trans if x[-2] == TRANSACTION_TYPES[-1]] \ No newline at end of file + trans = loadUpdateTransactionData() + + drawTransactionGraphs(trans) + \ No newline at end of file