142 lines
4.6 KiB
TypeScript
142 lines
4.6 KiB
TypeScript
import React from 'react';
|
|
import {
|
|
BarChart,
|
|
Bar,
|
|
XAxis,
|
|
YAxis,
|
|
CartesianGrid,
|
|
Tooltip,
|
|
ResponsiveContainer,
|
|
Cell,
|
|
} from 'recharts';
|
|
import { DeviceWeightData } from '../../types.ts';
|
|
import { formatWeight } from '../../utils/weightUtils';
|
|
|
|
interface DeviceWeightComparisonChartProps {
|
|
data: DeviceWeightData[];
|
|
selectedDate: string;
|
|
isLoading?: boolean;
|
|
}
|
|
|
|
const DeviceWeightComparisonChart: React.FC<DeviceWeightComparisonChartProps> = ({
|
|
data,
|
|
isLoading = false,
|
|
}) => {
|
|
const hasNoData = !data || data.length === 0;
|
|
|
|
// Custom tooltip
|
|
const CustomTooltip = ({ active, payload }: any) => {
|
|
if (active && payload && payload.length) {
|
|
const device = payload[0].payload as DeviceWeightData;
|
|
|
|
return (
|
|
<div className="bg-white p-4 border border-gray-300 rounded shadow-lg">
|
|
<p className="font-bold text-gray-800">{device.deviceName}</p>
|
|
<div className="mt-2 space-y-1">
|
|
<p className="text-sm">
|
|
<span className="font-semibold">BW:</span> {formatWeight(device.avgWeight)}
|
|
</p>
|
|
<p className="text-sm">
|
|
<span className="font-semibold">Jumlah Ayam:</span> {device.chickenCount} ekor
|
|
</p>
|
|
<p className="text-sm">
|
|
<span className="font-semibold">Status:</span>{' '}
|
|
<span className={device.isOnline ? 'text-green-600' : 'text-red-600'}>
|
|
{device.isOnline ? 'Online' : 'Offline'}
|
|
</span>
|
|
</p>
|
|
{!device.isOnline && (
|
|
<p className="text-xs text-gray-500">
|
|
Last online: {new Date(device.lastOnline).toLocaleString('id-ID')}
|
|
</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
// Custom label for bars
|
|
const CustomBarLabel = (props: any) => {
|
|
const { x, y, width, height, index } = props;
|
|
const device = data[index];
|
|
if (!device) return null;
|
|
return (
|
|
<text
|
|
x={x + width + 5}
|
|
y={y + height / 2}
|
|
fill="#4b5563"
|
|
textAnchor="start"
|
|
dominantBaseline="middle"
|
|
fontSize={12}
|
|
>
|
|
{device.chickenCount} ekor
|
|
</text>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<div className="bg-white p-6 rounded-xl shadow-sm">
|
|
<div className="flex justify-between items-center mb-4">
|
|
<h2 className="text-xl font-bold">Perbandingan Berat per Sensor</h2>
|
|
</div>
|
|
<div className="h-80">
|
|
{isLoading ? (
|
|
<div className="flex items-center justify-center h-full">
|
|
<div className="text-center">
|
|
<i className="fa-solid fa-spinner fa-spin text-4xl text-red-600 mb-4"></i>
|
|
<p className="text-gray-600 text-lg">Memuat data sensor...</p>
|
|
</div>
|
|
</div>
|
|
) : 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">
|
|
Data sensor belum tersedia untuk tanggal ini
|
|
</p>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
<ResponsiveContainer width="100%" height="100%">
|
|
<BarChart
|
|
data={data}
|
|
layout="vertical"
|
|
margin={{ top: 5, right: 80, left: 20, bottom: 5 }}
|
|
>
|
|
<CartesianGrid strokeDasharray="3 3" stroke="#e0e0e0" />
|
|
<XAxis
|
|
type="number"
|
|
tickFormatter={(value) => `${value}g`}
|
|
label={{ value: 'BW (gram)', position: 'insideBottom', offset: -5 }}
|
|
/>
|
|
<YAxis type="category" dataKey="deviceName" width={120} />
|
|
<Tooltip content={<CustomTooltip />} />
|
|
<Bar dataKey="avgWeight" label={<CustomBarLabel />} radius={[0, 8, 8, 0]}>
|
|
{data.map((device, index) => (
|
|
<Cell key={`cell-${index}`} fill={device.isOnline ? '#dc2626' : '#9ca3af'} />
|
|
))}
|
|
</Bar>
|
|
</BarChart>
|
|
</ResponsiveContainer>
|
|
)}
|
|
</div>
|
|
{!hasNoData && (
|
|
<div className="mt-4 flex items-center justify-center space-x-6 text-sm text-gray-600">
|
|
<div className="flex items-center space-x-2">
|
|
<div className="w-4 h-4 bg-red-600 rounded"></div>
|
|
<span>Online</span>
|
|
</div>
|
|
<div className="flex items-center space-x-2">
|
|
<div className="w-4 h-4 bg-gray-400 rounded"></div>
|
|
<span>Offline</span>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default DeviceWeightComparisonChart;
|