import type { DemoCase } from '../content/cases' /** * 제안가 추이 차트 — 이 화면의 시그니처. * 라운드마다 내려오는 제안가를 영역 차트로 그리고, 기존가·목표가·앵커가를 * 점선 기준선으로 깔아 "지금 제안이 어느 구간에 있는지" 하나만 전달한다. */ export function PriceChart({ demoCase, priceTrail }: { demoCase: DemoCase; priceTrail: number[] }) { const { anchor, target, item_price } = demoCase const W = 300 const H = 150 const PAD = { top: 14, right: 72, bottom: 12, left: 10 } const plotW = W - PAD.left - PAD.right const plotH = H - PAD.top - PAD.bottom const hi = Math.max(item_price, ...(priceTrail.length ? priceTrail : [item_price])) const lo = Math.min(anchor, ...(priceTrail.length ? priceTrail : [anchor])) const span = (hi - lo) || 1 const yOf = (v: number) => PAD.top + (1 - (v - lo) / span) * plotH // 첫 라운드부터 점이 왼쪽에 고정되도록 슬롯을 최소 3칸으로 유지 const slots = Math.max(priceTrail.length - 1, 3) const xOf = (i: number) => PAD.left + (i / slots) * plotW // 기준선 3종 — 라벨은 선 오른쪽에 두고, 세로로 가까우면 밀어내서 겹침을 막는다 const refs = [ { v: item_price, label: '기존가', color: 'var(--color-base-content)', op: 0.4 }, { v: target, label: '목표가', color: 'var(--color-base-content)', op: 0.75 }, { v: anchor, label: '앵커가', color: 'var(--color-primary)', op: 1 }, ] .map((r) => ({ ...r, y: yOf(r.v), ly: yOf(r.v) })) .sort((a, b) => a.y - b.y) const MIN_GAP = 13 for (let i = 1; i < refs.length; i++) { if (refs[i].ly - refs[i - 1].ly < MIN_GAP) refs[i].ly = refs[i - 1].ly + MIN_GAP } const overflow = refs[refs.length - 1].ly - (H - 4) if (overflow > 0) for (const r of refs) r.ly -= overflow const pts = priceTrail.map((v, i) => ({ x: xOf(i), y: yOf(v), v })) const line = pts.map((p, i) => `${i === 0 ? 'M' : 'L'}${p.x},${p.y}`).join(' ') const area = pts.length >= 2 ? `${line} L${pts[pts.length - 1].x},${H - PAD.bottom} L${pts[0].x},${H - PAD.bottom} Z` : null const last = pts.length ? pts[pts.length - 1] : null return ( {refs.map((r) => ( {r.label} {r.v.toLocaleString()} ))} {area && } {pts.length >= 2 && ( )} {pts.slice(0, -1).map((p, i) => ( ))} {last && ( W - PAD.right - 30 ? 'end' : last.x < 36 ? 'start' : 'middle'} fontSize="10.5" fontWeight="700" fill="var(--color-base-content)" stroke="var(--color-base-100)" strokeWidth="3" paintOrder="stroke" className="num" > {last.v.toLocaleString()} )} {!last && ( 첫 제안가를 입력하면 추이가 그려져요 )} ) }