-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatagrabber.js
More file actions
78 lines (65 loc) · 3.1 KB
/
datagrabber.js
File metadata and controls
78 lines (65 loc) · 3.1 KB
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
// Function to fetch and process CSV data
async function fetchCSVData() {
try {
const response = await fetch('./c_income_zhvi_mortgage_comprehensive_affordability.csv');
const csvData = await response.text();
const parsedData = parseCSVData(csvData);
updateHTML(parsedData);
} catch (error) {
console.error('Error fetching CSV data:', error);
}
}
function parseCSVData(csv) {
const lines = csv.split('\n').filter(line => line.trim() !== '');
const headers = lines[0].split(',');
const rows = lines.slice(1);
return rows.map(row => {
const values = row.split(',');
const obj = {};
headers.forEach((header, index) => {
if (header.trim() === 'Date') {
obj[header.trim()] = values[index]?.trim();
} else {
obj[header.trim()] = values[index + 1]?.trim();
}
});
return obj;
});
}
function updateHTML(data) {
const finalRow = data.length - 1;
const secondToLastRow = data.length - 2;
const yearAgoRow = data.length - 13;
// Calculate the values from the data
const currentGap = parseFloat(data[finalRow]['ZHVI_Affordable_Difference']);
const previousMonthGap = parseFloat(data[secondToLastRow]['ZHVI_Affordable_Difference']);
const yearAgoGap = parseFloat(data[yearAgoRow]['ZHVI_Affordable_Difference']);
const currentMortgageRate = parseFloat(data[finalRow]['Mortgage_Rate']);
const monthlyChange = currentGap - previousMonthGap;
const yearlyChange = currentGap - yearAgoGap;
// Extract and format the dates from the adjusted rows
const currentDate = new Date(data[secondToLastRow]['Date']);
const lastMonthDate = new Date(data[yearAgoRow]['Date']);
// Format the month names and year
const currentMonthName = currentDate.toLocaleString('default', { month: 'long' });
const lastMonthName = lastMonthDate.toLocaleString('default', { month: 'long' });
const currentYear = currentDate.getFullYear();
// Update the months directly with the shifted logic
document.getElementById('current-date').textContent = `${currentMonthName} ${currentYear}`;
document.getElementById('last-month').textContent = `${lastMonthName}`;
// Update the values using our calculated numbers
document.getElementById('current-gap').textContent = `$${Math.round(currentGap).toLocaleString()}`;
const monthChangeElement = document.getElementById('change-month');
monthChangeElement.textContent = `${monthlyChange >= 0 ? '▲' : '▼'} $${Math.abs(Math.round(monthlyChange)).toLocaleString()}`;
monthChangeElement.style.color = monthlyChange >= 0 ? 'red' : 'green';
const yearChangeElement = document.getElementById('change-year');
yearChangeElement.textContent = `${yearlyChange >= 0 ? '▲' : '▼'} $${Math.abs(Math.round(yearlyChange)).toLocaleString()}`;
yearChangeElement.style.color = yearlyChange >= 0 ? 'red' : 'green';
// Update mortgage rate
const mortgageRateElement = document.getElementById('mortgage-rate');
if (mortgageRateElement && !isNaN(currentMortgageRate)) {
mortgageRateElement.textContent = `${currentMortgageRate.toFixed(1)}%`;
}
}
// Initialize when DOM is ready
document.addEventListener('DOMContentLoaded', fetchCSVData);