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

112 lines
3.6 KiB
TypeScript

import React, { useState, useMemo } from 'react';
import {
LineChart,
Line,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
ResponsiveContainer,
} from 'recharts';
import { BehaviorDataPoint } from '../../types.ts';
import { useAppContext } from '../../context/AppContext.tsx';
import { generateBehaviorChartData } from '../../utils/chartDataUtils.ts';
interface TimeRangeButtonProps {
active: boolean;
onClick: () => void;
children: React.ReactNode;
}
const TimeRangeButton: React.FC<TimeRangeButtonProps> = ({ active, onClick, children }) => (
<button
onClick={onClick}
className={`px-3 py-1 text-xs font-bold rounded-md transition-all duration-200 ${
active ? 'bg-white text-red-600 shadow-sm' : 'text-gray-600 hover:bg-gray-200/50'
}`}
>
{children}
</button>
);
interface BehaviorTrendChartProps {
data: BehaviorDataPoint[];
}
const BehaviorTrendChart: React.FC<BehaviorTrendChartProps> = ({ data }) => {
const { cycleData } = useAppContext();
const [timeRange, setTimeRange] = useState<'24h' | '7d' | '30d' | 'cycle'>('24h');
const chartData = useMemo(() => {
return generateBehaviorChartData(data, timeRange, cycleData);
}, [data, timeRange, cycleData]);
const chartTitle = {
'24h': 'Tren Per Jam (1 Hari)',
'7d': 'Tren Harian (1 Minggu)',
'30d': 'Tren Harian (1 Bulan)',
cycle: 'Tren Mingguan (Siklus)',
}[timeRange];
return (
<div className="bg-white p-6 rounded-xl shadow-sm">
<div className="flex flex-col sm:flex-row justify-between sm:items-center mb-4 gap-3">
<h2 className="text-xl font-bold text-gray-800">{chartTitle}</h2>
<div className="flex bg-gray-100 rounded-lg p-1 self-start sm:self-center">
<TimeRangeButton active={timeRange === '24h'} onClick={() => setTimeRange('24h')}>
1 Hari
</TimeRangeButton>
<TimeRangeButton active={timeRange === '7d'} onClick={() => setTimeRange('7d')}>
1 Minggu
</TimeRangeButton>
<TimeRangeButton active={timeRange === '30d'} onClick={() => setTimeRange('30d')}>
1 Bulan
</TimeRangeButton>
<TimeRangeButton active={timeRange === 'cycle'} onClick={() => setTimeRange('cycle')}>
Siklus
</TimeRangeButton>
</div>
</div>
<div className="h-64">
<ResponsiveContainer width="100%" height="100%">
<LineChart data={chartData} margin={{ top: 5, right: 20, left: -10, bottom: 5 }}>
<CartesianGrid strokeDasharray="3 3" stroke="#e0e0e0" />
<XAxis dataKey="date" fontSize={12} tick={{ fill: '#6b7280' }} />
<YAxis yAxisId="left" fontSize={12} unit="%" domain={[0, 100]} />
<Tooltip
contentStyle={{
backgroundColor: 'rgba(255, 255, 255, 0.9)',
border: '1px solid #ccc',
borderRadius: '8px',
}}
formatter={(value: number) => [`${value.toFixed(1)}%`, '']}
/>
<Legend wrapperStyle={{ fontSize: '14px' }} verticalAlign="top" align="right" />
<Line
yAxisId="left"
type="monotone"
dataKey="activity"
stroke="#3b82f6"
strokeWidth={2}
name="Aktivitas"
dot={false}
/>
<Line
yAxisId="left"
type="monotone"
dataKey="stress"
stroke="#ef4444"
strokeWidth={2}
name="Stres"
dot={false}
/>
</LineChart>
</ResponsiveContainer>
</div>
</div>
);
};
export default BehaviorTrendChart;