"use client"; import * as React from "react"; import { CalendarIcon } from "lucide-react"; import { type DateRange } from "react-day-picker"; import { Bar, BarChart, CartesianGrid, XAxis } from "recharts"; import { Button } from "~/components/ui/button"; import { Calendar } from "~/components/ui/calendar"; import { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "~/components/ui/card"; import { type ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent, } from "~/components/ui/chart"; import { Popover, PopoverContent, PopoverTrigger, } from "~/components/ui/popover"; import { cn } from "~/lib/utils"; import { enUS, it } from "date-fns/locale"; import { useTranslation } from "~/providers/I18nProvider"; const chartConfig = { valueA: { label: "valueA", color: "var(--chart-2)", }, valueB: { label: "valueB", color: "var(--chart-3)", }, } satisfies ChartConfig; export type StackedTimeserie = { date: Date; valueA: number; valueB: number }; type AnalyticsChartProps = { title: string; description?: string; valueALabel: string; valueBLabel: string; timeserie: StackedTimeserie[]; }; export default function TimeserieBarChartStacked({ timeserie, title, valueALabel, valueBLabel, description, }: AnalyticsChartProps) { const { locale } = useTranslation(); const configs = chartConfig; configs.valueA.label = valueALabel; configs.valueB.label = valueBLabel; const startDate = timeserie[0] ? new Date(timeserie[0].date) : new Date(2025, 1, 1); const endDate = timeserie[timeserie.length - 1] ? new Date(timeserie[timeserie.length - 1]!.date) : new Date(); const [range, setRange] = React.useState({ from: startDate, to: endDate, }); const filteredData = React.useMemo(() => { if (!range?.from && !range?.to) { return timeserie; } return timeserie.filter((item) => { return item.date >= range.from! && item.date <= range.to!; }); }, [range]); const DatePickerLocale = locale === "it" ? it : enUS; return ( {title} {description || "Analytics for the selected period."} {filteredData.length === 0 ? (
Nessun dato disponibile per il periodo selezionato.
) : ( { const date = new Date(String(value)); return date.toLocaleDateString("it-IT", { day: "numeric", month: "short", }); }} /> } /> )}
Totale: {filteredData.reduce((acc) => acc + 1, 0)}
); }