06-15-2025, 08:02 AM
Hello everyone!
I created a finance tracker program. Nothing original, as there are thousands of similar programs out there, but I thought I would give it a shot.
Please find the code below.
PS : Try the pie chart function after having added a few items
I created a finance tracker program. Nothing original, as there are thousands of similar programs out there, but I thought I would give it a shot.
Please find the code below.
Code: (Select All)
Screen _NewImage(120 * 8, 40 * 16, 256) ' Approximate dimensions
Type Transaction
id As Integer
amount As Double
category As String * 20
description As String * 55
ttype As String * 10 ' "Income" or "Expense"
tdate As String * 10 ' MM/DD/YYYY format
End Type
Const BLACK = 0, BLUE = 1, GREEN = 2, CYAN = 3, RED = 4, MAGENTA = 5, BROWN = 6
Const WHITE = 7, GRAY = 8, LIGHTBLUE = 9, LIGHTGREEN = 10, LIGHTCYAN = 11
Const LIGHTRED = 12, LIGHTMAGENTA = 13, YELLOW = 14, BRIGHTWHITE = 15
Dim Shared transactions(1000) As Transaction: Dim Shared transactionCount As Integer
Dim Shared balance As Double: Dim Shared categories(20) As String
InitializeCategories
LoadData
Do
DisplayMenu
choice$ = Input$(1)
Select Case choice$
Case "1"
AddTransaction "Income"
Case "2"
AddTransaction "Expense"
Case "3"
ViewTransactions
Case "4"
MonthlyReport
Case "5"
SearchTransactions
Case "6"
CategorySummary
Case "7"
DeleteTransaction
Case "8"
Cls
ExportToCSV
Case "9"
DisplayGraphicsMenu
Case "0"
SaveData
Cls
Print ""
Color LIGHTGREEN: Print " Thank you for using Delsus' Finance Tracker!"
Color WHITE: Print " Your data has been saved."
End
End Select
Loop
Sub InitializeCategories
categories(1) = "Food"
categories(2) = "Transportation"
categories(3) = "Housing"
categories(4) = "Utilities"
categories(5) = "Entertainment"
categories(6) = "Healthcare"
categories(7) = "Shopping"
categories(8) = "Education"
categories(9) = "Insurance"
categories(10) = "Savings"
categories(11) = "Salary"
categories(12) = "Baby stuff"
categories(13) = "Other bills"
categories(14) = "Other"
End Sub
Sub LoadData
If _FileExists("finances.dat") Then
Open "finances.dat" For Input As #1
Input #1, transactionCount
Input #1, balance
For i = 1 To transactionCount
Input #1, transactions(i).id
Input #1, transactions(i).amount
Input #1, transactions(i).category
Input #1, transactions(i).description
Input #1, transactions(i).ttype
Input #1, transactions(i).tdate
Next i
Close #1
Else
transactionCount = 0
balance = 0
End If
End Sub
Sub SaveData
Open "finances.dat" For Output As #1
Print #1, transactionCount
Print #1, balance
For i = 1 To transactionCount
Print #1, transactions(i).id
Print #1, transactions(i).amount
Print #1, transactions(i).category
Print #1, transactions(i).description
Print #1, transactions(i).ttype
Print #1, transactions(i).tdate
Next i
Close #1
End Sub
Sub DisplayMenu
Cls
' Header with gradient-like effect
Color BRIGHTWHITE, BLUE
Print "+---------------------------------------------------------------------------------------------------------------------+"
Print "| DELSUS' FINANCE TRACKER |"
Print "+---------------------------------------------------------------------------------------------------------------------+"
Color WHITE, BLACK
Print
' Balance display with color coding
If balance >= 0 Then
Color LIGHTGREEN: Print " Current Balance: î"; balance
Else
Color LIGHTRED: Print " Current Balance: î"; balance; " (DEFICIT)"
End If
Color GRAY
Print String$(119, "-")
Print
' Menu options with colors
Color YELLOW: Print " TRANSACTIONS:"
Color WHITE
Print " 1. Add Income"
Print " 2. Add Expense"
Print " 3. View All Transactions"
Print
Color YELLOW: Print " REPORTS & ANALYSIS:"
Color WHITE
Print " 4. View Monthly Report"
Print " 5. Search Transactions"
Print " 6. Category Summary"
Print
Color YELLOW: Print " UTILITIES:"
Color WHITE
Print " 7. Delete Transaction"
Print " 8. Export to CSV"
Color LIGHTCYAN: Print " 9. Graphics & Charts"
Print
Color LIGHTRED: Print " 0. Exit"
Print
Color BRIGHTWHITE: Print " Enter your choice (0-9): ";
Color WHITE
End Sub
Sub DisplayGraphicsMenu
Cls
Color BRIGHTWHITE, MAGENTA
Print "+---------------------------------------------------------------------------------------------------------------------+"
Print "| GRAPHICS & VISUAL REPORTS |"
Print "+---------------------------------------------------------------------------------------------------------------------+"
Color WHITE, BLACK
Print
Color LIGHTCYAN: Print " VISUAL REPORTS:"
Color WHITE
Print " 1. Category Pie Chart"
Print " 2. Monthly Spending Bar Chart"
Print " 3. Income vs Expenses Graph"
Print " 4. Balance Trend (Text-based)"
Print
Color YELLOW: Print " 0. Return to Main Menu"
Print
Color BRIGHTWHITE: Print " Enter your choice (0-4): ";
Color WHITE
choice$ = Input$(1)
Select Case choice$
Case "1"
DrawCategoryPieChart
Case "2"
DrawMonthlyBarChart
Case "3"
DrawIncomeVsExpenses
Case "4"
ShowBalanceTrend
Case "0"
End Select
End Sub
Sub AddTransaction (ttype As String)
Cls
If ttype = "Income" Then
Color LIGHTGREEN, BLACK
Else
Color LIGHTRED, BLACK
End If
Print " Adding New "; ttype
Color GRAY: Print String$(119, "-")
Color WHITE
Print
transactionCount = transactionCount + 1
transactions(transactionCount).id = transactionCount
transactions(transactionCount).ttype = ttype
Do
Color YELLOW: Print "Enter date (DD/MM/YYYY, DD.MM.YYYY, or DDMMYYYY) or press ENTER for today's date: ";
Color WHITE
Input dateInput$
If dateInput$ = "" Then
' Use current date if you press ENTER and convert to DD/MM/YYYY
transactions(transactionCount).tdate = ConvertToStandardFormat$(GetCurrentDate$)
Exit Do
Else
' Normalize and validate the entered date
normalizedDate$ = NormalizeDate$(dateInput$)
If normalizedDate$ <> "" Then
transactions(transactionCount).tdate = normalizedDate$
Exit Do
Else
Color LIGHTRED: Print "Invalid date format! Please use DD/MM/YYYY, DD.MM.YYYY, or DDMMYYYY format."
Print
End If
End If
Loop
' Get amount
Color YELLOW: Print "Enter amount: î";
Color WHITE
Input transactions(transactionCount).amount
' Display categories with colors
Print
Color LIGHTCYAN: Print "Available Categories:"
Color WHITE
For i = 1 To 14
Color YELLOW: Print Using "##"; i;
Color WHITE: Print ". "; categories(i)
Next i
Print
Color YELLOW: Print "Select category (1-11): ";
Color WHITE
Input catChoice
If catChoice >= 1 And catChoice <= 14 Then
transactions(transactionCount).category = categories(catChoice)
Else
transactions(transactionCount).category = "Other"
End If
' Get description
Color YELLOW: Print "Enter description: ";
Color WHITE
Input transactions(transactionCount).description
' Update balance
If ttype = "Income" Then
balance = balance + transactions(transactionCount).amount
Else
balance = balance - transactions(transactionCount).amount
End If
SaveData
Color LIGHTGREEN: Print " Transaction added successfully!"
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
End Sub
' Function to normalize different date formats to DD/MM/YYYY
Function NormalizeDate$ (inputDate As String)
NormalizeDate$ = ""
dateStr$ = RTrim$(LTrim$(inputDate))
' Handle DDMMYYYY format (8 digits)
If Len(dateStr$) = 8 And IsNumeric(dateStr$) Then
day$ = Left$(dateStr$, 2)
month$ = Mid$(dateStr$, 3, 2)
year$ = Right$(dateStr$, 4)
normalizedDate$ = day$ + "/" + month$ + "/" + year$
' Handle DD/MM/YYYY or DD.MM.YYYY format (10 characters)
ElseIf Len(dateStr$) = 10 Then
separator$ = Mid$(dateStr$, 3, 1)
If separator$ = "/" Or separator$ = "." Then
If Mid$(dateStr$, 6, 1) = separator$ Then
day$ = Left$(dateStr$, 2)
month$ = Mid$(dateStr$, 4, 2)
year$ = Right$(dateStr$, 4)
normalizedDate$ = day$ + "/" + month$ + "/" + year$
End If
End If
End If
' Validate the normalized date
If normalizedDate$ <> "" And ValidateDate(normalizedDate$) Then
NormalizeDate$ = normalizedDate$
End If
End Function
' Function to validate date format (DD/MM/YYYY)
Function ValidateDate (dateStr As String)
ValidateDate = 0 ' Default to invalid
' Check if string has correct length (10 characters)
If Len(dateStr) <> 10 Then Exit Function
' Check if slashes are in correct positions
If Mid$(dateStr, 3, 1) <> "/" Or Mid$(dateStr, 6, 1) <> "/" Then Exit Function
' Extract day, month, and year (DD/MM/YYYY format)
day$ = Left$(dateStr, 2)
month$ = Mid$(dateStr, 4, 2)
year$ = Right$(dateStr, 4)
' Check if all parts are numeric
If Not IsNumeric(day$) Or Not IsNumeric(month$) Or Not IsNumeric(year$) Then Exit Function
' Convert to numbers for range checking
dayNum = Val(day$)
monthNum = Val(month$)
yearNum = Val(year$)
' Validate ranges
If monthNum < 1 Or monthNum > 12 Then Exit Function
If dayNum < 1 Or dayNum > 31 Then Exit Function
If yearNum < 1900 Or yearNum > 2100 Then Exit Function
' Basic day validation (doesn't account for leap years or month-specific days)
If monthNum = 2 And dayNum > 29 Then Exit Function
If (monthNum = 4 Or monthNum = 6 Or monthNum = 9 Or monthNum = 11) And dayNum > 30 Then Exit Function
ValidateDate = -1 ' Valid date
End Function
' Helper function to check if a string is numeric
Function IsNumeric (testStr As String)
IsNumeric = -1 ' Default to true
For i = 1 To Len(testStr)
If Asc(Mid$(testStr, i, 1)) < 48 Or Asc(Mid$(testStr, i, 1)) > 57 Then
IsNumeric = 0
Exit Function
End If
Next i
End Function
' Function to convert MM/DD/YYYY (from Date$) to DD/MM/YYYY
Function ConvertToStandardFormat$ (usDate As String)
' Date$ returns MM/DD/YYYY, we need DD/MM/YYYY
If Len(usDate) = 10 And Mid$(usDate, 3, 1) = "/" And Mid$(usDate, 6, 1) = "/" Then
month$ = Left$(usDate, 2)
day$ = Mid$(usDate, 4, 2)
year$ = Right$(usDate, 4)
ConvertToStandardFormat$ = day$ + "/" + month$ + "/" + year$
Else
ConvertToStandardFormat$ = usDate
End If
End Function
' Extract day from DD/MM/YYYY format
Function ExtractDay (dateStr As String)
ExtractDay = Val(Left$(dateStr, 2))
End Function
' Extract month from DD/MM/YYYY format
Function ExtractMonth (dateStr As String)
ExtractMonth = Val(Mid$(dateStr, 4, 2))
End Function
' Extract year from DD/MM/YYYY format
Function ExtractYear (dateStr As String)
ExtractYear = Val(Right$(dateStr, 4))
End Function
' Get current date in MM/DD/YYYY format
Function GetCurrentDate$
GetCurrentDate$ = Date$
End Function
Sub ViewTransactions
Cls
Color BRIGHTWHITE, BLUE: Print "ALL TRANSACTIONS": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
If transactionCount = 0 Then
Color LIGHTRED: Print "No transactions found."
Color GRAY: Print "Press any key to continue..."
a$ = Input$(1)
Exit Sub
End If
Dim itemsPerPage As Integer
Dim currentPage As Integer
Dim totalPages As Integer
Dim startIndex As Integer
Dim endIndex As Integer
itemsPerPage = 25
currentPage = 1
totalPages = Int((transactionCount - 1) / itemsPerPage) + 1
Do
Cls
Color BRIGHTWHITE, BLUE: Print "ALL TRANSACTIONS - Page "; currentPage; " of "; totalPages: Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
startIndex = (currentPage - 1) * itemsPerPage + 1
endIndex = currentPage * itemsPerPage
If endIndex > transactionCount Then endIndex = transactionCount
' Display header with colors
Color YELLOW: Print " ID Date Type Amount Category Description"
Color GRAY: Print String$(119, "-")
' Display transactions with alternating colors
For i = startIndex To endIndex
If i Mod 2 = 0 Then Color WHITE Else Color GRAY
Print Using "### "; transactions(i).id;
Print transactions(i).tdate; " ";
' Color code by transaction type
If transactions(i).ttype = "Income " Then
Color LIGHTGREEN
Else
Color LIGHTRED
End If
Print Left$(transactions(i).ttype + Space$(8), 8); " ";
Print Using "E####.## "; transactions(i).amount;
If i Mod 2 = 0 Then Color WHITE Else Color GRAY
Print Left$(transactions(i).category + Space$(15), 15); " ";
Print transactions(i).description
Next i
Color GRAY: Print String$(119, "-")
Color LIGHTCYAN: Print " Showing transactions "; startIndex; " to "; endIndex; " of "; transactionCount
Print
If totalPages > 1 Then
Color YELLOW: Print " Navigation: [N]ext page, [P]revious page, [Q]uit to menu"
Else
Color YELLOW: Print " Press [Q] to return to menu"
End If
Color WHITE
Do
choice$ = UCase$(Input$(1))
Select Case choice$
Case "N"
If currentPage < totalPages Then
currentPage = currentPage + 1
Exit Do
End If
Case "P"
If currentPage > 1 Then
currentPage = currentPage - 1
Exit Do
End If
Case "Q"
Exit Sub
End Select
Loop
Loop
End Sub
Sub MonthlyReport
Cls
Print ""
Color BRIGHTWHITE, BLUE: Print "MONTHLY REPORT": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
Color YELLOW
Input "Enter month (MM): ", targetMonth$
Input "Enter year (YYYY): ", targetYear$
Dim totalIncome As Double, totalExpenses As Double
Dim monthCount As Integer
Cls
Color LIGHTCYAN: Print "REPORT FOR "; targetMonth$; "/"; targetYear$
Color GRAY: Print String$(119, "-")
Print
For i = 1 To transactionCount
' Extract month and year from DD/MM/YYYY format
transMonth = ExtractMonth(transactions(i).tdate)
transYear = ExtractYear(transactions(i).tdate)
If transMonth = Val(targetMonth$) And transYear = Val(targetYear$) Then
monthCount = monthCount + 1
If RTrim$(transactions(i).ttype) = "Income" Then
totalIncome = totalIncome + transactions(i).amount
Else
totalExpenses = totalExpenses + transactions(i).amount
End If
End If
Next i
Color LIGHTGREEN: Print " Total Income: î"; totalIncome
Color LIGHTRED: Print " Total Expenses: î"; totalExpenses
If totalIncome - totalExpenses >= 0 Then
Color LIGHTGREEN: Print " Net Amount: î"; totalIncome - totalExpenses
Else
Color LIGHTRED: Print " Net Amount: î"; totalIncome - totalExpenses
End If
Color WHITE: Print " Transactions: "; monthCount
Print
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
End Sub
Sub SearchTransactions
Cls
Color BRIGHTWHITE, BLUE: Print " SEARCH TRANSACTIONS": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
Color LIGHTCYAN
Print " 1. Search by Description"
Print " 2. Search by Category"
Print " 3. Search by Amount Range"
Print
Color YELLOW: Input " Choose search type (1-3): ", searchType
Cls
Color BRIGHTWHITE: Print " SEARCH RESULTS"
Color GRAY: Print String$(119, "-")
Print
Select Case searchType
Case 1
Color YELLOW: Input "Enter description to search: ", searchTerm$
Color YELLOW: Print " ID Date Type Amount Category Description"
Color GRAY: Print String$(119, "-")
For i = 1 To transactionCount
If InStr(UCase$(transactions(i).description), UCase$(searchTerm$)) > 0 Then
Color WHITE: Print Using "### "; transactions(i).id;
Print transactions(i).tdate; " ";
If transactions(i).ttype = "Income" Then Color LIGHTGREEN Else Color LIGHTRED
Print Left$(transactions(i).ttype + Space$(8), 8); " ";
Print Using "î####.## "; transactions(i).amount;
Color WHITE: Print Left$(transactions(i).category + Space$(15), 15); " ";
Print transactions(i).description
End If
Next i
Case 2
Color YELLOW: Input "Enter category to search: ", searchTerm$
Color YELLOW: Print " ID Date Type Amount Category Description"
Color GRAY: Print String$(119, "-")
For i = 1 To transactionCount
If InStr(UCase$(transactions(i).category), UCase$(searchTerm$)) > 0 Then
Color WHITE: Print Using "### "; transactions(i).id;
Print transactions(i).tdate; " ";
If transactions(i).ttype = "Income" Then Color LIGHTGREEN Else Color LIGHTRED
Print Left$(transactions(i).ttype + Space$(8), 8); " ";
Print Using "î####.## "; transactions(i).amount;
Color WHITE: Print Left$(transactions(i).category + Space$(15), 15); " ";
Print transactions(i).description
End If
Next i
Case 3
Color YELLOW
Input "Enter minimum amount: î", minAmount
Input "Enter maximum amount: î", maxAmount
Color YELLOW: Print " ID Date Type Amount Category Description"
Color GRAY: Print String$(119, "-")
For i = 1 To transactionCount
If transactions(i).amount >= minAmount And transactions(i).amount <= maxAmount Then
Color WHITE: Print Using "### "; transactions(i).id;
Print transactions(i).tdate; " ";
If transactions(i).ttype = "Income" Then Color LIGHTGREEN Else Color LIGHTRED
Print Left$(transactions(i).ttype + Space$(8), 8); " ";
Print Using "î####.## "; transactions(i).amount;
Color WHITE: Print Left$(transactions(i).category + Space$(15), 15); " ";
Print transactions(i).description
End If
Next i
End Select
Print
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
End Sub
Sub CategorySummary
Cls
Color BRIGHTWHITE, BLUE: Print "SPENDING BY CATEGORY": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
Dim categoryTotals(14) As Double
For i = 1 To transactionCount
If transactions(i).ttype = "Expense " Then
For j = 1 To 14
If transactions(i).category = categories(j) Then
categoryTotals(j) = categoryTotals(j) + transactions(i).amount
Exit For
End If
Next j
End If
Next i
Color YELLOW: Print "Category Amount Percentage"
Color GRAY: Print String$(119, "-")
Dim totalExpenses As Double
For i = 1 To 14
totalExpenses = totalExpenses + categoryTotals(i)
Next i
For i = 1 To 14
If categoryTotals(i) > 0 Then
percentage = (categoryTotals(i) / totalExpenses) * 100
Color WHITE: Print Left$(categories(i) + Space$(15), 15); " ";
Color LIGHTRED: Print Using "î####.## "; categoryTotals(i);
Color LIGHTCYAN: Print Using "##.#%"; percentage
End If
Next i
Print
Color YELLOW: Print " Total Expenses: î"; totalExpenses
Print
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
End Sub
Sub DeleteTransaction
Cls
Color LIGHTRED, BLACK: Print "DELETE TRANSACTION": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
If transactionCount = 0 Then
Color LIGHTRED: Print " No transactions to delete."
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
Exit Sub
End If
Color YELLOW: Input " Enter transaction ID to delete: ", deleteID
For i = 1 To transactionCount
If transactions(i).id = deleteID Then
If transactions(i).ttype = "Income" Then
balance = balance - transactions(i).amount
Else
balance = balance + transactions(i).amount
End If
For j = i To transactionCount - 1
transactions(j) = transactions(j + 1)
Next j
transactionCount = transactionCount - 1
SaveData
Color LIGHTGREEN: Print " Transaction deleted successfully!"
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
Exit Sub
End If
Next i
Color LIGHTRED: Print " Transaction not found!"
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
End Sub
Sub ExportToCSV
Open "transactions.csv" For Output As #1
Print #1, "ID,Date,Type,Amount,Category,Description"
For i = 1 To transactionCount
Print #1, transactions(i).id; ",";
Print #1, transactions(i).tdate; ",";
Print #1, transactions(i).ttype; ",";
Print #1, transactions(i).amount; ",";
Print #1, transactions(i).category; ",";
Print #1, transactions(i).description
Next i
Close #1
Color LIGHTGREEN: Print " Data exported to transactions.csv successfully!"
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
End Sub
Sub DrawCategoryPieChart
Cls
Color BRIGHTWHITE, MAGENTA: Print "CATEGORY PIE CHART": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
Dim categoryTotals(14) As Double
Dim totalExpenses As Double
' Calculate category totals
For i = 1 To transactionCount
If RTrim$(transactions(i).ttype) = "Expense" Then
For j = 1 To 14
If RTrim$(transactions(i).category) = RTrim$(categories(j)) Then
categoryTotals(j) = categoryTotals(j) + transactions(i).amount
Exit For
End If
Next j
End If
Next i
For i = 1 To 14
totalExpenses = totalExpenses + categoryTotals(i)
Next i
If totalExpenses = 0 Then
Color LIGHTRED: Print " No expense data available for pie chart."
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
Exit Sub
End If
' Switch to graphics mode for the pie chart
Screen _NewImage(800, 600, 32)
_ScreenMove 200, 100
' Set up variables for the pie chart
Dim centerX As Integer, centerY As Integer, radius As Integer
Dim startAngle As Single, currentAngle As Single, sliceAngle As Single
Dim colors(14) As _Unsigned Long
centerX = 300
centerY = 250
radius = 150
startAngle = 0
' Define colors for each category
colors(1) = _RGB(255, 100, 100) ' Food - Light Red
colors(2) = _RGB(100, 150, 255) ' Transportation - Light Blue
colors(3) = _RGB(100, 255, 100) ' Housing - Light Green
colors(4) = _RGB(255, 255, 100) ' Utilities - Yellow
colors(5) = _RGB(255, 100, 255) ' Entertainment - Magenta
colors(6) = _RGB(100, 255, 255) ' Healthcare - Cyan
colors(7) = _RGB(200, 150, 100) ' Shopping - Brown
colors(8) = _RGB(100, 100, 255) ' Education - Blue
colors(9) = _RGB(150, 255, 150) ' Insurance - Light Green
colors(10) = _RGB(150, 200, 255) ' Savings - Light Blue
colors(11) = _RGB32(0, 255, 0) ' Salary - Green
colors(12) = _RGB(200, 100, 200)
colors(13) = _RGB(150, 150, 150) ' gray
colors(14) = _RGB(255, 159, 64) ' orange
Cls
_PrintString (250, 30), "EXPENSE BREAKDOWN BY CATEGORY"
_PrintString (300, 50), "Total: î" + Str$(totalExpenses)
' Draw the pie chart
currentAngle = startAngle
For i = 1 To 14
If categoryTotals(i) > 0 Then
' Calculate slice angle (360 degrees = 2 * Pi radians)
sliceAngle = (categoryTotals(i) / totalExpenses) * 2 * 3.14159
' Draw the slice
DrawPieSlice centerX, centerY, radius, currentAngle, currentAngle + sliceAngle, colors(i)
' Draw label at the middle of the slice
Dim labelAngle As Single, labelX As Integer, labelY As Integer
labelAngle = currentAngle + sliceAngle / 2
labelX = centerX + Int((radius + 20) * Cos(labelAngle))
labelY = centerY + Int((radius + 20) * Sin(labelAngle))
' Only show label if slice is big enough
If sliceAngle > 0.2 Then
Dim perc As Single
perc = (categoryTotals(i) / totalExpenses) * 100
If perc >= 1 Then
_PrintString (labelX - 30, labelY), RTrim$(categories(i))
End If
End If
currentAngle = currentAngle + sliceAngle
End If
Next i
' Draw legend on the right side
Dim legendY As Integer
legendY = 100
_PrintString (500, 80), "LEGEND:"
For i = 1 To 14
If categoryTotals(i) > 0 Then
' Draw colored square
Line (500, legendY)-(515, legendY + 10), colors(i), BF
' Draw category name and amount
Dim perc2 As Single
perc2 = (categoryTotals(i) / totalExpenses) * 100
_PrintString (520, legendY), RTrim$(categories(i)) + ": î" + Str$(categoryTotals(i)) + " (" + Str$(Int(perc2)) + "%)"
legendY = legendY + 20
End If
Next i
_PrintString (200, 550), "Press any key to return to menu..."
Sleep
Do: Loop While InKey$ = ""
' Return to text mode
Screen _NewImage(120, 40, 0)
End Sub
Sub DrawPieSlice (centerX As Integer, centerY As Integer, radius As Integer, startAngle As Single, endAngle As Single, fillColor As _Unsigned Long)
' Draw filled pie slice
Dim x As Integer, y As Integer
Dim angle As Single, step_size As Single
step_size = 0.05 ' Smaller step for smoother circle
' Fill the slice by drawing lines from center to circumference
angle = startAngle
Do While angle <= endAngle
x = centerX + Int(radius * Cos(angle))
y = centerY + Int(radius * Sin(angle))
Line (centerX, centerY)-(x, y), fillColor
angle = angle + step_size
Loop
' Draw the final line to complete the slice
x = centerX + Int(radius * Cos(endAngle))
y = centerY + Int(radius * Sin(endAngle))
Line (centerX, centerY)-(x, y), fillColor
' Draw outline
Circle (centerX, centerY), radius, _RGB(255, 255, 255), startAngle, endAngle
Line (centerX, centerY)-(centerX + Int(radius * Cos(startAngle)), centerY + Int(radius * Sin(startAngle))), _RGB(255, 255, 255)
Line (centerX, centerY)-(centerX + Int(radius * Cos(endAngle)), centerY + Int(radius * Sin(endAngle))), _RGB(255, 255, 255)
End Sub
Sub DrawMonthlyBarChart
Cls
Color BRIGHTWHITE, MAGENTA: Print "MONTHLY SPENDING CHART": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
Dim monthlyTotals(14) As Double
Dim monthNames(12) As String
monthNames(1) = "Jan": monthNames(2) = "Feb": monthNames(3) = "Mar"
monthNames(4) = "Apr": monthNames(5) = "May": monthNames(6) = "Jun"
monthNames(7) = "Jul": monthNames(8) = "Aug": monthNames(9) = "Sep"
monthNames(10) = "Oct": monthNames(11) = "Nov": monthNames(12) = "Dec"
' Calculate monthly totals for expenses using DD/MM/YYYY format
For i = 1 To transactionCount
If RTrim$(transactions(i).ttype) = "Expense" Then
month = ExtractMonth(transactions(i).tdate)
If month >= 1 And month <= 12 Then
monthlyTotals(month) = monthlyTotals(month) + transactions(i).amount
End If
End If
Next i
' Find maximum for scaling
maxAmount = 0
For i = 1 To 14
If monthlyTotals(i) > maxAmount Then maxAmount = monthlyTotals(i)
Next i
If maxAmount = 0 Then
Color LIGHTRED: Print "No expense data available for chart."
Color GRAY: Print "Press any key to continue..."
a$ = Input$(1)
Exit Sub
End If
Print " MONTHLY EXPENSES BAR CHART"
Print
Color GRAY: Print String$(80, "-")
Print
For i = 1 To 14
If monthlyTotals(i) > 0 Then
barLength = Int((monthlyTotals(i) / maxAmount) * 40) ' Scale to 40 chars max
Color LIGHTCYAN: Print monthNames(i); " |";
Color LIGHTGREEN: Print String$(barLength, Chr$(219));
Color WHITE: Print " î"; monthlyTotals(i)
End If
Next i
Color GRAY: Print String$(80, "-")
Print
Color GRAY: Print "Press any key to continue..."
a$ = Input$(1)
End Sub
Sub DrawIncomeVsExpenses
Cls
Color BRIGHTWHITE, MAGENTA: Print "INCOME vs EXPENSES COMPARISON": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
Dim totalIncome As Double, totalExpenses As Double
For i = 1 To transactionCount
If transactions(i).ttype = "Income " Then
totalIncome = totalIncome + transactions(i).amount
Else
totalExpenses = totalExpenses + transactions(i).amount
End If
Next i
If totalIncome = 0 And totalExpenses = 0 Then
Color LIGHTRED: Print "No data available for comparison."
Color GRAY: Print "Press any key to continue..."
a$ = Input$(1)
Exit Sub
End If
maxAmount = totalIncome
If totalExpenses > maxAmount Then maxAmount = totalExpenses
Print " INCOME vs EXPENSES COMPARISON"
Print
Color GRAY: Print String$(80, "-")
Print
' Income bar
incomeBarLength = Int((totalIncome / maxAmount) * 50)
Color LIGHTGREEN: Print "Income |";
Print String$(incomeBarLength, Chr$(219));
Color WHITE: Print " î"; totalIncome
Print
' Expenses bar
expenseBarLength = Int((totalExpenses / maxAmount) * 50)
Color LIGHTRED: Print "Expenses |";
Print String$(expenseBarLength, Chr$(219));
Color WHITE: Print " î"; totalExpenses
Print
Color GRAY: Print String$(80, "-")
' Net amount
netAmount = totalIncome - totalExpenses
If netAmount >= 0 Then
Color LIGHTGREEN: Print "Net Profit: î"; netAmount
Else
Color LIGHTRED: Print "Net Loss: î"; Abs(netAmount)
End If
Print
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
End Sub
Sub ShowBalanceTrend
Cls
Color BRIGHTWHITE, MAGENTA: Print "BALANCE TREND": Color WHITE, BLACK
Color GRAY: Print String$(119, "-")
Print
If transactionCount = 0 Then
Color LIGHTRED: Print " No transaction data available."
Color GRAY: Print " Press any key to continue..."
a$ = Input$(1)
Exit Sub
End If
Dim runningBalance As Double
runningBalance = 0
Print " BALANCE HISTORY (Last 10 Transactions)"
Print
Color GRAY: Print String$(80, "-")
Print
' Show last 10 transactions and running balance
startIndex = transactionCount - 9
If startIndex < 1 Then startIndex = 1
For i = startIndex To transactionCount
If transactions(i).ttype = "Income" Then
runningBalance = runningBalance + transactions(i).amount
Color LIGHTGREEN: Print transactions(i).tdate; " +î"; transactions(i).amount;
Else
runningBalance = runningBalance - transactions(i).amount
Color LIGHTRED: Print transactions(i).tdate; " -î"; transactions(i).amount;
End If
Color WHITE: Print " | Balance: ";
If runningBalance >= 0 Then
Color LIGHTGREEN: Print "î"; runningBalance
Else
Color LIGHTRED: Print "î"; runningBalance
End If
Next i
Color GRAY: Print String$(80, "-")
Color YELLOW: Print "Current Balance: ";
If balance >= 0 Then
Color LIGHTGREEN: Print "î"; balance
Else
Color LIGHTRED: Print "î"; balance
End If
Print
Color GRAY: Print "Press any key to continue..."
a$ = Input$(1)
End Sub
PS : Try the pie chart function after having added a few items

