الأمثلة
أمثلة عملية، وعروض حيّة.
اختر مثالًا لتفتحه بملء الشاشة: المعاينة الحية وكوده المصدري جنبًا إلى جنب.
اختر مثالًا لتفتحه بملء الشاشة: المعاينة الحية وكوده المصدري جنبًا إلى جنب.
عينات تتدفق من أربعة توزيعات. تُعاد حسابات الفئات مع كل تغيير، ومنحنى جرسي ملائم يتبع المتوسط، وشريط تمرير يحدد عدد الفئات.
import 'nuclo' type Dist = 'normal' | 'uniform' | 'bimodal' | 'skewed' const W = 320 const H = 150 const BASE = H - 14 // the y of the baseline const MAX_BINS = 40 // A seeded generator, so the first frame is always the same. let seed = 41 const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647 const gauss = (mean: number, sd: number) => mean + sd * Math.sqrt(-2 * Math.log(1 - random())) * Math.cos(2 * Math.PI * random()) const draw: Record<Dist, () => number> = { normal: () => gauss(50, 14), uniform: () => random() * 100, bimodal: () => (random() < 0.5 ? gauss(27, 8) : gauss(72, 10)), skewed: () => -Math.log(1 - random()) * 18, } let dist: Dist = 'normal' let bins = 20 let samples: number[] = [] let streaming = false let fit = true let counts: number[] = [] let peak = 4 let mean = 0 let sd = 1 const sum = (list: number[]) => list.reduce((a, b) => a + b, 0) // One pass over the samples gives the counts per bin and the stats. function recount() { counts = Array(bins).fill(0) for (const s of samples) if (s >= 0 && s < 100) counts[Math.floor((s / 100) * bins)]++ peak = Math.max(4, ...counts) mean = sum(samples) / (samples.length || 1) sd = Math.sqrt(sum(samples.map((s) => (s - mean) ** 2)) / (samples.length || 1)) || 1 } function add(count: number) { for (let i = 0; i < count; i++) samples.push(draw[dist]()) if (samples.length > 6000) samples = samples.slice(-6000) recount() } add(500) const scale = () => (BASE - 6) / (peak * 1.15) // The bell a normal distribution with this mean and spread would draw. const curve = () => Array.from({ length: 61 }, (_, i) => { const v = (i / 60) * 100 const n = samples.length * (100 / bins) * (Math.exp(-0.5 * ((v - mean) / sd) ** 2) / (sd * Math.sqrt(2 * Math.PI))) return (i ? 'L' : 'M') + ((v / 100) * W).toFixed(1) + ',' + (BASE - Math.min(n * scale(), BASE)).toFixed(1) }).join(' ') // The bars come from a list of slots, and the slider only // changes how many of them are shown. Every bar reads its count, // so a stream of samples grows them, and a CSS transition on // x, width, y and height smooths both that and a new bin count. const slots = Array.from({ length: MAX_BINS }, (_, i) => ({ i })) // Run a change, recount, and update only the 'hist' scope. const run = (change: () => void) => () => { change(); recount(); update('hist') } const row = { display: 'flex', flexWrap: 'wrap', justifyContent: 'center', gap: '6px' } const pressed = (on: () => boolean) => () => ({ outline: on() ? '2px solid currentColor' : 'none', outlineOffset: '2px' }) export const Histogram = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px', width: '100%', maxWidth: '380px' } }, scope('hist'), svgSvg( { viewBox: '0 0 320 150', role: 'img', 'aria-label': 'Histogram', style: { width: '100%', overflow: 'visible' }, // a timer that adds samples while streaming onMount: () => { const timer = setInterval(() => { if (!streaming) return add(6) update('hist') }, 60) return () => clearInterval(timer) }, }, lineSvg({ x1: 0, x2: W, y1: BASE + 0.5, y2: BASE + 0.5, stroke: '#ddd' }), list(() => slots.slice(0, bins), (s) => rectSvg({ rx: 2, fill: '#ff3f00', 'fill-opacity': 0.85, style: { transition: 'x 0.25s ease, y 0.25s ease, width 0.25s ease, height 0.25s ease' }, x: () => (s.i * W) / bins + 0.75, width: () => W / bins - 1.5, y: () => BASE - counts[s.i] * scale(), height: () => counts[s.i] * scale(), }), ), pathSvg({ fill: 'none', stroke: '#222', 'stroke-width': 2, 'stroke-linejoin': 'round', style: { pointerEvents: 'none' }, opacity: () => (fit && samples.length > 1 ? 1 : 0), d: curve, }), lineSvg({ y1: 4, y2: BASE, stroke: '#888', 'stroke-dasharray': '3 3', style: { pointerEvents: 'none' }, x1: () => (mean / 100) * W, x2: () => (mean / 100) * W, }), ...[0, 25, 50, 75, 100].map((v) => textSvg( { x: (v / 100) * W, y: H - 2, 'font-size': 11, 'fill-opacity': 0.6, 'text-anchor': v === 0 ? 'start' : v === 100 ? 'end' : 'middle' }, String(v), ), ), ), div( { style: { fontFamily: 'ui-monospace, monospace', fontSize: '12px', textAlign: 'center', minHeight: '36px', opacity: '0.7' } }, () => 'n ' + samples.length + ' · mean ' + mean.toFixed(1) + ' · σ ' + sd.toFixed(1) + ' · tallest bin ' + peak, ), div( { style: row }, ...(['normal', 'uniform', 'bimodal', 'skewed'] as const).map((d) => button(d, { 'aria-pressed': () => String(dist === d), style: pressed(() => dist === d), onClick: run(() => { dist = d; samples = []; add(500) }), }), ), ), div( { style: row }, button('+100', { style: { color: '#fff', backgroundColor: '#ff3f00', borderColor: '#ff3f00' }, onClick: run(() => add(100)) }), button('Stream', { 'aria-pressed': () => String(streaming), style: pressed(() => streaming), onClick: () => { streaming = !streaming; update('hist') }, }), button('Normal fit', { 'aria-pressed': () => String(fit), style: pressed(() => fit), onClick: () => { fit = !fit; update('hist') }, }), ), label( { style: { display: 'flex', alignItems: 'center', gap: '8px' } }, span('Bins'), input({ type: 'range', min: '5', max: String(MAX_BINS), step: '1', value: () => String(bins), onInput: (e) => { bins = Number(e.currentTarget.value); recount(); update('hist') }, }), span(() => String(bins)), ), )