"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[], startMonth: number, startDay: number, endMonth: number, endDay: number ) { const labels: string[] = []; const users: { name: string; data: number[] }[] = []; const sortedData = data.sort((a, b) => a.month - b.month); console.log("sorted,data", sortedData); for (const monthData of sortedData) { const { month, user_levels: u } = monthData; if (month < startMonth || month > endMonth) continue; console.log("uuu", month, startMonth, endMonth, u.length); let startIndex = 0; let endIndex = u[0].data.length - 1; if (month === startMonth) startIndex = startDay - 1; if (month === endMonth) endIndex = endDay - 1; console.log("start,eend", startIndex, endIndex, month); for (let j = 0; j < u.length; j++) { const now = u[j].data; // console.log("u.j", now); const temp = []; for (let i = startIndex; i <= endIndex; i++) { temp.push(now[i]); if (j == 0) { const label = `${(i + 1).toString().padStart(2, "0")} - ${month .toString() .padStart(2, "0")}`; labels.push(label); } } const existing = users.find((item) => item.name === u[j].name); if (existing) { existing.data.push(...temp); // gabungkan data } else { users.push({ name: u[j].name, data: temp }); // tambahkan baru } } } console.log("users", users); 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); console.log("split", splitDate); console.log("daily", splitDateDaily); let data: any = []; if ( (type === "monthly" && splitDate[1] === years) || (type === "daily" && splitDateDaily === years) ) { data = datas; } else { const res = await getStatisticUsersMonthly( type === "monthly" ? splitDate[1] : splitDateDaily ); console.log("fetch", res?.data?.data); data = res?.data?.data; // data = dummy.data; setDatas(data); } if (type === "daily") { const mappedData = getRangeAcrossMonths( data, range.start.month, range.start.day, range.end.month, range.end.day ); setSeries(mappedData.users); setCategories(mappedData.labels); } if (type === "monthly") { console.log("daaa", data); const getDatas = data?.find( (a: any) => a.month == Number(splitDate[0]) && a.year === Number(splitDate[1]) ); if (getDatas) { console.log("datanya", getDatas.user_levels); setSeries(getDatas.user_levels); } else { setSeries([]); } } setYear(type === "monthly" ? splitDate[1] : splitDateDaily); }; return (
); }; export default ApexMultiLineChart;