blind-monkeys / static /btc-chart.js
deanpowers's picture
feat: commit pending site changes (sideways/downward/financials/upward pages, widgets, templates)
1db0bad
Raw History Blame Contribute Delete
4.65 kB
// BTC spot price — 7-day continuous line chart (no candles).
// Renders into any element with [data-btc-chart] and writes the 7-day change
// into any element with [data-btc-change]. Self-contained, no dependencies.
(function () {
'use strict';
var state = { data: null, pct: null, first: null, last: null };
function num(v) { return Number(v); }
function usd(v) { return '$' + num(v).toLocaleString(undefined, { maximumFractionDigits: 0 }); }
function pctFmt(v) { return (v >= 0 ? '+' : '') + v.toFixed(2) + '%'; }
function timeLabel(t) {
var d = new Date(t);
return d.toLocaleDateString(undefined, { month: 'short', day: 'numeric' }) + ' ' +
d.toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' });
}
function buildSvg(w, h) {
var data = state.data;
var padL = 64, padR = 18, padT = 16, padB = 30;
var iw = w - padL - padR, ih = h - padT - padB;
var closes = data.map(function (d) { return num(d.close); });
var min = Math.min.apply(null, closes), max = Math.max.apply(null, closes);
if (max - min === 0) { max += 1; min -= 1; }
var n = data.length;
function x(i) { return padL + (n <= 1 ? 0 : (i / (n - 1)) * iw); }
function y(v) { return padT + (1 - (v - min) / (max - min)) * ih; }
var s = '<svg xmlns="http://www.w3.org/2000/svg" width="' + w + '" height="' + h + '" viewBox="0 0 ' + w + ' ' + h + '" font-family="-apple-system, BlinkMacSystemFont, sans-serif">';
s += '<defs><linearGradient id="btcArea" x1="0" y1="0" x2="0" y2="1">';
s += '<stop offset="0%" stop-color="#f0b90b" stop-opacity="0.35"/>';
s += '<stop offset="100%" stop-color="#f0b90b" stop-opacity="0"/>';
s += '</linearGradient></defs>';
var ticks = 5;
for (var t = 0; t < ticks; t++) {
var val = min + (max - min) * t / (ticks - 1);
var yy = y(val);
s += '<line x1="' + padL + '" y1="' + yy.toFixed(1) + '" x2="' + (w - padR) + '" y2="' + yy.toFixed(1) + '" stroke="#30363d" stroke-width="1"/>';
s += '<text x="' + (padL - 8) + '" y="' + (yy + 4).toFixed(1) + '" fill="#8b949e" font-size="14" text-anchor="end">' + usd(val) + '</text>';
}
var xs = [0, Math.floor((n - 1) / 2), n - 1];
xs.forEach(function (i) {
var xx = x(i);
var anchor = i === 0 ? 'start' : (i === n - 1 ? 'end' : 'middle');
s += '<text x="' + xx.toFixed(1) + '" y="' + (h - 8) + '" fill="#8b949e" font-size="14" text-anchor="' + anchor + '">' + timeLabel(data[i].t) + '</text>';
});
var pts = data.map(function (d, i) { return x(i).toFixed(1) + ',' + y(num(d.close)).toFixed(1); });
var linePath = 'M' + pts.join(' L');
var areaPath = linePath + ' L' + x(n - 1).toFixed(1) + ',' + (padT + ih).toFixed(1) + ' L' + x(0).toFixed(1) + ',' + (padT + ih).toFixed(1) + ' Z';
s += '<path d="' + areaPath + '" fill="url(#btcArea)"/>';
s += '<path d="' + linePath + '" fill="none" stroke="#f0b90b" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>';
s += '</svg>';
return s;
}
function redraw() {
document.querySelectorAll('[data-btc-chart]').forEach(function (c) {
if (!state.data) return;
var w = c.clientWidth || 720;
c.innerHTML = buildSvg(w, 320);
});
}
function init() {
var changeEl = document.querySelector('[data-btc-change]');
fetch('/api/btc-chart/weekly')
.then(function (r) { if (!r.ok) throw new Error('http'); return r.json(); })
.then(function (res) {
var data = (res && res.data) || [];
if (!data || data.length < 2) throw new Error('nodata');
state.data = data;
state.first = num(data[0].close);
state.last = num(data[data.length - 1].close);
state.pct = (state.last - state.first) / state.first * 100;
if (changeEl) {
var cls = state.pct > 0.5 ? 'pos' : (state.pct < -0.5 ? 'neg' : 'mut');
changeEl.innerHTML = '7-Day Change: <span class="' + cls + '">' + pctFmt(state.pct) + '</span> <span class="mut" style="margin-left:8px;">' + usd(state.first) + ' → ' + usd(state.last) + '</span>';
}
redraw();
})
.catch(function () {
document.querySelectorAll('[data-btc-chart]').forEach(function (c) {
c.innerHTML = '<p class="muted" style="text-align:center;padding:20px;">Could not load BTC chart.</p>';
});
});
}
var resizeTimer;
window.addEventListener('resize', function () {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(redraw, 150);
});
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
else init();
})();