import { useMemo } from 'react'; import { ScoreHistory } from '../../api/types'; import { cn } from '../../utils/helpers'; import { getTeamColor } from '../../utils/colors'; interface ScoreChartProps { history: ScoreHistory[]; height?: number; showLegend?: boolean; showGrid?: boolean; className?: string; } export function ScoreChart({ history, height = 300, showLegend = true, showGrid = true, className, }: ScoreChartProps) { // Get all rounds const allRounds = useMemo(() => { const rounds = new Set(); history.forEach((h) => { h.history.forEach((point) => rounds.add(point.round)); }); return Array.from(rounds).sort((a, b) => a - b); }, [history]); // Get max score for scaling const maxScore = useMemo(() => { let max = 0; history.forEach((h) => { h.history.forEach((point) => { if (point.score > max) max = point.score; }); }); return max || 100; }, [history]); // Generate path for a team const generatePath = (teamHistory: ScoreHistory) => { if (allRounds.length === 0 || teamHistory.history.length === 0) return ''; const points = allRounds.map((round) => { const point = teamHistory.history.find((p) => p.round === round); const score = point?.score || 0; const x = (allRounds.indexOf(round) / (allRounds.length - 1 || 1)) * 100; const y = 100 - (score / maxScore) * 100; return `${x},${y}`; }); return points.map((p, i) => (i === 0 ? `M ${p}` : `L ${p}`)).join(' '); }; if (history.length === 0) { return (

Нет данных для отображения

); } return (
{/* Chart */}
{/* Grid */} {showGrid && ( <> {[0, 25, 50, 75, 100].map((y) => ( ))} )} {/* Lines */} {history.map((teamHistory, index) => ( {/* Points */} {teamHistory.history.map((point, i) => { const x = (allRounds.indexOf(point.round) / (allRounds.length - 1 || 1)) * 100; const y = 100 - (point.score / maxScore) * 100; return ( ); })} ))} {/* X-axis labels */}
{allRounds.slice(0, 10).map((round) => ( {round} ))} {allRounds.length > 10 && ...} {allRounds.length > 1 && {allRounds[allRounds.length - 1]}}
{/* Legend */} {showLegend && (
{history.map((teamHistory, index) => (
{teamHistory.teamName}
))}
)}
); } export default ScoreChart;