import React, { useState, useEffect } from "react"; import { format, parseISO, isBefore, isAfter, startOfDay } from "date-fns"; import { CalendarIcon } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Calendar } from "@/components/ui/calendar"; import { Popover, PopoverContent, PopoverTrigger, } from "@/components/ui/popover"; import { Select, SelectContent, SelectGroup, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { CartesianGrid, Line, LineChart, XAxis, YAxis, Tooltip, ResponsiveContainer, Legend } from "recharts"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { ScrollArea, ScrollBar } from '@/components/ui/scroll-area' import axios from 'axios'; import { API_URL } from "../../helpers/networt"; import { useToast } from '@/hooks/use-toast'; const ViewGrafik = ({ date, setDate, dataYAxis, setDataYAxis, priceType, setPriceType, chartData, setChartData, setTabelDataAktual, setTabelDataPredict }) => { const { toast } = useToast(); const [dateTerbaru, setDateTerbaru] = useState(null); const [dateTerlama, setDateTerlama] = useState(null); const oldestDate = dateTerlama ? parseISO(dateTerlama) : null; const latestDate = dateTerbaru ? parseISO(dateTerbaru) : null; const [tempPriceType, setTempPriceType] = useState(""); const handleDateChange = (selectedDate) => setDate(selectedDate); const handlePriceTypeChange = (value) => setTempPriceType(value); const fetchDataDate = async () => { try { const response = await axios.get(`${API_URL}/predict/date`); console.log(response.data.tanggal_old) setDateTerbaru(response.data.tanggal_new) setDateTerlama(response.data.tanggal_old) } catch (error) { console.error("Error fetching data", error); } } useEffect(() => { fetchDataDate(); }, []); const fetchData = async () => { const formattedDate = format(date, "yyyy-MM-dd"); if (!date && !tempPriceType) { setTabelDataAktual([]); setTabelDataPredict([]); setPriceType(""); setChartData([]); setDataYAxis([11000, 15000]); toast({ description: "tanggal dan harga harus di isi!", variant: "destructive", }); return; } if (!date) { setTabelDataAktual([]); setTabelDataPredict([]); setPriceType(""); setChartData([]); setDataYAxis([11000, 15000]); toast({ description: "Pilih tanggal terlebih dahulu!", variant: "destructive", }); return; } if (formattedDate==dateTerbaru && tempPriceType=="all") { setTabelDataAktual([]); setTabelDataPredict([]); setPriceType(""); setChartData([]); setDataYAxis([11000, 15000]); toast({ description: "tanggal ini hanya ada data prediksi", variant: "destructive", }); return; } if (formattedDate==dateTerbaru && tempPriceType=="actual") { setTabelDataAktual([]); setTabelDataPredict([]); setPriceType(""); setChartData([]); setDataYAxis([11000, 15000]); toast({ description: "tanggal ini hanya ada data prediksi", variant: "destructive", }); return; } if (!tempPriceType) { setTabelDataAktual([]); setTabelDataPredict([]); setPriceType(""); setChartData([]); setDataYAxis([11000, 15000]); toast({ description: "Pilih harga terlebih dahulu!", variant: "destructive", }); return; } try { const response = await axios.get(`${API_URL}/predict/history?tanggal=${formattedDate}&data_type=${tempPriceType}`); const formattedData = response.data.dataGrafik.map(item => ({ date: item.tanggal, actual: item.harga_aktual, predicted: item.harga_prediksi })); setPriceType(tempPriceType); setDataYAxis(response.data.YAxis); setChartData(formattedData); setTabelDataAktual(response.data.dataTableAktual); setTabelDataPredict(response.data.dataTablePrediksi) toast({ description: "Data berhasil di tampilkan", }); } catch (error) { console.error("Error fetching data", error); } }; return (

Grafik Harga Tomat Konsumen dari Tanggal Terpilih

Tanggal:

// Nonaktifkan tanggal SETELAH tanggal terbaru (latestDate && isAfter(startOfDay(day), latestDate)) || // Nonaktifkan tanggal SEBELUM tanggal terlama (oldestDate && isBefore(startOfDay(day), oldestDate)) } />

Harga:

{priceType === "all" ? "Harga Aktual vs Prediksi" : priceType === "actual" ? "Harga Aktual" : "Harga Prediksi"} Data harga tomat `Rp${value}`} /> `Rp${value}`} /> {/* */} {priceType !== "predicted" && ( )} {priceType !== "actual" && ( )}
Tanggal
{priceType !== "predicted" && (

Harga Aktual

)} {priceType !== "actual" && (

Harga Prediksi

)}
); }; export default ViewGrafik;