import React from 'react';
import { motion } from 'framer-motion';
import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Cell } from 'recharts';
import { BarChart2, TrendingUp, TrendingDown, Activity, AlertTriangle } from 'lucide-react';
const ROAD_LABELS = ['Road A', 'Road B', 'Road C', 'Road D'];
const BAR_COLORS = ['#3b82f6', '#06b6d4', '#8b5cf6', '#f59e0b'];
const getCongestion = (pct) => {
if (pct <= 20) return { label: 'Low', barClass: 'bar-green', color: '#4ade80' };
if (pct <= 45) return { label: 'Medium', barClass: 'bar-amber', color: '#fbbf24' };
if (pct <= 70) return { label: 'High', barClass: 'bar-orange', color: '#fb923c' };
return { label: 'Critical', barClass: 'bar-red', color: '#f87171' };
};
const CustomTooltip = ({ active, payload }) => {
if (!active || !payload?.length) return null;
return (
{payload[0].payload.name}
{payload[0].value} vehicles
);
};
const StatCard = ({ title, value, Icon, color, sub }) => (
{title}
{value}
{sub &&
{sub}
}
);
const TrafficAnalysisPanel = ({ vehicleData = [] }) => {
const counts = vehicleData.map(d => d?.count || 0);
const total = counts.reduce((a, b) => a + b, 0);
const avg = vehicleData.length > 0 ? (total / vehicleData.length).toFixed(1) : 0;
const peak = Math.max(...counts, 0);
const low = vehicleData.length > 0 ? Math.min(...counts) : 0;
const maxC = peak || 1;
const ranked = [...vehicleData]
.map((d, i) => ({ label: ROAD_LABELS[i] || `Road ${i+1}`, count: d?.count || 0, index: i }))
.sort((a, b) => b.count - a.count);
const chartData = vehicleData.map((d, i) => ({ name: ROAD_LABELS[i] || `Road ${i+1}`, count: d?.count || 0 }));
return (
{/* Header */}
Traffic Analysis
Vehicle density scoring, congestion levels, and road priority ranking
{/* LEFT COLUMN */}
{/* Stat Cards */}
{/* Congestion Bars */}
Road Congestion Density
{vehicleData.map((d, i) => {
const count = d?.count || 0;
const pct = (count / maxC) * 100;
const cong = getCongestion(pct);
return (
{ROAD_LABELS[i]}
{cong.label}
{count}
);
})}
{/* Priority Ranking */}
Priority Ranking
{ranked.map((road, rank) => {
const rankColors = ['#ef4444', '#f97316', '#f59e0b', '#64748b'];
return (
#{rank + 1}
{road.label}
{road.count}
);
})}
{/* RIGHT — Bar Chart */}
Vehicle Count by Road
} cursor={{ fill: 'rgba(59,130,246,0.06)' }} />
{chartData.map((_, i) => | )}
{/* Legend */}
{chartData.map((d, i) => (
))}
);
};
export default TrafficAnalysisPanel;