|
1 | 1 | // This file is used with expenses.html file
|
2 | 2 |
|
3 | 3 | /* Find the input elements & the button on the document page */
|
4 |
| -let expenseNameInput = document.querySelector('#expense-name'); |
5 |
| -let expenseAmountInput = document.querySelector('#expense-amount'); |
6 |
| -let addExpenseButton = document.querySelector('#add-expense-btnID'); |
| 4 | +let expenseNameInput = document.querySelector("#expense-name"); |
| 5 | +let expenseAmountInput = document.querySelector("#expense-amount"); |
| 6 | +let addExpenseButton = document.querySelector("#add-expense-btnID"); |
7 | 7 |
|
8 | 8 | /* Find the chart canvas element */
|
9 |
| -let chartCanvas = document.querySelector('#expenses-doughnut-chart'); |
| 9 | +let chartCanvas = document.querySelector("#expenses-doughnut-chart"); |
10 | 10 |
|
11 | 11 | // create a drawing context that does the drawing on the canvas
|
12 |
| -let context = chartCanvas.getContext('2d'); |
| 12 | +let context = chartCanvas.getContext("2d"); |
13 | 13 |
|
14 |
| -// TODO create chart object |
| 14 | +// TODO create chart object |
15 | 15 | let expenseChart = new Chart(context, {
|
16 |
| - type: 'doughnut', // type of chart |
17 |
| - data: { |
18 |
| - // what does each item in the chart represent |
19 |
| - labels: [], |
20 |
| - datasets: [{ |
21 |
| - // label name for the chart |
22 |
| - label: 'Expense chart', |
23 |
| - data: [], |
24 |
| - backgroundColor: [], |
25 |
| - }], |
26 |
| - }, |
27 |
| - options: {} |
28 |
| -}) |
29 |
| - |
30 |
| -// TODO (optional) replace with colors of your choice. The array can have as many or as few colors as you like |
31 |
| -let chartColors = ['tomato', 'orange', 'dodgerblue', 'mediumseagreen', 'slateblue', 'violet']; |
| 16 | + type: "doughnut", // type of chart |
| 17 | + data: { |
| 18 | + // what does each item in the chart represent |
| 19 | + labels: [], |
| 20 | + datasets: [ |
| 21 | + { |
| 22 | + // label name for the chart |
| 23 | + label: "Expense chart", |
| 24 | + data: [], |
| 25 | + backgroundColor: [], |
| 26 | + }, |
| 27 | + ], |
| 28 | + }, |
| 29 | + options: {}, |
| 30 | +}); |
| 31 | + |
| 32 | +// TODO (optional) replace with colors of your choice. |
| 33 | +// The array can have as many or as few colors as you like |
| 34 | +let chartColors = [ |
| 35 | + "tomato", |
| 36 | + "orange", |
| 37 | + "dodgerblue", |
| 38 | + "mediumseagreen", |
| 39 | + "slateblue", |
| 40 | + "violet", |
| 41 | +]; |
32 | 42 |
|
33 | 43 | /**
|
34 |
| - Adds the expense type, amount, & color to 'labels', 'data', & 'backgroundColor' arrays |
35 |
| - in the chart object. |
36 |
| - @param {string} name Expense type, for e.g., coffee, rent, food, etc. |
37 |
| - @param {number} amount Money spent for each expense item |
| 44 | + Adds the expense type, amount, & color to 'labels', 'data', & 'backgroundColor' arrays in the chart object. |
| 45 | + @param {string} name Expense type, for e.g., coffee, rent, food, etc. |
| 46 | + @param {number} amount Money spent for each expense item |
38 | 47 | */
|
39 | 48 | function addExpenseToChart(name, amount) {
|
40 |
| - // TODO add expense to chart |
41 |
| - expenseChart.data.labels.push(name); |
42 |
| - expenseChart.data.datasets[0].data.push(amount); |
43 |
| - |
44 |
| - // initial colorCount will begin at 0 |
45 |
| - let colorCount = expenseChart.data.datasets[0].backgroundColor.length; |
| 49 | + // TODO add expense to chart |
| 50 | + expenseChart.data.labels.push(name); |
| 51 | + expenseChart.data.datasets[0].data.push(amount); |
46 | 52 |
|
47 |
| - // this will always return a valid color from the chartColors array |
48 |
| - let bgColor = chartColors[colorCount % chartColors.length]; |
| 53 | + // initial colorCount will begin at 0 |
| 54 | + let colorCount = expenseChart.data.datasets[0].backgroundColor.length; |
49 | 55 |
|
50 |
| - // push the colors onto the chart backgroundColor array |
51 |
| - expenseChart.data.datasets[0].backgroundColor.push(bgColor); |
| 56 | + // this will always return a valid color from the chartColors array |
| 57 | + let bgColor = chartColors[colorCount % chartColors.length]; |
52 | 58 |
|
53 |
| - expenseChart.update(); |
54 |
| -} |
55 |
| - |
56 |
| - |
57 |
| -addExpenseButton.addEventListener('click', function () { |
58 |
| - // array of errors for validation |
59 |
| - let errors = []; |
60 |
| - |
61 |
| - // get the input values for the expense name and the amount |
62 |
| - let expenseName = expenseNameInput.value.trim(); |
63 |
| - let expenseAmount = expenseAmountInput.value; |
64 |
| - |
65 |
| - // Validate both fields are filled in |
66 |
| - if (expenseName.length == 0) { |
67 |
| - errors.push('Enter a type of expense'); |
68 |
| - expenseNameInput.value = ''; |
69 |
| - } |
| 59 | + // push the colors onto the chart backgroundColor array |
| 60 | + expenseChart.data.datasets[0].backgroundColor.push(bgColor); |
70 | 61 |
|
71 |
| - // Validate that the amount is a positive number |
72 |
| - if (expenseAmount.length == 0 || expenseAmountInput < 0) { |
73 |
| - errors.push('Enter a positive amount for the expense'); |
74 |
| - } |
75 |
| - |
76 |
| - // If any errors exist, alert and return - do not procede to add to chart |
77 |
| - if (errors.length > 0) { |
78 |
| - alert(errors.join('\n')); |
79 |
| - return; // stop processing |
80 |
| - } |
| 62 | + expenseChart.update(); |
| 63 | +} |
81 | 64 |
|
82 |
| - // TODO call function to update chart |
83 |
| - addExpenseToChart(expenseName, expenseAmount); |
84 |
| - |
85 |
| - // Clear inputs, ready for next expense name and amount. |
86 |
| - expenseNameInput.value = ''; |
87 |
| - expenseAmountInput.value = ''; |
88 |
| -}) |
89 |
| - |
90 |
| - |
91 |
| -// TODO add event listener to click the Add Expense button when the enter key is pressed |
92 |
| -window.addEventListener('keyup', function(event) { |
93 |
| - // Check if enter key is pressed, which has keyCode 13 |
94 |
| - if (event.key === 'Enter') { |
95 |
| - // And if the focus is on one of the inputs, then click the addExpenseButton |
96 |
| - let inputElements = [ expenseNameInput, expenseAmountInput, addExpenseButton ] |
97 |
| - |
98 |
| - if (inputElements.includes(document.activeElement)) { |
99 |
| - addExpenseButton.click() // click button |
100 |
| - expenseNameInput.focus() // move focus to expense name input, for convenience of entering new expense |
101 |
| - } |
| 65 | +addExpenseButton.addEventListener("click", function () { |
| 66 | + // array of errors for validation |
| 67 | + let errors = []; |
| 68 | + |
| 69 | + // get the input values for the expense name and the amount |
| 70 | + let expenseName = expenseNameInput.value.trim(); |
| 71 | + let expenseAmount = expenseAmountInput.value; |
| 72 | + |
| 73 | + // Validate both fields are filled in |
| 74 | + if (expenseName.length == 0) { |
| 75 | + errors.push("Enter a type of expense"); |
| 76 | + expenseNameInput.value = ""; |
| 77 | + } |
| 78 | + |
| 79 | + // Validate that the amount is a positive number |
| 80 | + if (expenseAmount.length == 0 || expenseAmountInput < 0) { |
| 81 | + errors.push("Enter a positive amount for the expense"); |
| 82 | + } |
| 83 | + |
| 84 | + // If any errors exist, alert and return - do not procede to add to chart |
| 85 | + if (errors.length > 0) { |
| 86 | + alert(errors.join("\n")); |
| 87 | + return; // stop processing |
| 88 | + } |
| 89 | + |
| 90 | + // TODO call function to update chart |
| 91 | + addExpenseToChart(expenseName, expenseAmount); |
| 92 | + |
| 93 | + // Clear inputs, ready for next expense name and amount. |
| 94 | + expenseNameInput.value = ""; |
| 95 | + expenseAmountInput.value = ""; |
| 96 | +}); |
| 97 | + |
| 98 | +// TODO add event listener to click the Add Expense button when the enter key is pressed |
| 99 | +window.addEventListener("keyup", function (event) { |
| 100 | + // Check if enter key is pressed, which has keyCode 13 |
| 101 | + if (event.key === "Enter") { |
| 102 | + // And if the focus is on one of the inputs, then click the addExpenseButton |
| 103 | + let inputElements = [ |
| 104 | + expenseNameInput, |
| 105 | + expenseAmountInput, |
| 106 | + addExpenseButton, |
| 107 | + ]; |
| 108 | + |
| 109 | + if (inputElements.includes(document.activeElement)) { |
| 110 | + addExpenseButton.click(); // click button |
| 111 | + expenseNameInput.focus(); // move focus to expense name input, for convenience of entering new expense |
102 | 112 | }
|
103 |
| -}) |
104 |
| -
|
| 113 | + } |
| 114 | +}); |
0 commit comments