179 lines
6.1 KiB
TypeScript
179 lines
6.1 KiB
TypeScript
import React from 'react';
|
|
import {
|
|
BarChart,
|
|
Bar,
|
|
XAxis,
|
|
YAxis,
|
|
CartesianGrid,
|
|
Tooltip,
|
|
ResponsiveContainer,
|
|
Cell,
|
|
} from 'recharts';
|
|
import { WeightDistributionPoint, WeightDistributionStats } from '../../types.ts';
|
|
import { formatWeight } from '../../utils/weightUtils';
|
|
import { formatNumber } from '../../utils/formatters.ts';
|
|
|
|
interface WeightDistributionChartProps {
|
|
data: WeightDistributionPoint[];
|
|
stats: WeightDistributionStats | null;
|
|
// Date selector props
|
|
selectedDate?: Date;
|
|
minDate?: Date;
|
|
maxDate?: Date;
|
|
onDateChange?: (date: Date) => void;
|
|
showDateSelector?: boolean; // Only show in LIVE mode
|
|
}
|
|
|
|
const WeightDistributionChart: React.FC<WeightDistributionChartProps> = ({
|
|
data,
|
|
stats,
|
|
selectedDate,
|
|
minDate,
|
|
maxDate,
|
|
onDateChange,
|
|
showDateSelector = false,
|
|
}) => {
|
|
// Check if there's no data
|
|
const hasNoData = !data || data.length === 0;
|
|
|
|
// Assuming target range is around the middle bars
|
|
const targetIndexStart = Math.floor(data.length / 2) - 1;
|
|
const targetIndexEnd = Math.floor(data.length / 2) + 1;
|
|
|
|
const formatDateForInput = (date: Date): string => {
|
|
return date.toISOString().split('T')[0] ?? '';
|
|
};
|
|
|
|
const handleDateChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
if (onDateChange) {
|
|
const newDate = new Date(e.target.value);
|
|
onDateChange(newDate);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="bg-white p-6 rounded-xl shadow-sm">
|
|
<div className="flex items-center justify-between mb-4">
|
|
<div>
|
|
<h2 className="text-xl font-bold">Distribusi Bobot</h2>
|
|
{stats && (
|
|
<div className="mt-2 text-sm text-gray-600">
|
|
<p className="font-medium">
|
|
{stats.date}
|
|
{stats.dayOfCycle > 0 && ` (Hari ke-${stats.dayOfCycle})`}
|
|
</p>
|
|
<p>
|
|
Total Ayam:{' '}
|
|
<span className="font-semibold">{formatNumber(stats.totalChickenCount)}</span> ekor
|
|
</p>
|
|
</div>
|
|
)}
|
|
</div>
|
|
{/* Date Selector - Top right */}
|
|
{showDateSelector && selectedDate && minDate && maxDate && (
|
|
<div className="flex items-center space-x-2">
|
|
<label htmlFor="distribution-date" className="text-sm font-medium text-gray-700">
|
|
Pilih Tanggal:
|
|
</label>
|
|
<input
|
|
id="distribution-date"
|
|
type="date"
|
|
value={formatDateForInput(selectedDate)}
|
|
min={formatDateForInput(minDate)}
|
|
max={formatDateForInput(maxDate)}
|
|
onChange={handleDateChange}
|
|
className="px-3 py-1.5 text-sm border border-gray-300 rounded-md focus:ring-2 focus:ring-red-500 focus:border-red-500 shadow-sm"
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="h-64">
|
|
{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 distribusi bobot untuk periode yang dipilih
|
|
</p>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<BarChart data={data} margin={{ top: 5, right: 20, left: -15, bottom: 5 }}>
|
|
<CartesianGrid strokeDasharray="3 3" stroke="#e0e0e0" />
|
|
<XAxis
|
|
dataKey="range"
|
|
fontSize={10}
|
|
tick={{ fill: '#6b7280' }}
|
|
interval={0}
|
|
angle={-30}
|
|
textAnchor="end"
|
|
height={40}
|
|
/>
|
|
<YAxis
|
|
fontSize={12}
|
|
tick={{ fill: '#6b7280' }}
|
|
label={{ value: 'Jumlah Ayam', angle: -90, position: 'insideLeft' }}
|
|
/>
|
|
<Tooltip
|
|
cursor={{ fill: 'rgba(239, 68, 68, 0.1)' }}
|
|
contentStyle={{
|
|
backgroundColor: 'rgba(255, 255, 255, 0.9)',
|
|
border: '1px solid #ccc',
|
|
borderRadius: '8px',
|
|
}}
|
|
formatter={(value: number) => [`${formatNumber(value)} ekor`, 'Jumlah']}
|
|
/>
|
|
<Bar dataKey="count" name="Jumlah">
|
|
{data.map((_entry, index) => (
|
|
<Cell
|
|
key={`cell-${index}`}
|
|
fill={
|
|
index >= targetIndexStart && index <= targetIndexEnd ? '#ef4444' : '#fca5a5'
|
|
}
|
|
/>
|
|
))}
|
|
</Bar>
|
|
</BarChart>
|
|
</ResponsiveContainer>
|
|
)}
|
|
</div>
|
|
|
|
{/* Statistics Section - Below Chart */}
|
|
{stats && (
|
|
<div className="mt-4 pt-4 border-t border-gray-200">
|
|
<div className="grid grid-cols-4 gap-4 text-sm">
|
|
<div className="text-center">
|
|
<span className="block text-gray-500 mb-1">Min</span>
|
|
<span className="block font-semibold text-gray-800">
|
|
{stats.min ? formatWeight(stats.min, { maxDecimals: 2 }) : '-'}
|
|
</span>
|
|
</div>
|
|
<div className="text-center">
|
|
<span className="block text-gray-500 mb-1">Max</span>
|
|
<span className="block font-semibold text-gray-800">
|
|
{stats.max ? formatWeight(stats.max, { maxDecimals: 2 }) : '-'}
|
|
</span>
|
|
</div>
|
|
<div className="text-center">
|
|
<span className="block text-gray-500 mb-1">Rata-rata</span>
|
|
<span className="block font-semibold text-gray-800">
|
|
{stats.avg ? formatWeight(stats.avg, { maxDecimals: 2 }) : '-'}
|
|
</span>
|
|
</div>
|
|
<div className="text-center">
|
|
<span className="block text-gray-500 mb-1">Uniformitas</span>
|
|
<span className="block font-semibold text-gray-800">
|
|
{stats.uniformity ? `${stats.uniformity.toFixed(1)}%` : '-'}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default WeightDistributionChart;
|