diff --git a/app/components/ui/chart.tsx b/app/components/ui/chart.tsx index ed1074a..c003539 100644 --- a/app/components/ui/chart.tsx +++ b/app/components/ui/chart.tsx @@ -3,12 +3,29 @@ import { ArcElement, Tooltip, Legend, + CategoryScale, + LinearScale, + BarElement, + Title, type ChartOptions, + type ChartEvent, + type ActiveElement, } from 'chart.js' -import { Pie } from 'react-chartjs-2' -ChartJS.register(ArcElement, Tooltip, Legend) +import { useState } from 'react' +import { Bar, Doughnut, Pie } from 'react-chartjs-2' +ChartJS.register( + ArcElement, + CategoryScale, + LinearScale, + BarElement, + Title, + Tooltip, + Legend, +) -export const ChartPie = () => { +type HandleChartClick = (event: ChartEvent, elements: ActiveElement[]) => void + +export const UiChartPie = () => { const data = { labels: [ 'Pidana', @@ -64,3 +81,147 @@ export const ChartPie = () => { ) } + +export const UiChartBar = () => { + const yearlyData = { + labels: ['2022', '2023', '2024'], + datasets: [ + { + label: 'Total Sales', + data: [800, 950, 1200], + backgroundColor: '#1E3A8A', + }, + ], + } + + const monthlyData = { + labels: [ + 'Jan', + 'Feb', + 'Mar', + 'Apr', + 'May', + 'Jun', + 'Jul', + 'Aug', + 'Sep', + 'Oct', + 'Nov', + 'Dec', + ], + datasets: [ + { + label: 'Monthly Sales', + data: [70, 90, 750, 80, 90, 95, 75, 85, 78, 88, 95, 0], + backgroundColor: '#2E2F7C', + }, + ], + } + + const [view, setView] = useState('month') // Default tampil bulanan + const [selectedYear, setSelectedYear] = useState('') + + const handleChartClick: HandleChartClick = (event, elements) => { + if (elements.length > 0 && view === 'year') { + const clickedIndex = elements[0].index + const year = yearlyData.labels[clickedIndex] + setSelectedYear(year) + setView('month') + } + } + + const handleBackToYear = () => { + setView('year') + setSelectedYear('') + } + + const options = { + responsive: true, + maintainAspectRatio: false, + onClick: handleChartClick, + scales: { + y: { + beginAtZero: true, + max: selectedYear ? 100 : 1500, + ticks: { + stepSize: 25, + }, + }, + }, + } + + return ( +