import React, { memo, useState, useEffect } from 'react'; import { Button, Row, Col, Card, Input, DatePicker, Select, Typography } from 'antd'; import dayjs from 'dayjs'; import { FileTextOutlined } from '@ant-design/icons'; import { ResponsiveLine } from '@nivo/line'; import './trending.css'; import { getAllPlantSection } from '../../../api/master-plant-section'; import { getAllHistoryValueTrendingPivot } from '../../../api/history-value'; const { Text } = Typography; const ReportTrending = memo(function ReportTrending(props) { const dateNow = dayjs(); const dateNowFormated = dateNow.format('YYYY-MM-DD'); const [plantSubSection, setPlantSubSection] = useState(0); const [plantSubSectionList, setPlantSubSectionList] = useState([]); const [startDate, setStartDate] = useState(dateNow); const [endDate, setEndDate] = useState(dateNow); const [periode, setPeriode] = useState(60); const defaultFilter = { criteria: '', plant_sub_section_id: plantSubSection, from: dateNowFormated, to: dateNowFormated, interval: periode, }; const [formDataFilter, setFormDataFilter] = useState(defaultFilter); const [trendingValue, setTrendingValue] = useState([]); const handleSearch = async () => { const formattedDateStart = startDate.format('YYYY-MM-DD'); const formattedDateEnd = endDate.format('YYYY-MM-DD'); const newFilter = { criteria: '', plant_sub_section_id: plantSubSection, from: formattedDateStart, to: formattedDateEnd, interval: periode, }; setFormDataFilter(newFilter); const param = new URLSearchParams(newFilter); const response = await getAllHistoryValueTrendingPivot(param); if (response?.data?.length > 0) { // 🔹 Bersihkan dan format data agar aman untuk Nivo const cleanedData = response.data.map((serie) => ({ id: serie.id ?? 'Unknown', data: Array.isArray(serie.data) ? serie.data.map((d) => ({ x: d?.x ?? null, y: d?.y !== null && d?.y !== undefined ? Number(d.y).toFixed(4) // format 4 angka di belakang koma : null, })) : [], })); setTrendingValue(cleanedData); } else { // 🔹 Jika tidak ada data dari API setTrendingValue([]); } }; const handleReset = () => { setPlantSubSection(0); setStartDate(dateNow); setEndDate(dateNow); setPeriode(5); }; const getPlantSubSection = async () => { const params = new URLSearchParams({ page: 1 }); const response = await getAllPlantSection(params); if (response && response.data) { const activePlantSubSections = response.data.filter( (section) => section.is_active === true ); setPlantSubSectionList(activePlantSubSections); } }; useEffect(() => { getPlantSubSection(); }, []); return (
Plant Sub Section
Tanggal Mulai
Tanggal Akhir
Periode
{trendingValue && trendingValue.length > 0 ? ( Number(value).toFixed(4)} // ✅ format 4 angka di belakang koma axisBottom={{ format: '%Y-%m-%d %H:%M', // ✅ tampilkan tanggal + jam tickValues: 'every 2 hours', // tampilkan setiap 2 jam (bisa ubah ke every 30 minutes) tickSize: 5, tickPadding: 5, tickRotation: -45, legend: 'Tanggal & Waktu', legendOffset: 60, legendPosition: 'middle', }} axisLeft={{ tickSize: 5, tickPadding: 5, tickRotation: 0, legend: 'Nilai (Avg)', legendOffset: -60, legendPosition: 'middle', format: (value) => Number(value).toFixed(4), // ✅ tampilkan 4 angka di sumbu Y }} curve="monotoneX" colors={{ scheme: 'category10' }} pointSize={6} pointColor={{ theme: 'background' }} pointBorderWidth={2} pointBorderColor={{ from: 'serieColor' }} enablePointLabel={false} enableGridX={true} enableGridY={true} useMesh={true} tooltip={({ point }) => (
{point.serieId}
{point.data.xFormatted}
{Number(point.data.y).toFixed(4)}
)} legends={[ { anchor: 'bottom-right', direction: 'column', justify: false, translateX: 100, translateY: 0, itemsSpacing: 2, itemDirection: 'left-to-right', itemWidth: 120, itemHeight: 20, itemOpacity: 0.85, symbolSize: 12, symbolShape: 'circle', }, ]} /> ) : (
Tidak ada data untuk ditampilkan
)}
); }); export default ReportTrending;