| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  | import { Box, CircularProgress, Alert } from '@mui/material'; | 
					
						
							|  |  |  | import type { CotizacionGrano } from '../models/mercadoModels'; | 
					
						
							|  |  |  | import { useApiData } from '../hooks/useApiData'; | 
					
						
							|  |  |  | import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'; | 
					
						
							| 
									
										
										
										
											2025-07-07 10:01:10 -03:00
										 |  |  | import { formatFullDateTime } from '../utils/formatters'; | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | interface GrainsHistoricalChartWidgetProps { | 
					
						
							|  |  |  |   nombre: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-07 10:01:10 -03:00
										 |  |  | const formatTooltipLabel = (label: string) => { | 
					
						
							|  |  |  |     return formatFullDateTime(label); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  | const formatXAxis = (tickItem: string) => { | 
					
						
							|  |  |  |     const date = new Date(tickItem); | 
					
						
							|  |  |  |     return date.toLocaleDateString('es-AR', { day: '2-digit', month: '2-digit' }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const GrainsHistoricalChartWidget = ({ nombre }: GrainsHistoricalChartWidgetProps) => { | 
					
						
							|  |  |  |     const apiUrl = `/mercados/granos/history/${encodeURIComponent(nombre)}?dias=30`; | 
					
						
							|  |  |  |     const { data, loading, error } = useApiData<CotizacionGrano[]>(apiUrl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (loading) { | 
					
						
							|  |  |  |         return <Box sx={{ display: 'flex', justifyContent: 'center', p: 4, height: 300 }}><CircularProgress /></Box>; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (error) { | 
					
						
							|  |  |  |         return <Alert severity="error" sx={{ height: 300 }}>{error}</Alert>; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!data || data.length < 2) { | 
					
						
							|  |  |  |         return <Alert severity="info" sx={{ height: 300 }}>No hay suficientes datos históricos para graficar este grano.</Alert>; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ( | 
					
						
							|  |  |  |         <ResponsiveContainer width="100%" height={300}> | 
					
						
							|  |  |  |             <LineChart data={data} margin={{ top: 5, right: 30, left: 20, bottom: 5 }}> | 
					
						
							|  |  |  |                 <CartesianGrid strokeDasharray="3 3" /> | 
					
						
							| 
									
										
										
										
											2025-07-07 10:01:10 -03:00
										 |  |  |                 {/* Para los granos, usamos la fecha de operación en el eje X, que es más relevante */} | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |                 <XAxis dataKey="fechaOperacion" tickFormatter={formatXAxis} /> | 
					
						
							|  |  |  |                 <YAxis domain={['dataMin - 1000', 'dataMax + 1000']} tickFormatter={(tick) => `$${tick.toLocaleString('es-AR')}`} /> | 
					
						
							| 
									
										
										
										
											2025-07-07 10:01:10 -03:00
										 |  |  |                 <Tooltip  | 
					
						
							|  |  |  |                     formatter={(value: number) => [`$${value.toFixed(0)}`, 'Precio']}  | 
					
						
							|  |  |  |                     labelFormatter={formatTooltipLabel} | 
					
						
							|  |  |  |                 /> | 
					
						
							| 
									
										
										
										
											2025-07-03 11:44:10 -03:00
										 |  |  |                 <Legend /> | 
					
						
							|  |  |  |                 <Line type="monotone" dataKey="precio" name="Precio" stroke="#028fbe" strokeWidth={2} dot={false} /> | 
					
						
							|  |  |  |             </LineChart> | 
					
						
							|  |  |  |         </ResponsiveContainer> | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | }; |