'use client';
import * as React from 'react';
import {
Area,
AreaChart,
Bar,
BarChart,
CartesianGrid,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis,
} from 'recharts';
interface DataPoint {
period: string;
value: number;
unit: string;
}
interface ReportChartProps {
data: DataPoint[];
title: string;
variant?: 'area' | 'bar';
color?: string;
height?: number;
}
const COLORS = {
primary: '#2563eb',
secondary: '#16a34a',
accent: '#d97706',
};
function formatValue(value: number, unit: string): string {
if (unit === 'tỷ USD' || unit === 'tỷ VND') {
return `${value.toLocaleString('vi-VN')} ${unit}`;
}
if (unit === '%') {
return `${value}%`;
}
if (unit === 'người' || unit === 'triệu người') {
return value.toLocaleString('vi-VN');
}
return `${value.toLocaleString('vi-VN')} ${unit}`;
}
export function ReportChart({
data,
title,
variant = 'area',
color = COLORS.primary,
height = 240,
}: ReportChartProps) {
if (!data || data.length === 0) return null;
const unit = data[0]?.unit ?? '';
const chartData = data.map((d) => ({
name: d.period,
value: d.value,
}));
return (
{title}
{variant === 'bar' ? (
[formatValue(Number(value), unit), title]}
contentStyle={{ fontSize: 12 }}
/>
) : (
[formatValue(Number(value), unit), title]}
contentStyle={{ fontSize: 12 }}
/>
)}
);
}
interface ReportChartsGridProps {
charts: Record;
labels?: Record;
}
const DEFAULT_CHART_LABELS: Record = {
gdp_trend: 'GDP',
fdi_trend: 'Vốn FDI',
population: 'Dân số',
urbanization: 'Đô thị hóa',
labor_force: 'Lực lượng lao động',
avg_wage: 'Lương bình quân',
industrial_output: 'Sản lượng công nghiệp',
cpi: 'Chỉ số giá tiêu dùng',
mortgage_rate: 'Lãi suất vay',
};
const CHART_COLORS: Record = {
gdp_trend: COLORS.primary,
fdi_trend: COLORS.secondary,
population: COLORS.accent,
urbanization: COLORS.primary,
labor_force: COLORS.secondary,
avg_wage: COLORS.accent,
};
export function ReportChartsGrid({ charts, labels }: ReportChartsGridProps) {
const mergedLabels = { ...DEFAULT_CHART_LABELS, ...labels };
const validCharts = Object.entries(charts).filter(
([, data]) => Array.isArray(data) && data.length > 0,
);
if (validCharts.length === 0) return null;
return (
{validCharts.map(([key, data]) => (
))}
);
}