-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTxGraph.tsx
105 lines (95 loc) · 3.02 KB
/
TxGraph.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import ActivityCalendar from "react-activity-calendar";
import { Tooltip as MuiTooltip } from "@mui/material";
const getLevel = (count: number): number => {
if (count >= 15) return 4;
if (count >= 5) return 3;
if (count >= 2) return 2;
if (count >= 1) return 1;
return 0;
};
function generateYearData(inputData: { date: string; count: number }[]): { date: string; count: number; level: number }[] {
const yearData: { date: string; count: number; level: number }[] = [];
let startDate = new Date(); // Start date: today
let endDate = new Date(); // End date: one year backward from today
endDate.setFullYear(endDate.getFullYear() - 1);
// Ensure proper date loop
if (startDate < endDate) {
[startDate, endDate] = [endDate, startDate];
}
for (let d = new Date(startDate); d >= endDate; d.setDate(d.getDate() - 1)) {
const dateString = d.toISOString().split("T")[0];
const existingData = inputData.find((item) => item.date === dateString);
const count = existingData ? existingData.count : 0;
yearData.push({
date: dateString,
count,
level: getLevel(count),
});
}
yearData.sort((a, b) => new Date(a.date).getTime() - new Date(b.date).getTime());
return yearData;
}
export default function TxGraph({
data,
}: {
data: { date: string; count: number }[];
}) {
const yearData = generateYearData(data);
console.log(yearData);
return (
<Card className="w-full bg-white">
<CardHeader>
<CardTitle className="text-xl sm:text-2xl font-bold text-gray-800">
Transaction Activity
</CardTitle>
</CardHeader>
<CardContent className="overflow-x-auto">
<ActivityCalendar
data={yearData.map((item) => ({
date: item.date,
count: item.count,
level: item.level,
}))}
labels={{
months: [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
],
weekdays: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
}}
renderBlock={(block, activity) => (
<MuiTooltip
title={`${activity.count} Transactions on ${activity.date}`}
className="cursor-pointer"
>
{block}
</MuiTooltip>
)}
renderColorLegend={(block, level) => (
<MuiTooltip title={`Level: ${level}`}>{block}</MuiTooltip>
)}
blockSize={10}
blockMargin={2}
fontSize={12}
theme={{
light: ["#EAEDF0", "#C6E48B", "#7BC96F", "#239A3B", "#196127"],
dark: ["#EAEDF0", "#C6E48B", "#7BC96F", "#239A3B", "#196127"],
}}
maxLevel={4}
hideTotalCount={true}
/>
</CardContent>
</Card>
);
}