-
Notifications
You must be signed in to change notification settings - Fork 53
/
reports.js
336 lines (318 loc) · 11.2 KB
/
reports.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SUMMARY: this file handles all of the functionality on the various Reporting pages of Tendie Tracker.
// Loads *budget report data* from Flask/Jinja that is passed from the request
function loadBudgetData(budgetData) {
budgets = JSON.parse(budgetData);
loadBudgetCharts(budgets);
loadBudgetTables(budgets);
}
// Loads *monthly spending* data from Flask/Jinja that is passed from the request
function loadMonthlySpendingData(monthlyData) {
monthlySpending = JSON.parse(monthlyData);
loadMonthlySpendingChart(monthlySpending.chart);
loadMonthlySpendingTable(monthlySpending.table);
}
// Loads *spending trends* data from Flask/Jinja that is passed from the request
function loadTrendsData(trendsData_chart, trendsData_table) {
spendingTrends_chart = JSON.parse(trendsData_chart);
spendingTrends_table = JSON.parse(trendsData_table);
loadSpendingTrendsChart(spendingTrends_chart);
loadSpendingTrendsTable(spendingTrends_table);
}
// Loads *payers* data from Flask/Jinja that is passed from the request
function loadPayersData(payersData) {
payersSpending = JSON.parse(payersData);
loadPayersSpendingChart(payersSpending);
}
function loadBudgetCharts(budgets) {
if (budgets == null) {
return;
}
// Loop through the budgets and build the charts
else {
let chartElement = []
let budgetCharts = []
for (i = 0; i < budgets.length; i++) {
chartElement[i] = document.getElementById('budgetChart.' + (i)).getContext('2d');
budgetCharts[i] = new Chart(chartElement[i], {
type: 'doughnut',
data: {
labels: ['Spent', 'Remaining'],
datasets: [{
label: budgets[i].name,
data: [budgets[i].spent, budgets[i].remaining],
backgroundColor: [
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)'
],
borderColor: [
'rgba(192, 138, 62, 1)',
'rgba(1, 147, 93, 1)'
],
borderWidth: 2
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
labels: {
fontColor: 'black'
}
}
}
});
}
}
}
function loadBudgetTables(budgets) {
if (budgets == null) {
return;
}
// Loop through the budgets and build the charts
else {
for (i = 0; i < budgets.length; i++) {
$('#budgetTable_' + (i + 1)).DataTable({
"order": [[5, "desc"]],
"scrollY": "300px",
"scrollCollapse": true,
"paging": false,
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel'
],
"searching": false
});
}
}
}
function loadMonthlySpendingChart(monthlySpendingChart) {
if (monthlySpendingChart == null) {
return;
}
else {
// Build arrays to hold the months and amounts for chart labels and data
let months = []
let amounts = []
for (i = 0; i < monthlySpendingChart.length; i++) {
months[i] = monthlySpendingChart[i].name;
amounts[i] = monthlySpendingChart[i].amount;
}
// Draw the chart
let chartElement = document.getElementById('monthlyChart').getContext('2d');
let spendingChart = new Chart(chartElement, {
type: 'bar',
data: {
labels: months,
datasets: [{
label: 'Total $ Spent',
data: amounts,
backgroundColor: [
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)',
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)',
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)',
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)',
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)',
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)',
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)'
],
borderColor: [
'rgba(192, 138, 62, 1)',
'rgba(1, 147, 93, 1)',
'rgba(192, 138, 62, 1)',
'rgba(1, 147, 93, 1)',
'rgba(192, 138, 62, 1)',
'rgba(1, 147, 93, 1)',
'rgba(192, 138, 62, 1)',
'rgba(1, 147, 93, 1)',
'rgba(192, 138, 62, 1)',
'rgba(1, 147, 93, 1)',
'rgba(192, 138, 62, 1)',
'rgba(1, 147, 93, 1)'
],
borderWidth: 2
}]
},
options: {
legend: { display: false },
title: {
display: true,
text: 'Total spending per month during the current the year'
},
responsive: true,
maintainAspectRatio: false,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
}
function loadMonthlySpendingTable(monthlySpendingTable) {
if (monthlySpendingTable == null) {
return;
}
else {
$('#monthlyExpenses').DataTable({
"pagingType": "full_numbers",
"order": [[3, "desc"]],
"scrollY": "300px",
"scrollCollapse": true,
"paging": false,
dom: 'Bfrtip',
buttons: [
'copy', 'csv', 'excel'
]
});
}
}
function loadSpendingTrendsChart(spendingTrends_chart) {
if (spendingTrends_chart == null) {
return;
}
else {
// Build dict/array to hold the data
let trendsDataset = []
// Loop through each JSON object that was received from Flask/Jinja and populate a dict that matches ChartJS parameters
for (i = 0; i < spendingTrends_chart.length; i++) {
data = {
label: spendingTrends_chart[i].name,
backgroundColor: "rgba(240, 173, 78, 0.7)",
borderColor: "rgba(2, 184, 117, 1)",
borderWidth: 2,
data: [{
x: spendingTrends_chart[i].totalSpent,
y: spendingTrends_chart[i].totalCount,
r: spendingTrends_chart[i].proportionalAmount
}]
}
// Add the data set to the list
trendsDataset.push(data)
}
// Draw the chart
let chartElement = document.getElementById('spendingChart').getContext('2d');
let spendingChart = new Chart(chartElement, {
type: 'bubble',
data: {
labels: "Spend Categories",
datasets: trendsDataset
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'Total spent, total # of expenses, and % of overall spent per spend category'
},
scales: {
yAxes: [{
scaleLabel: {
display: true,
labelString: "Total # of Expenses"
}
}],
xAxes: [{
scaleLabel: {
display: true,
labelString: "Total $ Spent"
}
}]
}
}
});
}
}
function loadSpendingTrendsTable(spendingTrends_table) {
if (spendingTrends_table == null) {
return;
}
else {
$('#monthlyCategoryExpenses').DataTable({
"pagingType": "full_numbers",
"bSort": false,
"bFilter": false,
"bInfo": false,
"paging": false,
dom: 'Bfrtip',
buttons: ['copy', 'csv', 'excel', 'colvis']
});
}
}
function loadPayersSpendingChart(payersSpending) {
if (payersSpending == null) {
return;
}
else {
// Loop through the payers object and build the labels
payerNames = []
for (i = 0; i < payersSpending.length; i++) {
// If the payer represents less than 1% of overall expenses do not include
if (payersSpending[i].percentAmount < 1) {
continue;
}
else {
payerNames.push(payersSpending[i].name)
}
}
// Loop through the payers object and build the amounts dataset
payerAmounts = []
for (i = 0; i < payersSpending.length; i++) {
// If the payer represents less than 1% of overall expenses do not include
if (payersSpending[i].percentAmount < 1) {
continue;
}
else {
payerAmounts.push(payersSpending[i].amount)
}
}
// Build the chart
chartElement = document.getElementById('payersChart').getContext('2d');
budgetCharts = new Chart(chartElement, {
type: 'pie',
data: {
labels: payerNames,
datasets: [{
data: payerAmounts,
// Note: the color scheme for this is hard-coded with an assumption of 6 total payers including the default 'Self' payer.
// If max payer count changes in the future, the background/border colors will need to as well
backgroundColor: [
'rgba(240, 173, 78, 1)',
'rgba(2, 184, 117, 1)',
'rgba(69, 130, 236)',
'rgba(23, 162, 184)',
'rgba(202, 207, 212)',
'rgba(217, 83, 79)'
],
borderColor: [
'rgba(192, 138, 62, 1)',
'rgba(1, 147, 93, 1)',
'rgba(64, 121, 220)',
'rgba(21, 151, 171)',
'rgba(173, 181, 189)',
'rgba(195, 74, 71)'
],
borderWidth: 2
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
legend: {
labels: {
fontColor: 'black'
}
}
}
});
}
}