master @ 27 LINES
[ HISTORY ] [ UP ]
┌─ TS ───────────────────────────────────────────────────────────────────────┐
│ // Inline series as block glyphs: ▁▂▄▇▅▂ - all within ON_GRID. │
│ export interface Props { │
│ values: number[]; │
│ /** Max glyphs; longer series bucket-average down. */ │
│ width?: number; │
│ } │
│ │
│ const BLOCKS = ["▁", "▂", "▃", "▄", "▅", "▆", "▇", "█"]; │
│ │
│ export function buildSparkline(values: number[], width = values.length): string { │
│ if (!values.length || width < 1) return ""; │
│ │
│ const n = Math.min(width, values.length); │
│ const buckets = Array.from({ length: n }, (_, i) => { │
│ const start = Math.floor((i * values.length) / n); │
│ const end = Math.max(start + 1, Math.floor(((i + 1) * values.length) / n)); │
│ const slice = values.slice(start, end); │
│ return slice.reduce((a, b) => a + b, 0) / slice.length; │
│ }); │
│ │
│ const min = Math.min(...buckets); │
│ const span = Math.max(...buckets) - min; │
│ return buckets │
│ .map((v) => BLOCKS[span === 0 ? 3 : Math.min(7, Math.floor(((v - min) / span) │
│ * 8))]) │
│ .join(""); │
│ } │
└────────────────────────────────────────────────────────────────────────────────────┘
┌─ TS ─────────────────────────────────┐
│ // Inline series as block glyphs: ▁▂▄▇▅▂ - a │
│ ll within ON_GRID. │
│ export interface Props { │
│ values: number[]; │
│ /** Max glyphs; longer series bucket-avera │
│ ge down. */ │
│ width?: number; │
│ } │
│ │
│ const BLOCKS = ["▁", "▂", "▃", "▄", "▅", "▆" │
│ , "▇", "█"]; │
│ │
│ export function buildSparkline(values: numbe │
│ r[], width = values.length): string { │
│ if (!values.length || width < 1) return "" │
│ ; │
│ │
│ const n = Math.min(width, values.length); │
│ const buckets = Array.from({ length: n }, │
│ (_, i) => { │
│ const start = Math.floor((i * values.len │
│ gth) / n); │
│ const end = Math.max(start + 1, Math.flo │
│ or(((i + 1) * values.length) / n)); │
│ const slice = values.slice(start, end); │
│ return slice.reduce((a, b) => a + b, 0) │
│ / slice.length; │
│ }); │
│ │
│ const min = Math.min(...buckets); │
│ const span = Math.max(...buckets) - min; │
│ return buckets │
│ .map((v) => BLOCKS[span === 0 ? 3 : Math │
│ .min(7, Math.floor(((v - min) / span) * 8))] │
│ ) │
│ .join(""); │
│ } │
└──────────────────────────────────────────────┘
──────────────────────────────────────────────────────────────────────────────────────
OOKNET
────────────────────────────────────────────────
OOKNET