Sparkline.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use client';
  2. import './sparkline.scss';
  3. import { Area, AreaChart, ResponsiveContainer, YAxis } from 'recharts';
  4. import type { MoveDir } from '@/types/worldIndex';
  5. // 미니 스파크라인 — 최근 종가 시계열(과거→최신)을 방향색(상승 적/하락 청)으로 표시.
  6. // 색은 래퍼의 dir 클래스(.spark--up/down/flat)가 지정하는 CSS color 를 currentColor 로 상속받는다.
  7. // (recharts stroke/fill 은 SVG presentation attribute 로 렌더돼 var() 가 해석되지 않으므로 currentColor 사용.)
  8. // 축·툴팁·점 없음. 부모 슬롯의 고정 크기를 100% 채운다(카드/그리드 셀 공용). 데이터 2개 미만이면 빈 placeholder.
  9. type Props = {
  10. data: number[];
  11. dir: MoveDir;
  12. };
  13. export default function Sparkline({ data, dir }: Props)
  14. {
  15. if (!data || data.length < 2) {
  16. return <div className='spark spark--empty' aria-hidden='true' />;
  17. }
  18. const chartData = data.map((v, i) => ({ i, v }));
  19. return (
  20. <div className={`spark spark--${dir}`} aria-hidden='true'>
  21. <ResponsiveContainer width='100%' height='100%'>
  22. <AreaChart data={chartData} margin={{ top: 2, right: 0, bottom: 2, left: 0 }}>
  23. <YAxis hide domain={['dataMin', 'dataMax']} />
  24. <Area
  25. type='monotone'
  26. dataKey='v'
  27. stroke='currentColor'
  28. strokeWidth={1.5}
  29. fill='currentColor'
  30. fillOpacity={0.12}
  31. dot={false}
  32. isAnimationActive={false}
  33. />
  34. </AreaChart>
  35. </ResponsiveContainer>
  36. </div>
  37. );
  38. }