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
Loading Analytics...
; const pieData = { labels: defects.predictions.map(d => d.defect_type), datasets: [{ data: defects.predictions.map(d => d.count), backgroundColor: defects.predictions.map(d => DEFECT_COLORS[d.defect_type] || COLORS.textMuted), borderColor: 'transparent' }] }; const barData = { labels: waste.waste_by_type.map(w => w.defect_type), datasets: [{ label: 'Total Material Waste (Wafers)', data: waste.waste_by_type.map(w => w.total_waste), backgroundColor: waste.waste_by_type.map(w => DEFECT_COLORS[w.defect_type] || COLORS.textMuted) }] }; const trendData = { labels: trends.dates, datasets: [{ label: 'Fail Rate %', data: trends.fail_rate, borderColor: COLORS.danger, backgroundColor: 'rgba(251, 113, 133, 0.4)', fill: true, yAxisID: 'y' }] }; const wasteTrendData = { labels: trends.dates, datasets: [{ label: 'Total Lost Wafers', data: trends.waste, borderColor: COLORS.warning, backgroundColor: 'rgba(251, 191, 36, 0.4)', fill: true, yAxisID: 'y' }] }; return (

YOLOv8 Predicted Distributions

Total Material Waste by Predict Defect

Daily Defect Rate Over Time

Daily Material Waste Over Time

); };