import React, { useState, useEffect } from 'react'; import axios from 'axios'; import { Chart as ChartJS, ArcElement, Tooltip, Legend, CategoryScale, LinearScale, PointElement, LineElement, BarElement } from 'chart.js'; import { Pie, Bar, Line } from 'react-chartjs-2'; import KPICard from './KPICard'; import API_BASE_URL from '../apiConfig'; ChartJS.register(ArcElement, Tooltip, Legend, CategoryScale, LinearScale, PointElement, LineElement, BarElement); const COLORS = { accent: '#f472b6', accent2: '#38bdf8', accent3: '#4ade80', danger: '#fb7185', warning: '#fbbf24', text: '#000000', textMuted: '#3f3f46' }; const DEFECT_COLORS = { 'Center': '#ef4444', 'Donut': '#f59e0b', 'Edge-Loc': '#10b981', 'Edge-Ring': '#3b82f6', 'Loc': '#8b5cf6', 'Random': '#ec4899', 'Scratch': '#06b6d4', 'Near-full': '#f97316', 'None': '#6b7280', 'Undetected': '#374151' }; const chartOptions = { color: COLORS.text, plugins: { legend: { labels: { color: COLORS.textMuted } } }, scales: { x: { ticks: { color: COLORS.textMuted }, grid: { color: 'rgba(0,0,0,0.1)' } }, y: { ticks: { color: COLORS.textMuted }, grid: { color: 'rgba(0,0,0,0.1)' } } } }; const pieOptions = { color: COLORS.text, plugins: { legend: { labels: { color: COLORS.textMuted } } } }; export const HistoricalAnalytics = () => { const [kpis, setKpis] = useState(null); const [defects, setDefects] = useState(null); const [waste, setWaste] = useState(null); const [trends, setTrends] = useState(null); useEffect(() => { const fetchData = async () => { const [kRes, dRes, wRes, tRes] = await Promise.all([ axios.get(`${API_BASE_URL}/api/kpi`), axios.get(`${API_BASE_URL}/api/charts/defects`), axios.get(`${API_BASE_URL}/api/charts/waste`), axios.get(`${API_BASE_URL}/api/charts/trends`) ]); setKpis(kRes.data); setDefects(dRes.data); setWaste(wRes.data); setTrends(tRes.data); }; fetchData(); }, []); if (!kpis || !defects || !waste || !trends) return