/* Lightweight SVG charts — no external dependencies. */ let uid = 0; const BAND_COLORS = { healthy: 'var(--ok)', watch: 'var(--watch)', tight: 'var(--tight)', critical: 'var(--crit)', unknown: 'var(--muted)', }; function esc(value) { return String(value ?? '').replace(/[&<>"']/g, (character) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', }[character])); } function nearest(points, target) { let best = null; for (const point of points) { if (!best || Math.abs(point.t - target) < Math.abs(best.t - target)) best = point; } return best; } export function bandColor(band) { return BAND_COLORS[band] || BAND_COLORS.unknown; } export function lineChart(container, series, options = {}) { if (!container) return; const active = (series || []).filter((item) => item && item.points && item.points.length); if (!active.length) { container.innerHTML = '
No telemetry in this window yet.
'; return; } const width = options.width || 920; const height = options.height || 280; const pad = { left: options.padLeft ?? 48, right: 18, top: 16, bottom: 30 }; const all = active.flatMap((item) => item.points); let t0 = Math.min(...all.map((point) => point.t)); let t1 = Math.max(...all.map((point) => point.t)); if (t1 <= t0) t1 = t0 + 3600000; let max = options.max; if (!max) { const peak = Math.max(...all.map((point) => point.v), 1); max = peak * 1.15; } const plotW = width - pad.left - pad.right; const plotH = height - pad.top - pad.bottom; const x = (t) => pad.left + ((t - t0) / (t1 - t0)) * plotW; const y = (v) => pad.top + plotH - (Math.max(0, Math.min(v, max)) / max) * plotH; const span = t1 - t0; const valueFormat = options.valueFormat || ((value) => `${Math.round(value)}`); const timeFormat = (t) => { const date = new Date(t); if (span <= 2 * 86400000) { return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); } return date.toLocaleDateString([], { month: 'short', day: 'numeric' }); }; const tooltipTime = (t) => { const date = new Date(t); if (span <= 2 * 86400000) { return date.toLocaleString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); } return date.toLocaleDateString([], { weekday: 'short', month: 'short', day: 'numeric' }); }; const parts = []; parts.push(``); for (let index = 0; index <= 4; index += 1) { const value = (max / 4) * index; const yy = y(value); parts.push(``); parts.push(`${esc(valueFormat(value))}`); } for (let index = 0; index <= 4; index += 1) { const t = t0 + ((t1 - t0) * index) / 4; parts.push(`${esc(timeFormat(t))}`); } active.forEach((item) => { if (!item.points.length) return; const points = item.points; const color = item.color || 'var(--accent)'; const d = points.map((point, index) => `${index === 0 ? 'M' : 'L'}${x(point.t).toFixed(2)},${y(point.v).toFixed(2)}`).join(' '); if (item.area) { const gradient = `chart-grad-${++uid}`; const baseY = pad.top + plotH; parts.push(``); parts.push(``); } parts.push(``); if (item.dot) { const last = points[points.length - 1]; parts.push(``); } }); parts.push(``); parts.push(``); parts.push(''); const wrap = document.createElement('div'); wrap.className = 'chart-wrap'; wrap.innerHTML = parts.join('') + ''; container.replaceChildren(wrap); const svg = wrap.querySelector('svg'); const tip = wrap.querySelector('.chart-tip'); const guide = wrap.querySelector('.chart-guide'); svg.addEventListener('pointermove', (event) => { const rect = svg.getBoundingClientRect(); const px = ((event.clientX - rect.left) / rect.width) * width; if (px < pad.left || px > width - pad.right) { tip.hidden = true; guide.setAttribute('opacity', '0'); return; } const t = t0 + ((px - pad.left) / plotW) * (t1 - t0); const rows = active.map((item) => { const point = nearest(item.points, t); return { label: item.label || item.key || '', color: item.color || 'var(--accent)', point }; }).filter((row) => row.point); if (!rows.length) return; const reference = rows[0].point.t; tip.innerHTML = `
${esc(tooltipTime(reference))}
` + rows.map((row) => ( `
${esc(row.label)}${esc(valueFormat(row.point.v))}
` )).join(''); tip.hidden = false; const guideX = x(reference); guide.setAttribute('x1', guideX); guide.setAttribute('x2', guideX); guide.setAttribute('opacity', '1'); const left = (guideX / width) * 100; tip.style.left = `${Math.max(2, Math.min(76, left))}%`; }); svg.addEventListener('pointerleave', () => { tip.hidden = true; guide.setAttribute('opacity', '0'); }); } export function ring(value, options = {}) { const size = options.size || 104; const stroke = options.stroke || 9; const radius = (size - stroke) / 2; const circumference = 2 * Math.PI * radius; const safe = Math.max(0, Math.min(100, Number(value) || 0)); const band = options.band || (safe >= 90 ? 'critical' : safe >= 80 ? 'tight' : safe >= 60 ? 'watch' : 'healthy'); return `
${Math.round(safe)}${esc(options.suffix || '/100')}
`; } export function sparkline(points, color = 'var(--accent)', options = {}) { if (!points || points.length < 2) return ''; const width = options.width || 116; const height = options.height || 34; const pad = 2; let min = Math.min(...points.map((point) => point.v)); let max = Math.max(...points.map((point) => point.v)); if (max - min < 0.01) { max += 1; min -= 1; } const t0 = points[0].t; const t1 = points[points.length - 1].t || t0 + 1; const x = (t) => pad + ((t - t0) / (t1 - t0 || 1)) * (width - pad * 2); const y = (v) => pad + (1 - (v - min) / (max - min)) * (height - pad * 2); const d = points.map((point, index) => `${index === 0 ? 'M' : 'L'}${x(point.t).toFixed(1)},${y(point.v).toFixed(1)}`).join(' '); const area = `${d} L${x(points[points.length - 1].t).toFixed(1)},${height - pad} L${x(points[0].t).toFixed(1)},${height - pad} Z`; return ``; } export function distributionBars(items, options = {}) { const total = items.reduce((sum, item) => sum + item.value, 0) || 1; return `
${items.map((item) => `
`).join('')}
`; }