WorldMarketMap.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. 'use client';
  2. import './world-map.scss';
  3. import { useMemo, useState } from 'react';
  4. import { EXCHANGE_GEO, moveDir, formatFlucRate, formatIndexClose, type WorldIndexRow } from '@/types/worldIndex';
  5. // 등거리(plate carrée) 투영 — viewBox 1000×500 (경도 -180~180, 위도 90~-90)
  6. const VW = 1000;
  7. const VH = 500;
  8. const project = (lat: number, lng: number) => ({
  9. x: ((lng + 180) / 360) * VW,
  10. y: ((90 - lat) / 180) * VH
  11. });
  12. // 스타일라이즈드(저폴리) 대륙 실루엣 — 지리적 근사(정밀 지도 아님). viewBox 좌표계 직접 좌표.
  13. const CONTINENTS: string[] = [
  14. // 북미
  15. '140,84 210,66 302,70 314,104 288,126 302,158 274,176 250,156 248,196 226,210 200,236 188,222 212,198 214,172 190,156 166,130 150,106',
  16. // 그린란드
  17. '330,54 366,48 380,74 352,92 332,76',
  18. // 남미
  19. '300,266 340,258 364,278 380,300 366,336 344,384 320,414 304,420 296,388 300,342 290,300 292,278',
  20. // 유럽
  21. '470,92 518,84 550,94 560,116 540,134 512,150 496,134 480,142 470,116',
  22. // 아프리카
  23. '486,176 548,166 588,188 596,236 570,300 542,358 520,350 508,298 500,244 488,206',
  24. // 아시아(중동~러시아~중국~인도 벌지)
  25. '560,92 634,68 714,70 792,82 858,98 900,120 872,140 852,150 872,178 830,198 800,178 810,214 776,234 748,208 720,180 700,206 686,182 656,168 628,146 600,150 580,128 566,110',
  26. // 일본
  27. '878,136 896,150 886,172 872,158',
  28. // 인도네시아/동남아 도서
  29. '756,234 806,230 830,248 800,264 760,258',
  30. // 호주
  31. '800,316 858,306 928,332 916,362 856,378 812,362 796,336'
  32. ];
  33. // 위/경도 격자선
  34. const LNG_LINES = [-150, -120, -90, -60, -30, 0, 30, 60, 90, 120, 150];
  35. const LAT_LINES = [-60, -30, 0, 30, 60];
  36. type Props = {
  37. rows: WorldIndexRow[];
  38. };
  39. type Pin = {
  40. row: WorldIndexRow;
  41. label: string;
  42. x: number;
  43. y: number;
  44. dir: 'up' | 'down' | 'flat';
  45. radius: number;
  46. };
  47. export default function WorldMarketMap({ rows }: Props)
  48. {
  49. const [active, setActive] = useState<string|null>(null);
  50. // 좌표를 아는 국가만 핀으로. 등락 크기에 따라 반경 가변(6~10).
  51. const pins = useMemo<Pin[]>(() => {
  52. return rows
  53. .map((row) => {
  54. const geo = EXCHANGE_GEO[row.countryCode];
  55. if (!geo) {
  56. return null;
  57. }
  58. const { x, y } = project(geo.lat, geo.lng);
  59. const radius = Math.min(10, 6 + Math.abs(row.flucRateBp) / 80);
  60. return { row, label: geo.label, x, y, dir: moveDir(row.flucRateBp), radius };
  61. })
  62. .filter((c): c is Pin => c !== null);
  63. }, [rows]);
  64. // 기준일 = 핀 중 가장 최근 거래일
  65. const asOf = useMemo(() => {
  66. return rows.reduce<string|null>((max, c) => (max === null || c.tradeDate > max ? c.tradeDate : max), null);
  67. }, [rows]);
  68. const activePin = pins.find((c) => c.row.countryCode === active) ?? null;
  69. return (
  70. <section className='world-map' aria-label='세계 주요국 증시지수'>
  71. <div className='world-map__head'>
  72. <h2 className='world-map__title'>세계 증시</h2>
  73. <div className='world-map__meta'>
  74. <span className='world-map__legend'>
  75. <i className='world-map__dot world-map__dot--up' aria-hidden='true' />상승
  76. <i className='world-map__dot world-map__dot--down' aria-hidden='true' />하락
  77. </span>
  78. {asOf && <span className='world-map__asof'>기준 {asOf}</span>}
  79. </div>
  80. </div>
  81. <div className='world-map__stage'>
  82. {pins.length === 0 ? (
  83. <div className='world-map__empty'>표시할 지수 데이터가 없습니다.</div>
  84. ) : (
  85. <svg
  86. className='world-map__svg'
  87. viewBox={`0 0 ${VW} ${VH}`}
  88. role='img'
  89. aria-label='세계지도 위 주요국 증시지수 핀'
  90. preserveAspectRatio='xMidYMid meet'
  91. >
  92. <defs>
  93. <radialGradient id='wm-ocean' cx='50%' cy='38%' r='75%'>
  94. <stop offset='0%' stopColor='var(--az-map-ocean-1)' />
  95. <stop offset='100%' stopColor='var(--az-map-ocean-2)' />
  96. </radialGradient>
  97. <filter id='wm-glow' x='-60%' y='-60%' width='220%' height='220%'>
  98. <feGaussianBlur stdDeviation='4' result='b' />
  99. <feMerge>
  100. <feMergeNode in='b' />
  101. <feMergeNode in='SourceGraphic' />
  102. </feMerge>
  103. </filter>
  104. </defs>
  105. {/* 대양 */}
  106. <rect x='0' y='0' width={VW} height={VH} rx='16' fill='url(#wm-ocean)' />
  107. {/* 격자선 */}
  108. <g className='world-map__grid'>
  109. {LNG_LINES.map((lng) => {
  110. const x = ((lng + 180) / 360) * VW;
  111. return <line key={`lng-${lng}`} x1={x} y1='0' x2={x} y2={VH} />;
  112. })}
  113. {LAT_LINES.map((lat) => {
  114. const y = ((90 - lat) / 180) * VH;
  115. return <line key={`lat-${lat}`} x1='0' y1={y} x2={VW} y2={y} />;
  116. })}
  117. </g>
  118. {/* 대륙 */}
  119. <g className='world-map__land'>
  120. {CONTINENTS.map((points, i) => (
  121. <polygon key={i} points={points} />
  122. ))}
  123. </g>
  124. {/* 핀 */}
  125. <g className='world-map__pins'>
  126. {pins.map((pin) => {
  127. const isActive = pin.row.countryCode === active;
  128. return (
  129. <g
  130. key={pin.row.countryCode}
  131. className={`world-map__pin world-map__pin--${pin.dir}${isActive ? ' is-active' : ''}`}
  132. transform={`translate(${pin.x} ${pin.y})`}
  133. role='button'
  134. tabIndex={0}
  135. aria-label={`${pin.label} ${pin.row.name} ${formatFlucRate(pin.row.flucRateBp)}`}
  136. onMouseEnter={() => setActive(pin.row.countryCode)}
  137. onMouseLeave={() => setActive((cur) => (cur === pin.row.countryCode ? null : cur))}
  138. onFocus={() => setActive(pin.row.countryCode)}
  139. onBlur={() => setActive((cur) => (cur === pin.row.countryCode ? null : cur))}
  140. >
  141. {pin.dir !== 'flat' && <circle className='world-map__pulse' r={pin.radius} />}
  142. <circle className='world-map__pin-dot' r={pin.radius} />
  143. <circle className='world-map__pin-core' r={pin.radius / 2.6} />
  144. </g>
  145. );
  146. })}
  147. </g>
  148. </svg>
  149. )}
  150. {/* 툴팁 — 활성 핀 기준 % 위치 (viewBox 비율) */}
  151. {activePin && (
  152. <div
  153. className='world-map__tip'
  154. style={{ left: `${(activePin.x / VW) * 100}%`, top: `${(activePin.y / VH) * 100}%` }}
  155. role='status'
  156. >
  157. <div className='world-map__tip-head'>
  158. <span className='world-map__tip-country'>{activePin.label}</span>
  159. <span className='world-map__tip-name'>{activePin.row.name}</span>
  160. </div>
  161. <div className='world-map__tip-close'>{formatIndexClose(activePin.row.close)}</div>
  162. <div className={`world-map__tip-change world-map__tip-change--${activePin.dir}`}>
  163. {formatFlucRate(activePin.row.flucRateBp)}
  164. <span className='world-map__tip-exch'>{activePin.row.exchangeName}</span>
  165. </div>
  166. </div>
  167. )}
  168. </div>
  169. </section>
  170. );
  171. }