-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
249 lines (218 loc) · 8.53 KB
/
Copy pathscript.js
File metadata and controls
249 lines (218 loc) · 8.53 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
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
/**
* SORA Rates Dashboard — Premium Edition
* Live data from a public Google Sheet (housingloansg.com source).
*/
const DATA_URL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSetwT6jWgq4H6LPeIp3hB_hgo4HdF1P1Q7pu0JYlaUqXnzxbonDFpA_Myndbv3ewsbFOyGhW6kxLro/pub?output=csv";
let chartInstance = null;
let allData = [];
let tableExpanded = false;
// ── Helpers ──────────────────────────
const $ = id => document.getElementById(id);
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
function setStatus(msg, type) {
$("statusText").textContent = msg;
$("status").className = "status" + (type ? " " + type : "");
}
function fmtDate(d) {
return d.getDate().toString().padStart(2, "0") + " " + MONTHS[d.getMonth()] + " " + d.getFullYear();
}
function fmtRate(v) { return v.toFixed(4); }
function changeText(curr, prev) {
const diff = curr - prev;
const sign = diff >= 0 ? "+" : "";
const bps = (diff * 100).toFixed(1);
return { text: `${sign}${bps} bps`, cls: diff >= 0 ? "down" : "up" };
}
// ── Parse CSV ───────────────────────
function parseCsv(csv) {
const lines = csv.trim().split("\n");
const data = [];
for (let i = 1; i < lines.length; i++) {
const cols = lines[i].split(",");
if (cols.length < 4) continue;
const parts = cols[0].trim().split("/");
const day = parseInt(parts[0], 10);
const month = parseInt(parts[1], 10) - 1;
const year = parseInt(parts[2], 10);
const dateObj = new Date(year, month, day);
const sora = parseFloat(cols[1]);
const sora1m = parseFloat(cols[2]);
const sora3m = parseFloat(cols[3]);
if (isNaN(sora)) continue;
data.push({ dateObj, sora, sora1m, sora3m });
}
return data;
}
// ── Hero Rate Cards ─────────────────
function renderHero(data) {
const latest = data[data.length - 1];
const prev = data.length > 1 ? data[data.length - 2] : latest;
// Animate numbers counting up
animateValue($("heroSora"), latest.sora);
animateValue($("hero1m"), latest.sora1m);
animateValue($("hero3m"), latest.sora3m);
// Change indicators
const cs = changeText(latest.sora, prev.sora);
const c1 = changeText(latest.sora1m, prev.sora1m);
const c3 = changeText(latest.sora3m, prev.sora3m);
setChange($("heroSoraChange"), cs);
setChange($("hero1mChange"), c1);
setChange($("hero3mChange"), c3);
}
function setChange(el, { text, cls }) {
el.textContent = text;
el.className = "rc-change " + cls;
}
function animateValue(el, target) {
const duration = 800;
const start = performance.now();
const from = 0;
function step(now) {
const t = Math.min((now - start) / duration, 1);
const ease = 1 - Math.pow(1 - t, 3); // ease-out cubic
const val = from + (target - from) * ease;
el.textContent = val.toFixed(4) + "%";
if (t < 1) requestAnimationFrame(step);
}
requestAnimationFrame(step);
}
// ── Chart ───────────────────────────
function renderChart(data) {
const ctx = $("chart").getContext("2d");
if (chartInstance) chartInstance.destroy();
const common = {
borderWidth: 2,
pointRadius: 0,
pointHoverRadius: 4,
pointHitRadius: 12,
tension: 0.4,
};
// Gradient fills
const soraGrad = ctx.createLinearGradient(0, 0, 0, 260);
soraGrad.addColorStop(0, "rgba(102,153,255,0.15)");
soraGrad.addColorStop(1, "rgba(102,153,255,0)");
const m1Grad = ctx.createLinearGradient(0, 0, 0, 260);
m1Grad.addColorStop(0, "rgba(45,212,168,0.10)");
m1Grad.addColorStop(1, "rgba(45,212,168,0)");
Chart.defaults.color = "#717789";
Chart.defaults.font.family = "'Inter', sans-serif";
Chart.defaults.font.size = 10;
chartInstance = new Chart(ctx, {
type: "line",
data: {
datasets: [
{
...common, label: "SORA",
data: data.map(d => ({ x: d.dateObj, y: d.sora })),
borderColor: "#6699ff",
backgroundColor: soraGrad,
fill: true
},
{
...common, label: "1M Comp.",
data: data.map(d => ({ x: d.dateObj, y: d.sora1m })),
borderColor: "#2dd4a8",
backgroundColor: m1Grad,
fill: true
},
{
...common, label: "3M Comp.",
data: data.map(d => ({ x: d.dateObj, y: d.sora3m })),
borderColor: "#a78bfa",
fill: false
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
interaction: { mode: "index", intersect: false },
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: "rgba(12,14,20,0.95)",
borderColor: "rgba(255,255,255,0.08)",
borderWidth: 1,
titleColor: "#fff",
titleFont: { weight: "600", size: 11 },
bodyColor: "#c4c8d4",
bodyFont: { size: 11 },
padding: 10,
boxPadding: 3,
cornerRadius: 8,
usePointStyle: true,
callbacks: {
title: items => {
if (!items.length) return "";
const d = new Date(items[0].parsed.x);
return fmtDate(d);
},
label: ctx => ` ${ctx.dataset.label}: ${ctx.parsed.y.toFixed(4)}%`
}
}
},
scales: {
x: {
type: "time",
time: { unit: "week", displayFormats: { week: "dd MMM" } },
border: { display: false },
grid: { display: false },
ticks: { maxTicksLimit: 7, padding: 4, font: { size: 10 } }
},
y: {
border: { display: false },
grid: { color: "rgba(255,255,255,0.03)", drawTicks: false },
ticks: {
padding: 6,
font: { size: 10 },
callback: v => v.toFixed(1) + "%"
}
}
}
}
});
}
// ── Table ───────────────────────────
function renderTable(data) {
const tbody = $("tableBody");
tbody.innerHTML = "";
const sorted = [...data].reverse();
for (const row of sorted) {
const tr = document.createElement("tr");
tr.innerHTML =
`<td>${fmtDate(row.dateObj)}</td>` +
`<td>${fmtRate(row.sora)}</td>` +
`<td>${fmtRate(row.sora1m)}</td>` +
`<td>${fmtRate(row.sora3m)}</td>`;
tbody.appendChild(tr);
}
// Start collapsed
$("tablePanel").classList.add("table-collapsed");
tableExpanded = false;
$("toggleBtn").textContent = `Show all (${sorted.length})`;
}
// ── Toggle table ────────────────────
$("toggleBtn").addEventListener("click", () => {
tableExpanded = !tableExpanded;
$("tablePanel").classList.toggle("table-collapsed", !tableExpanded);
$("toggleBtn").textContent = tableExpanded ? "Show less" : `Show all (${allData.length})`;
});
// ── Main ────────────────────────────
async function main() {
try {
setStatus("Fetching rates…", "");
const res = await fetch(DATA_URL);
if (!res.ok) throw new Error("HTTP " + res.status);
const csv = await res.text();
allData = parseCsv(csv);
if (allData.length === 0) throw new Error("No data rows found");
setStatus(`Live · ${allData.length} days · updated ${fmtDate(allData[allData.length - 1].dateObj)}`, "live");
renderHero(allData);
renderChart(allData);
renderTable(allData);
} catch (err) {
console.error(err);
setStatus("Failed — " + err.message, "error");
}
}
document.addEventListener("DOMContentLoaded", main);