示例
实用示例,实时演示。
挑一个示例全屏打开,运行效果和源码并排呈现。
挑一个示例全屏打开,运行效果和源码并排呈现。
四十个位置,各自读取对应的 K 线数据。你可以切换时间周期、开关均线、悬停看数值,或者让实时 K 线一直刷下去。
import 'nuclo' type Candle = { o: number; h: number; l: number; c: number; v: number } const W = 320 const H = 196 const PW = 282 // the plot; the price labels sit to its right const PH = 130 const SLOTS = 40 const UP = '#2563eb' const DOWN = '#ff3f00' // Seeded random numbers, so the first frame is always the same. let seed = 5 const random = () => (seed = (seed * 16807) % 2147483647) / 2147483647 const next = (o: number): Candle => { const c = o + (random() - 0.48) * 4 return { o, c, h: Math.max(o, c) + random() * 2, l: Math.min(o, c) - random() * 2, v: 20 + random() * 80, } } const raw: Candle[] = [] for (let i = 0, p = 100; i < 120; i++) { raw.push(next(p)); p = raw[i].c } let agg = 1 // 1, 3 or 5 raw candles per candle let showSma = true let live = true let hover: number | null = null let view: Candle[] = [] let lo = 0 let hi = 1 let vmax = 1 let n = 0 const sum = (list: number[]) => list.reduce((s, v) => s + v, 0) // Group the raw candles from the newest backwards, so the last one // is always complete, then fit the scales to what is visible. function refresh() { view = [] for (let end = raw.length; end > 0 && view.length < SLOTS; end -= agg) { const g = raw.slice(Math.max(0, end - agg), end) view.unshift({ o: g[0].o, c: g[g.length - 1].c, h: Math.max(...g.map((k) => k.h)), l: Math.min(...g.map((k) => k.l)), v: sum(g.map((k) => k.v)), }) } const span = Math.max(...view.map((k) => k.h)) - Math.min(...view.map((k) => k.l)) lo = Math.min(...view.map((k) => k.l)) - span * 0.06 hi = Math.max(...view.map((k) => k.h)) + span * 0.06 vmax = Math.max(...view.map((k) => k.v)) } refresh() const cx = (i: number) => ((i + 0.5) * PW) / view.length const y = (v: number) => 4 + (1 - (v - lo) / (hi - lo)) * (PH - 8) const pick = (i: number) => view[Math.min(i, view.length - 1)] const color = (i: number) => (pick(i).c >= pick(i).o ? UP : DOWN) const last = () => view[view.length - 1] const lastColor = () => (last().c >= last().o ? UP : DOWN) const slotWidth = () => (PW / view.length) * 0.62 // Simple moving average of the last five closes. const sma = () => view .map((_, i) => [cx(i), y(sum(view.slice(i - 4, i + 1).map((k) => k.c)) / 5)]) .filter((_, i) => i >= 4) .map((p, i) => (i ? 'L' : 'M') + p[0].toFixed(1) + ',' + p[1].toFixed(1)) .join(' ') // The live timer moves the newest raw candle; every 8th tick opens a new one. function tick() { if (!live) return const k = raw[raw.length - 1] k.c += (random() - 0.5) * 2.4 k.h = Math.max(k.h, k.c) k.l = Math.min(k.l, k.c) k.v += random() * 8 if (++n % 8 === 0) { raw.push(next(k.c)) raw.shift() } refresh() update('candles') } // Run a change, refit the scales, update only this scope. const set = (change: () => void) => () => { change(); refresh(); update('candles') } const muted = { 'font-family': 'ui-monospace, monospace', 'font-size': 11, fill: '#8a8a8a' } // A fixed number of slots, each reading its own candle. When // fewer candles exist, the extra slots fade out. export const Candles = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '18px', width: '100%', maxWidth: '380px' } }, scope('candles'), svgSvg( { viewBox: '0 0 320 196', role: 'img', 'aria-label': 'Candlestick chart', style: { width: '100%', height: 'auto', overflow: 'visible' }, onMount: () => { const timer = setInterval(tick, 450) return () => clearInterval(timer) }, }, // Price grid ...[0.1, 0.35, 0.6, 0.85].map((f) => gSvg( lineSvg({ x1: 0, x2: PW, stroke: '#ddd', 'stroke-dasharray': '2 4', y1: () => y(lo + (hi - lo) * f) + 0.5, y2: () => y(lo + (hi - lo) * f) + 0.5, }), textSvg(muted, { x: PW + 4, y: () => y(lo + (hi - lo) * f) + 4 }, () => (lo + (hi - lo) * f).toFixed(0)), ), ), ...Array.from({ length: SLOTS }, (_, i) => gSvg( { opacity: () => (i < view.length ? 1 : 0) }, // wick lineSvg({ 'stroke-width': 1.4, stroke: () => color(i), x1: () => cx(i), x2: () => cx(i), y1: () => y(pick(i).h), y2: () => y(pick(i).l), }), // body rectSvg({ rx: 1, fill: () => color(i), x: () => cx(i) - slotWidth() / 2, width: () => slotWidth(), y: () => y(Math.max(pick(i).o, pick(i).c)), height: () => Math.max(1.5, Math.abs(y(pick(i).o) - y(pick(i).c))), }), // volume rectSvg({ 'fill-opacity': 0.35, fill: () => color(i), x: () => cx(i) - slotWidth() / 2, width: () => slotWidth(), y: () => H - 2 - (pick(i).v / vmax) * 38, height: () => (pick(i).v / vmax) * 38, }), ), ), pathSvg({ fill: 'none', stroke: '#ff8a00', 'stroke-width': 1.6, 'stroke-linejoin': 'round', opacity: () => (showSma ? 1 : 0), d: sma, }), // The last price: a dashed line and a tag on the axis. lineSvg({ 'stroke-dasharray': '3 3', x1: 0, x2: PW, stroke: lastColor, y1: () => y(last().c), y2: () => y(last().c), }), rectSvg({ rx: 3, x: PW + 1, width: 37, height: 14, fill: lastColor, y: () => y(last().c) - 7 }), textSvg(muted, { x: PW + 4, style: { fill: '#fff' }, y: () => y(last().c) + 4 }, () => last().c.toFixed(1)), // The hover cursor gSvg( { stroke: '#8a8a8a', 'pointer-events': 'none', opacity: () => (hover === null ? 0 : 1) }, lineSvg({ y1: 0, y2: H, x1: () => cx(hover ?? 0), x2: () => cx(hover ?? 0) }), lineSvg({ x1: 0, x2: PW, y1: () => y(pick(hover ?? 0).c), y2: () => y(pick(hover ?? 0).c) }), ), // A transparent rect on top follows the pointer. rectSvg({ x: 0, y: 0, width: PW, height: H, fill: 'transparent', style: { cursor: 'crosshair', touchAction: 'none' }, onPointerMove: (e) => { const box = e.currentTarget.getBoundingClientRect() const i = Math.floor(((e.clientX - box.left) / box.width) * view.length) hover = Math.max(0, Math.min(view.length - 1, i)) update('candles') }, onPointerLeave: () => { hover = null; update('candles') }, }), ), div( { style: { fontFamily: 'ui-monospace, monospace', fontSize: '12px', color: '#8a8a8a', textAlign: 'center', minHeight: '36px' } }, () => { const k = pick(hover ?? view.length - 1) return 'O ' + k.o.toFixed(1) + ' · H ' + k.h.toFixed(1) + ' · L ' + k.l.toFixed(1) + ' · C ' + k.c.toFixed(1) + ' · ' + (((k.c - k.o) / k.o) * 100).toFixed(2) + '%' }, ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, ...[1, 3, 5].map((m) => button(m + 'm', { 'aria-pressed': () => String(agg === m), style: () => ({ outline: agg === m ? '2px solid currentColor' : 'none', outlineOffset: '2px' }), onClick: set(() => { agg = m; hover = null }), }), ), button('SMA', { 'aria-pressed': () => String(showSma), style: () => ({ outline: showSma ? '2px solid currentColor' : 'none', outlineOffset: '2px' }), onClick: set(() => { showSma = !showSma }), }), button(() => (live ? 'Live' : 'Paused'), { onClick: set(() => { live = !live }) }), ), )