|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | + <meta charset="UTF-8"> |
| 5 | + <title>Personal Finance Dashboard</title> |
| 6 | + <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> |
| 7 | + <style> |
| 8 | + body { font-family: Arial, sans-serif; margin: 40px; } |
| 9 | + .container { max-width: 600px; margin: auto; } |
| 10 | + input, button { margin: 5px; } |
| 11 | + </style> |
| 12 | +</head> |
| 13 | +<body> |
| 14 | +<div class="container"> |
| 15 | + <h2>Personal Finance Dashboard</h2> |
| 16 | + <div> |
| 17 | + <label>Set Budget: </label> |
| 18 | + <input type="number" id="budgetInput" /> |
| 19 | + <button onclick="setBudget()">Set</button> |
| 20 | + <span id="budgetDisplay"></span> |
| 21 | + </div> |
| 22 | + <div> |
| 23 | + <label>Add Expense: </label> |
| 24 | + <input type="text" id="expenseName" placeholder="Name" /> |
| 25 | + <input type="number" id="expenseAmount" placeholder="Amount" /> |
| 26 | + <button onclick="addExpense()">Add</button> |
| 27 | + </div> |
| 28 | + <canvas id="expenseChart" width="400" height="200"></canvas> |
| 29 | +</div> |
| 30 | +<script> |
| 31 | +let expenses = []; |
| 32 | +let budget = 0; |
| 33 | +const chartCtx = document.getElementById('expenseChart').getContext('2d'); |
| 34 | +let chart; |
| 35 | + |
| 36 | +function fetchBudget() { |
| 37 | + fetch('/api/budget').then(r => r.json()).then(data => { |
| 38 | + budget = data.budget; |
| 39 | + document.getElementById('budgetDisplay').innerText = `Budget: $${budget}`; |
| 40 | + }); |
| 41 | +} |
| 42 | +function setBudget() { |
| 43 | + const val = Number(document.getElementById('budgetInput').value); |
| 44 | + fetch('/api/budget', { |
| 45 | + method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({budget: val}) |
| 46 | + }).then(fetchBudget); |
| 47 | +} |
| 48 | +function fetchExpenses() { |
| 49 | + fetch('/api/expenses').then(r => r.json()).then(data => { |
| 50 | + expenses = data; |
| 51 | + updateChart(); |
| 52 | + }); |
| 53 | +} |
| 54 | +function addExpense() { |
| 55 | + const name = document.getElementById('expenseName').value; |
| 56 | + const amount = Number(document.getElementById('expenseAmount').value); |
| 57 | + fetch('/api/expenses', { |
| 58 | + method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({name, amount}) |
| 59 | + }).then(fetchExpenses); |
| 60 | +} |
| 61 | +function updateChart() { |
| 62 | + const labels = expenses.map(e => e.name); |
| 63 | + const data = expenses.map(e => e.amount); |
| 64 | + if (chart) chart.destroy(); |
| 65 | + chart = new Chart(chartCtx, { |
| 66 | + type: 'bar', |
| 67 | + data: { |
| 68 | + labels: labels, |
| 69 | + datasets: [{ label: 'Expenses', data: data, backgroundColor: '#4caf50' }] |
| 70 | + }, |
| 71 | + options: { |
| 72 | + plugins: { |
| 73 | + title: { display: true, text: `Budget: $${budget} | Total Spent: $${data.reduce((a,b)=>a+b,0)}` } |
| 74 | + } |
| 75 | + } |
| 76 | + }); |
| 77 | +} |
| 78 | +fetchBudget(); |
| 79 | +fetchExpenses(); |
| 80 | +</script> |
| 81 | +</body> |
| 82 | +</html> |
0 commit comments