diff --git a/src/components/BarChart.js b/src/components/BarChart.js index 9675955..f4ca6eb 100644 --- a/src/components/BarChart.js +++ b/src/components/BarChart.js @@ -1,126 +1,149 @@ -import React, { useRef, useEffect } from 'react'; -import ReactApexChart from 'react-apexcharts'; +import React, { useState } from "react"; +import Chart from "react-apexcharts"; +import styles from "./BarChart.module.css"; // Import the CSS module -// Colors palette -const colors = [ - "#D0E14F", "#B2B83D", "#A9C96E", "#A8B64E", - "#FF6347", "#FF4500", "#FF8C00", "#FF7F50", - "#1E90FF", "#FF00FF", "#32CD32", - "#00CED1", "#FFD700", "#FF1493", "#8A2BE2", - "#FFDAB9", "#F0E68C", "#8B4513", "#4B0082", "#C71585", - "#FFB6C1", "#E6E6FA", "#98FB98", "#F0FFF0", "#D3D3D3" -]; +const BarChart = ({ transactionGraph, colors }) => { + const [selectedIndex, setSelectedIndex] = useState(-1); -const SimpleLineChart = ({ Data }) => { - const lastMonthRef = useRef(''); - const lastYearRef = useRef(''); - const usedColors = useRef([]); // Keep track of used colors + const processData = (graphData) => { + if (!graphData) return null; + return graphData.map((dayData) => { + const categories = [ + "0-3", + "3-6", + "6-9", + "9-12", + "12-15", + "15-18", + "18-21", + "21-24", + ]; - useEffect(() => { - usedColors.current = []; - }, [Data]); + const seriesData = [ + dayData.hour0To3Transactions.reduce((acc, t) => acc + t.sold, 0), + dayData.hour3To6Transactions.reduce((acc, t) => acc + t.sold, 0), + dayData.hour6To9Transactions.reduce((acc, t) => acc + t.sold, 0), + dayData.hour9To12Transactions.reduce((acc, t) => acc + t.sold, 0), + dayData.hour12To15Transactions.reduce((acc, t) => acc + t.sold, 0), + dayData.hour15To18Transactions.reduce((acc, t) => acc + t.sold, 0), + dayData.hour18To21Transactions.reduce((acc, t) => acc + t.sold, 0), + dayData.hour21To24Transactions.reduce((acc, t) => acc + t.sold, 0), + ]; - // Ensure Data is not null or undefined - const transformedData = (Data?.length ? Data.reduce((acc, { date, materialId, priceAtp, stockDifference, name }) => { - function isSameDay(date1, date2) { - const d1 = new Date(date1); - const d2 = new Date(date2); - - return d1.getFullYear() === d2.getFullYear() && - d1.getMonth() === d2.getMonth() && - d1.getDate() === d2.getDate(); - } - - const existingEntry = acc.find(d => isSameDay(d.date, date)); - - if (existingEntry) { - existingEntry[name] = (existingEntry[name] || 0) + (priceAtp * stockDifference); - } else { - acc.push({ - date, - materialId, - name, - [name]: priceAtp * stockDifference - }); - } - return acc; - }, []) : []); - - const sortedData = [...transformedData].sort((a, b) => new Date(a.date) - new Date(b.date)); - const oldestDate = sortedData[0]?.date; - - const uniqueMaterials = Array.from(new Set(Data?.map(item => item.name) || [])); - const uniqueDates = Array.from(new Set(transformedData.map(item => item.date))); - - const getNextColor = () => { - const randomIndex = Math.floor(Math.random() * colors.length); - let color = null; - for (let i = 0; i < colors.length; i++) { - const index = (randomIndex + i) % colors.length; - if (!usedColors.current.includes(colors[index])) { - color = colors[index]; - usedColors.current.push(color); - break; - } - } - return color || '#000000'; + return { + date: new Date(dayData.date).toLocaleDateString(), + categories, + series: [ + { + name: `Transactions on ${new Date(dayData.date).toLocaleDateString()}`, + data: seriesData, + }, + ], + }; + }); }; - // Prepare data for ApexCharts - const series = uniqueMaterials.map((material) => { - return { - name: material, - data: transformedData.map(item => item[material] || 0) - }; - }); + const chartData = processData(transactionGraph); - const options = { - chart: { - type: 'line', - height: '100%', - zoom: { - enabled: true - } - }, - xaxis: { - categories: uniqueDates, - labels: { - style: { - fontSize: '12px', - } - } - }, - colors: uniqueMaterials.map(() => getNextColor()), // Assign unique colors for each material - legend: { - position: 'top', - horizontalAlign: 'center', - itemMargin: { - horizontal: 10, - vertical: 0 + let globalMax = null; + if (chartData) + globalMax = chartData.reduce( + (max, data) => { + const localMax = Math.max(...data.series[0].data); + return localMax > max ? localMax : max; }, - labels: { - colors: '#3498db', // Set legend text color - }, - }, - tooltip: { - theme: 'light', - style: { - fontSize: '14px', - color: '#3498db' - } - } + 0 + ); + + const formatDate = (dateString) => { + const date = new Date(dateString); + const monthNames = [ + "Jan", "Feb", "Mar", "Apr", "Mei", "Jun", "Jul", "Agu", "Sep", "Okt", "Nov", "Des" + ]; + const month = monthNames[date.getMonth()]; + const day = date.getDate(); + return { month, day }; }; return ( -