Files
Alberto-Audrix 32a36cceff
CI / lint-and-test (push) Canceled after 0s
first commit
2026-07-28 08:55:05 +07:00

133 lines
4.2 KiB
TypeScript

import React from 'react';
import {
LineChart,
Line,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
ResponsiveContainer,
} from 'recharts';
import { FcrDataPoint } from '../../types.ts';
interface FcrComparisonChartProps {
data: FcrDataPoint[];
startDate?: Date;
}
const FcrComparisonChart: React.FC<FcrComparisonChartProps> = ({ data, startDate }) => {
// Check if there's no data
const hasNoData = !data || data.length === 0;
// Helper function to format date for a given day
const formatDateForDay = (day: number): string => {
if (!startDate) return '';
const date = new Date(startDate);
date.setDate(date.getDate() + (day - 1)); // day is 1-based
return date.toLocaleDateString('id-ID', { day: 'numeric', month: 'short' });
};
// Get min and max days for tick formatting
const days = data.map((d) => d.day);
const minDay = days.length > 0 ? Math.min(...days) : 1;
const maxDay = days.length > 0 ? Math.max(...days) : 1;
// Custom tick component for x-axis
const CustomTick = (props: any) => {
const { x, y, payload } = props;
const day = payload.value;
const isFirstOrLast = day === minDay || day === maxDay;
return (
<g transform={`translate(${x},${y})`}>
<text x={0} y={0} dy={16} textAnchor="middle" fill="#6b7280" fontSize={12}>
{isFirstOrLast ? `Hari ${day}` : `${day}`}
</text>
{isFirstOrLast && startDate && (
<text x={0} y={0} dy={30} textAnchor="middle" fill="#6b7280" fontSize={11}>
{formatDateForDay(day)}
</text>
)}
</g>
);
};
return (
<div className="bg-white p-6 rounded-xl shadow-sm">
<h2 className="text-xl font-bold mb-4">Perbandingan FCR (Feed Consumption Ratio)</h2>
<div className="h-80">
{hasNoData ? (
<div className="flex items-center justify-center h-full">
<div className="text-center">
<p className="text-gray-500 text-lg font-medium">Tidak ada data tersedia</p>
<p className="text-gray-400 text-sm mt-2">Tidak ada data FCR yang tersedia</p>
</div>
</div>
) : (
<ResponsiveContainer width="100%" height="100%">
<LineChart data={data} margin={{ top: 5, right: 20, left: -10, bottom: 5 }}>
<CartesianGrid strokeDasharray="3 3" stroke="#e0e0e0" />
<XAxis dataKey="day" tick={<CustomTick />} height={60} />
<YAxis
fontSize={12}
tick={{ fill: '#6b7280' }}
width={50}
domain={[0, 'dataMax + 0.2']}
label={{ value: 'FCR', angle: -90, position: 'insideLeft' }}
/>
<Tooltip
contentStyle={{
backgroundColor: 'rgba(255, 255, 255, 0.9)',
border: '1px solid #ccc',
borderRadius: '8px',
}}
formatter={(value: number, name: string) => [value.toFixed(3), name]}
/>
<Legend wrapperStyle={{ fontSize: '14px' }} verticalAlign="top" align="right" />
{/* Manual FCR Line - Red */}
<Line
type="monotone"
dataKey="manualFcr"
name="Manual FCR"
stroke="#ef4444"
strokeWidth={2.5}
dot={{ r: 4 }}
activeDot={{ r: 6 }}
connectNulls
/>
{/* IoT FCR Line - Green */}
<Line
type="monotone"
dataKey="iotFcr"
name="IoT FCR"
stroke="#16a34a"
strokeWidth={2.5}
dot={{ r: 4 }}
activeDot={{ r: 6 }}
connectNulls
/>
{/* COBB Standard FCR Line - Blue Dashed */}
<Line
type="monotone"
dataKey="cobbFcr"
name="Standar COBB"
stroke="#3b82f6"
strokeWidth={2}
strokeDasharray="5 5"
dot={false}
connectNulls
/>
</LineChart>
</ResponsiveContainer>
)}
</div>
</div>
);
};
export default FcrComparisonChart;