import { useQuery } from '@tanstack/react-query'; import { BarElement, CategoryScale, Chart as ChartJS, Filler, Legend, LinearScale, LogarithmicScale, LineElement, PointElement, Tooltip, type ChartOptions, } from 'chart.js'; import { Bar, Line } from 'react-chartjs-2'; import { fetchStationSummary, type DayView, type Station } from '../api/traffic'; import { formatDate, formatNumber } from '../traffic/format'; import { DIRECTION_COLORS, VEHICLE_CLASSES, VEHICLE_GROUPS, sumCounts, } from '../traffic/vehicleClasses'; ChartJS.register( CategoryScale, LinearScale, LogarithmicScale, PointElement, LineElement, BarElement, Filler, Tooltip, Legend, ); ChartJS.defaults.font.family = 'system-ui, -apple-system, "Segoe UI", sans-serif'; ChartJS.defaults.font.size = 11; ChartJS.defaults.color = '#475569'; interface StationPanelProps { station: Station; day: DayView | null; hour: number; onClose: () => void; } const HOUR_LABELS = Array.from({ length: 24 }, (_, h) => String(h).padStart(2, '0')); const baseLineOptions: ChartOptions<'line'> = { responsive: true, maintainAspectRatio: false, animation: false, interaction: { mode: 'index', intersect: false }, plugins: { legend: { position: 'bottom', labels: { boxWidth: 12, boxHeight: 2 } } }, elements: { point: { radius: 0, hoverRadius: 3 }, line: { borderWidth: 1.75, tension: 0.25 } }, scales: { x: { grid: { display: false }, ticks: { maxRotation: 0, autoSkipPadding: 16 } }, y: { beginAtZero: true, grid: { color: '#e2e8f0' }, title: { display: true, text: 'Fz/h' } }, }, }; export function StationPanel({ station, day, hour, onClose }: StationPanelProps) { const summaryQuery = useQuery({ queryKey: ['station-summary', station.code], queryFn: () => fetchStationSummary(station.code), }); const summary = summaryQuery.data; const stationDay = day?.stations.find((s) => s.code === station.code); const hourIndex = Math.min(23, Math.max(0, hour)); const hourlyLabels = summary?.directions[0]?.hourly.map((h) => formatHourLabel(h.time)) ?? []; const dailyLabels = summary?.directions[0]?.daily.map((d) => formatDate(d.date, { year: undefined })) ?? []; return ( ); } function formatHourLabel(time: string): string { const date = new Date(time); return `${date.toLocaleDateString('de-CH', { weekday: 'short', day: '2-digit', month: '2-digit' })} ${String(date.getHours()).padStart(2, '0')}:00`; }