"use client"; import { useEffect, useState } from "react"; import ReactApexChart from "react-apexcharts"; import dummy from "../../../../const/dummy.json"; import { getStatisticUsersMonthly } from "@/services/article"; function getRangeAcrossMonths( data: any[], startYear: number, startMonth: number, startDay: number, endYear: number, endMonth: number, endDay: number ) { const labels: string[] = []; const users: { name: string; data: number[] }[] = []; const sortedData = [...data].sort( (a, b) => a.year - b.year || a.month - b.month ); for (const monthData of sortedData) { const { year, month, user_levels: u } = monthData; if (year < startYear || (year === startYear && month < startMonth)) continue; if (year > endYear || (year === endYear && month > endMonth)) continue; let startIndex = 0; let endIndex = u[0].data.length - 1; if (year === startYear && month === startMonth) { startIndex = startDay - 1; } if (year === endYear && month === endMonth) { endIndex = endDay - 1; } for (let j = 0; j < u.length; j++) { const now = u[j].data; const temp: number[] = []; for (let i = startIndex; i <= endIndex; i++) { if (now[i] == null) continue; temp.push(now[i]); if (j === 0) { labels.push( `${String(i + 1).padStart(2, "0")}-${String(month).padStart( 2, "0" )}-${year}` ); } } const existing = users.find((item) => item.name === u[j].name); if (existing) { existing.data.push(...temp); } else { users.push({ name: u[j].name, data: temp }); } } } return { users, labels }; } const ApexMultiLineChart = (props: { type: string; date: string; range: { start: any; end: any }; }) => { const { date, type, range } = props; const [datas, setDatas] = useState([]); const [years, setYear] = useState(""); const [series, setSeries] = useState([]); const [categories, setCategories] = useState([]); useEffect(() => { initFetch(); }, [date, type, range.start, range.end]); const initFetch = async () => { const splitDate = date.split(" "); const splitDateDaily = String(range.start.year); const splitDateDailyEnd = String(range.end.year); const isAccrossYear = splitDateDaily !== splitDateDailyEnd; let data: any = []; if ( (type === "monthly" && splitDate[1] === years) || (type === "daily" && splitDateDaily === years && !isAccrossYear) ) { data = datas; } else { if (isAccrossYear && type === "daily") { const resStart = await getStatisticUsersMonthly(splitDateDaily); const resEnd = await getStatisticUsersMonthly(splitDateDailyEnd); data = [...resStart?.data?.data, ...resEnd?.data?.data]; setDatas(data); } else { const res = await getStatisticUsersMonthly( type === "monthly" ? splitDate[1] : splitDateDaily ); data = res?.data?.data; setDatas(data); } } if (type === "daily") { const mappedData = getRangeAcrossMonths( data, range.start.year, range.start.month, range.start.day, range.end.year, range.end.month, range.end.day ); setSeries(mappedData.users); setCategories(mappedData.labels); } if (type === "monthly") { const getDatas = data?.find( (a: any) => a.month == Number(splitDate[0]) && a.year === Number(splitDate[1]) ); if (getDatas) { setSeries(getDatas.user_levels); } else { setSeries([]); } } setYear(type === "monthly" ? splitDate[1] : splitDateDaily); }; return (
); }; export default ApexMultiLineChart;