Generate the transaction flow over time graphs
This commit is contained in:
		
							
								
								
									
										78
									
								
								bankView.py
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								bankView.py
									
									
									
									
									
								
							@@ -1,9 +1,13 @@
 | 
				
			|||||||
#BankView - Calculate and Build Banking Stats
 | 
					#BankView - Calculate and Build Banking Stats
 | 
				
			||||||
#Cal.W 2020
 | 
					#Cal.W 2020
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import csv, statistics
 | 
					import csv, statistics, os
 | 
				
			||||||
from datetime import datetime
 | 
					from datetime import datetime
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import matplotlib
 | 
				
			||||||
 | 
					import matplotlib.pyplot as plt
 | 
				
			||||||
 | 
					import matplotlib.dates as mdates
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TRANSACTION_TYPES = ['Other', "Tap and Pay", "Card", "Deposit"]
 | 
					TRANSACTION_TYPES = ['Other', "Tap and Pay", "Card", "Deposit"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#transDetails, transDate, transType, transAcc
 | 
					#transDetails, transDate, transType, transAcc
 | 
				
			||||||
@@ -116,6 +120,28 @@ def saveTransactionData(transactions, fileName="transData.csv"):
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
    return
 | 
					    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):
 | 
					def calculateStats(transactions, yearRangeVal=None, monthRangeVal=None, otherFelid=None):
 | 
				
			||||||
    yearRange = lambda x: True
 | 
					    yearRange = lambda x: True
 | 
				
			||||||
    if isinstance(yearRangeVal, type(yearRange)): yearRange = yearRangeVal
 | 
					    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),
 | 
					        "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):
 | 
					def foo(transactions):
 | 
				
			||||||
@@ -157,18 +218,9 @@ def foo(transactions):
 | 
				
			|||||||
    for year in years:
 | 
					    for year in years:
 | 
				
			||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import pprint
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
#[TODO] Net balance and averages seem funny / dont line up with excell :/
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    trans = loadTransactionData()
 | 
					    trans = loadUpdateTransactionData()
 | 
				
			||||||
    y = lambda x: x[-2] == TRANSACTION_TYPES[1]
 | 
					 | 
				
			||||||
    stats = calculateStats(trans, 2020, 1, y) 
 | 
					 | 
				
			||||||
    pprint.pprint(stats, indent=4)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    #calculateStats(trans)
 | 
					    drawTransactionGraphs(trans)
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    
 | 
					 | 
				
			||||||
    #[print(x) for x in trans if x[-2] == TRANSACTION_TYPES[-1]]
 | 
					 | 
				
			||||||
		Reference in New Issue
	
	Block a user