"use client"; import { Bar, CartesianGrid, ComposedChart, Line, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"; import { Card } from "@/components/ui/card"; import { ChartTooltip } from "@/components/charts/chart-tooltip"; import { CHART_COLORS } from "@/lib/chart-theme"; import { useI18n } from "@/lib/i18n"; import { formatForecastRainfall, formatForecastTemperature } from "@/lib/forecast-utils"; import { useWeatherStore } from "@/lib/store"; import { convertTemperature, formatTemperatureValue, getTemperatureUnitLabel } from "@/lib/weather-utils"; import type { HourlyForecast } from "@/types/forecast"; const INITIAL_CHART_DIMENSION = { width: 1, height: 1 }; interface ChartLegendItem { color: string; label: string; marker: "bar" | "line" | "dashed-line"; } function formatHour(value: string) { return value.slice(11, 16); } function ChartLegend({ items }: { items: ChartLegendItem[] }) { return (
{items.map((item) => ( {item.marker === "bar" ? (
); } export function DayForecastCharts({ hours }: { hours: HourlyForecast[] }) { const { language, t } = useI18n(); const temperatureUnit = useWeatherStore((state) => state.temperatureUnit); const rows = hours.map((hour) => ({ time: formatHour(hour.time), temperature: hour.temperature === null ? null : convertTemperature(hour.temperature, temperatureUnit), feelsLike: hour.feelsLike === null ? null : convertTemperature(hour.feelsLike, temperatureUnit), precipitation: hour.precipitation, precipitationProbability: hour.precipitationProbability, })); const temperatureUnitLabel = getTemperatureUnitLabel(temperatureUnit); const temperatureLegendItems: ChartLegendItem[] = [ { color: CHART_COLORS.temperature, label: t("forecast.temperature"), marker: "line" }, { color: CHART_COLORS.feelsLike, label: t("forecast.apparentTemperature"), marker: "dashed-line" }, ]; const rainfallLegendItems: ChartLegendItem[] = [ { color: CHART_COLORS.rainfall, label: t("forecast.precipitation"), marker: "bar" }, { color: CHART_COLORS.probability, label: t("forecast.precipitationProbability"), marker: "line" }, ]; return (

{t("forecast.temperatureChart")}

{t("forecast.temperatureChartDescription")}

typeof entry.value === "number" ? formatTemperatureValue(entry.value, language, temperatureUnit) : formatForecastTemperature(null, language, temperatureUnit) } /> } />

{t("forecast.rainfallChart")}

{t("forecast.rainfallChartDescription")}

entry.dataKey === "precipitation" ? formatForecastRainfall(typeof entry.value === "number" ? entry.value : null, language) : `${typeof entry.value === "number" ? entry.value : "—"}%` } /> } />
); }