import { useState, useMemo } from 'react';
import { TrendingUp, Activity } from 'lucide-react';
import { AreaChart, Area, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer } from 'recharts';
interface StatisticalChartsProps {
data: Array<{
date: string;
cases: number;
risk?: number;
aqi?: number;
}>;
height?: number;
showCases?: boolean;
showRisk?: boolean;
showAQI?: boolean;
}
export function StatisticalCharts({
data,
height = 300,
showCases = true,
showRisk = false,
showAQI = false,
}: StatisticalChartsProps) {
const [activeChart, setActiveChart] = useState<'cases' | 'risk' | 'aqi'>('cases');
const chartData = useMemo(() => {
return data.map((item) => ({
...item,
date: new Date(item.date).toLocaleDateString('zh-CN', { month: 'short', day: 'numeric' }),
}));
}, [data]);
const calculateTrend = (values: number[]) => {
if (values.length < 2) return 'stable';
const firstHalf = values.slice(0, Math.floor(values.length / 2));
const secondHalf = values.slice(Math.floor(values.length / 2));
const firstAvg = firstHalf.reduce((a, b) => a + b, 0) / firstHalf.length;
const secondAvg = secondHalf.reduce((a, b) => a + b) / secondHalf.length;
const change = ((secondAvg - firstAvg) / firstAvg) * 100;
if (change > 10) return 'up';
if (change < -10) return 'down';
return 'stable';
};
const stats = useMemo(() => {
if (data.length === 0) return null;
const totalCases = data.reduce((sum, item) => sum + item.cases, 0);
const avgCases = totalCases / data.length;
const maxCases = Math.max(...data.map((item) => item.cases));
const trend = calculateTrend(data.map((item) => item.cases));
return {
totalCases,
avgCases: Math.round(avgCases),
maxCases,
trend,
};
}, [data]);
const getTrendIcon = () => {
if (!stats) return null;
switch (stats.trend) {
case 'up':
return