import React, { useState, useEffect } from 'react'; import { SellingPoint } from '../../types/api'; interface GeometricChartProps { data: SellingPoint[]; } export const GeometricChart: React.FC = ({ data }) => { const [animatedScores, setAnimatedScores] = useState(data.map(() => 0)); useEffect(() => { const targetScores = data.map(item => item.score); const duration = 1500; const startTime = Date.now(); const animate = () => { const elapsed = Date.now() - startTime; const progress = Math.min(elapsed / duration, 1); const easeProgress = 1 - Math.pow(1 - progress, 3); setAnimatedScores(targetScores.map(t => t * easeProgress)); if (progress < 1) requestAnimationFrame(animate); }; requestAnimationFrame(animate); }, [data]); const size = 560; const center = size / 2; const maxRadius = 160; const sides = data.length; const levels = 5; const angleStep = (Math.PI * 2) / sides; const getAngle = (i: number) => angleStep * i - Math.PI / 2; const getPoint = (i: number, r: number) => ({ x: center + r * Math.cos(getAngle(i)), y: center + r * Math.sin(getAngle(i)), }); // 순위 매핑 const sortedIndices = [...data] .map((item, i) => ({ index: i, score: item.score })) .sort((a, b) => b.score - a.score) .map((item, rank) => ({ ...item, rank: rank + 1 })); const rankMap = new Map(); sortedIndices.forEach(item => rankMap.set(item.index, item.rank)); // 레벨 폴리곤 const levelPolygons = Array.from({ length: levels }, (_, li) => { const r = ((li + 1) / levels) * maxRadius; return data.map((_, i) => { const p = getPoint(i, r); return `${p.x},${p.y}`; }).join(' '); }); // 데이터 폴리곤 const dataPolygon = animatedScores.map((score, i) => { const r = (score / 100) * maxRadius; const p = getPoint(i, r); return `${p.x},${p.y}`; }).join(' '); // 데이터 포인트 const dataPoints = animatedScores.map((score, i) => { const r = (score / 100) * maxRadius; return getPoint(i, r); }); const labelRadius = maxRadius + 22; return (
{/* 레이더 폴리곤 fill: radial gradient */} {/* 레이더 폴리곤 filter: outer drop-shadow + inset glow */} {/* Outer drop shadow */} {/* Inset glow: invert alpha → blur → composite inside */} {/* 그리드 레벨 */} {levelPolygons.map((points, i) => ( ))} {/* 축 선 */} {data.map((_, i) => { const p = getPoint(i, maxRadius); return ( ); })} {/* 데이터 폴리곤 */} {/* 데이터 포인트 */} {dataPoints.map((point, i) => ( ))} {/* 라벨 */} {data.map((item, i) => { const angle = getAngle(i); const pos = getPoint(i, labelRadius); const rank = rankMap.get(i) || i + 1; const isTopThree = rank <= 3; const isLeftSide = Math.cos(angle) < -0.1; const textX = isLeftSide ? -20 : 20; const textAnchor: 'start' | 'end' = isLeftSide ? 'end' : 'start'; return ( {isLeftSide && ( {item.korean_category} )} {rank} {!isLeftSide && ( {item.korean_category} )} ); })}
); }; export default GeometricChart;