"use client"; import React from "react"; import { CartesianGrid, Line, LineChart, XAxis, Bar, BarChart } from "recharts"; import { CardContent } from "@/components/ui/card"; import { ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent, ChartLegend, ChartLegendContent, } from "@/components/ui/chart"; import { formatCurrency, formatNumber } from "@/lib/utils/format"; import type { SpendingDataPoint, AnalyticsMetric } from "@/lib/types/api"; interface AnalyticsChartProps { data: SpendingDataPoint[]; metrics: AnalyticsMetric[]; chartType: "line" | "bar"; showDataLabels?: boolean; } const METRIC_LABELS: Record = { cost: "Расходы", subscriptions: "Подписки", views: "Просмотры", cpf: "CPF", cpm: "CPM", }; export const AnalyticsChart: React.FC = ({ data, metrics, chartType, showDataLabels = false, }) => { // Создаем конфигурацию для chart const chartConfig: ChartConfig = metrics.reduce((acc, metric, index) => { acc[metric] = { label: METRIC_LABELS[metric], color: `var(--chart-${index + 1})`, }; return acc; }, {} as ChartConfig); const formatValue = (value: number | null, metric: AnalyticsMetric) => { if (value === null) return "—"; if (metric === "cost" || metric === "cpf" || metric === "cpm") { return formatCurrency(value); } return formatNumber(value); }; if (chartType === "bar") { return ( `Период: ${value}`} formatter={(value, name) => [ formatValue( value as number, name as AnalyticsMetric ), chartConfig[name as AnalyticsMetric]?.label || name, ]} /> } /> } /> {metrics.map((metric) => ( formatValue(value, metric), fontSize: 10, } : undefined } /> ))} ); } return ( `Период: ${value}`} formatter={(value, name) => [ formatValue( value as number, name as AnalyticsMetric ), chartConfig[name as AnalyticsMetric]?.label || name, ]} /> } /> } /> {metrics.map((metric) => ( formatValue(value, metric), fontSize: 10, } : undefined } /> ))} ); };