web-humas-fe/components/main/dashboard/chart/column-chart.tsx

235 lines
5.9 KiB
TypeScript
Raw Normal View History

"use client";
import React, { Component, useEffect, useState } from "react";
import ReactApexChart from "react-apexcharts";
import dummyData from "../../../../const/dummy.json";
2025-05-04 07:14:12 +00:00
import { getStatisticMonthly } from "@/services/article";
2024-04-19 13:26:27 +00:00
type WeekData = {
week: number;
days: number[];
total: number;
};
type RemainingDays = {
days: number[];
total: number;
};
function processMonthlyData(count: number[]): {
weeks: WeekData[];
remaining_days: RemainingDays;
} {
const weeks: WeekData[] = [];
let weekIndex = 1;
for (let i = 0; i < count.length; i += 7) {
const weekData = count.slice(i, i + 7);
weeks.push({
week: weekIndex,
days: weekData,
total: weekData.reduce((sum, day) => sum + day, 0),
});
weekIndex++;
}
const remainingDays: RemainingDays = {
days: count.length % 7 === 0 ? [] : count.slice(-count.length % 7),
total: count.slice(-count.length % 7).reduce((sum, day) => sum + day, 0),
};
return {
weeks,
remaining_days: remainingDays,
};
2024-04-19 13:26:27 +00:00
}
2025-06-23 10:57:30 +00:00
function getRangeAcrossMonths(
data: any[],
startMonth: number,
startDay: number,
endMonth: number,
endDay: number
) {
const view: number[] = [];
const comment: number[] = [];
const share: number[] = [];
const labels: string[] = [];
const sortedData = data.sort((a, b) => a.month - b.month);
console.log("sorted data", sortedData);
for (const monthData of sortedData) {
const { month, view: v, comment: c, share: s } = monthData;
if (month < startMonth || month > endMonth) continue;
let startIndex = 0;
let endIndex = v.length - 1;
if (month === startMonth) startIndex = startDay - 1;
if (month === endMonth) endIndex = endDay - 1;
for (let i = startIndex; i <= endIndex; i++) {
view.push(v[i]);
comment.push(c[i]);
share.push(s[i]);
const label = `${(i + 1).toString().padStart(2, "0")} - ${month
.toString()
.padStart(2, "0")} `;
labels.push(label);
}
}
return { view, comment, share, labels };
}
const ApexChartColumn = (props: {
type: string;
date: string;
view: string[];
2025-06-23 10:57:30 +00:00
range: { start: any; end: any };
}) => {
2025-06-23 10:57:30 +00:00
const { date, type, view, range } = props;
const [categories, setCategories] = useState<string[]>([]);
const [series, setSeries] = useState<{ name: string; data: number[] }[]>([]);
2025-02-17 07:21:28 +00:00
const [seriesComment, setSeriesComment] = useState<number[]>([]);
const [seriesView, setSeriesView] = useState<number[]>([]);
const [seriesShare, setSeriesShare] = useState<number[]>([]);
2025-06-23 10:57:30 +00:00
const [years, setYear] = useState("");
const [datas, setDatas] = useState<any>([]);
useEffect(() => {
initFetch();
2025-06-23 10:57:30 +00:00
}, [date, type, view, range]);
const initFetch = async () => {
const splitDate = date.split(" ");
2025-06-23 10:57:30 +00:00
const splitDateDaily = String(range.start.year);
let data = [];
console.log(
"aaawwww",
type === "monthly" && splitDate[1] === years,
type === "daily" && splitDateDaily === years
);
if (
(type === "monthly" && splitDate[1] === years) ||
(type === "daily" && splitDateDaily === years)
) {
console.log("if", datas);
data = datas;
} else {
const res = await getStatisticMonthly(
type === "monthly" ? splitDate[1] : splitDateDaily
);
data = res?.data?.data;
setDatas(data);
}
console.log("datas", data);
2025-02-17 07:21:28 +00:00
const getDatas = data?.find(
(a: any) =>
a.month == Number(splitDate[0]) && a.year === Number(splitDate[1])
);
if (getDatas) {
2025-02-17 07:21:28 +00:00
const temp1 = processMonthlyData(getDatas?.comment);
const temp2 = processMonthlyData(getDatas?.view);
const temp3 = processMonthlyData(getDatas?.share);
if (type == "weekly") {
2025-02-17 07:21:28 +00:00
setSeriesComment(
temp1.weeks.map((list) => {
return list.total;
})
);
setSeriesView(
temp2.weeks.map((list) => {
return list.total;
})
);
setSeriesShare(
temp3.weeks.map((list) => {
return list.total;
})
);
} else {
2025-02-17 07:21:28 +00:00
setSeriesComment(getDatas.comment);
setSeriesView(getDatas.view);
setSeriesShare(getDatas.share);
}
2025-06-23 10:57:30 +00:00
if (type === "daily") {
const mappedData = getRangeAcrossMonths(
data,
range.start.month,
range.start.day,
range.end.month,
range.end.day
);
setSeriesComment(mappedData.comment);
setSeriesView(mappedData.view);
setSeriesShare(mappedData.share);
setCategories(mappedData.labels);
}
2025-06-23 10:57:30 +00:00
// if (type === "weekly") {
// const category = [];
// for (let i = 1; i <= temp1.weeks.length; i++) {
// category.push(`Week ${i}`);
// }
// setCategories(category);
// }
} else {
2025-02-17 07:21:28 +00:00
setSeriesComment([]);
2024-04-19 13:26:27 +00:00
}
2025-06-23 10:57:30 +00:00
setYear(type === "monthly" ? splitDate[1] : splitDateDaily);
};
useEffect(() => {
const temp = [
{
2025-02-17 07:21:28 +00:00
name: "Comment",
data: view.includes("comment") ? seriesComment : [],
},
{
name: "View",
data: view.includes("view") ? seriesView : [],
},
{
name: "Share",
data: view.includes("share") ? seriesShare : [],
},
];
setSeries(temp);
2025-02-17 07:21:28 +00:00
}, [view, seriesShare, seriesView, seriesComment]);
return (
<div className="h-full">
<div id="chart" className="h-full">
<ReactApexChart
options={{
chart: {
height: "100%",
type: "area",
},
stroke: {
curve: "smooth",
},
dataLabels: {
enabled: false,
},
xaxis: {
2025-06-23 10:57:30 +00:00
categories: type == "daily" ? categories : [],
},
}}
series={series}
type="area"
height="100%"
/>
</div>
<div id="html-dist"></div>
</div>
);
};
2024-04-19 13:26:27 +00:00
export default ApexChartColumn;