Files
Checkmate/Client/src/Pages/PageSpeed/Details/Charts/AreaChart.jsx
T

223 lines
5.7 KiB
React
Raw Normal View History

2024-09-10 14:17:59 -04:00
import {
AreaChart,
Area,
XAxis,
Tooltip,
CartesianGrid,
ResponsiveContainer,
} from "recharts";
import { useTheme } from "@emotion/react";
import { useMemo } from "react";
2024-09-10 17:43:43 -04:00
import { Box, Stack, Typography } from "@mui/material";
import { formatDate } from "../../../../Utils/timeUtils";
2024-09-10 14:17:59 -04:00
const config = {
accessibility: {
id: "accessibility",
2024-09-10 17:43:43 -04:00
text: "accessibility",
2024-09-10 14:17:59 -04:00
color: "primary",
},
bestPractices: {
id: "bestPractices",
2024-09-10 17:43:43 -04:00
text: "best practices",
2024-09-10 14:17:59 -04:00
color: "warning",
},
performance: {
id: "performance",
2024-09-10 17:43:43 -04:00
text: "performance",
2024-09-10 14:17:59 -04:00
color: "success",
},
seo: {
id: "seo",
2024-09-10 17:43:43 -04:00
text: "SEO",
2024-09-10 14:17:59 -04:00
color: "unresolved",
},
};
2024-09-10 17:43:43 -04:00
const CustomToolTip = ({ active, payload, label }) => {
const theme = useTheme();
if (active && payload && payload.length) {
return (
<Box
sx={{
backgroundColor: theme.palette.background.main,
border: 1,
borderColor: theme.palette.border.dark,
borderRadius: theme.shape.borderRadius,
py: theme.spacing(2),
px: theme.spacing(4),
}}
>
<Typography
sx={{
color: theme.palette.text.tertiary,
fontSize: 12,
fontWeight: 500,
}}
>
{formatDate(new Date(label))}
</Typography>
{Object.keys(config).map((key) => {
const { color } = config[key];
const dotColor = theme.palette[color].main;
return (
<Stack
key={`${key}-tooltip`}
direction="row"
alignItems="center"
gap={theme.spacing(3)}
mt={theme.spacing(1)}
sx={{
"& span": {
color: theme.palette.text.tertiary,
fontSize: 11,
fontWeight: 500,
},
}}
>
<Box
width={theme.spacing(4)}
height={theme.spacing(4)}
backgroundColor={dotColor}
sx={{ borderRadius: "50%" }}
/>
<Typography
component="span"
textTransform="capitalize"
sx={{ opacity: 0.8 }}
>
{config[key].text}
</Typography>{" "}
<Typography component="span">
{payload[0].payload[key]}
</Typography>
</Stack>
);
})}
</Box>
);
}
return null;
};
const processDataWithGaps = (data, interval) => {
if (data.length === 0) return [];
let formattedData = [];
let last = new Date(data[0].createdAt).getTime();
// Helper function to add a null entry
const addNullEntry = (timestamp) => {
formattedData.push({
accessibility: "N/A",
bestPractices: "N/A",
performance: "N/A",
seo: "N/A",
createdAt: timestamp,
2024-09-10 14:17:59 -04:00
});
};
2024-09-10 17:43:43 -04:00
data.forEach((entry) => {
const current = new Date(entry.createdAt).getTime();
if (current - last > interval * 2) {
// Insert null entries for each interval
let temp = last + interval;
while (temp < current) {
addNullEntry(new Date(temp).toISOString());
temp += interval;
}
}
formattedData.push(entry);
last = current;
});
2024-09-10 14:17:59 -04:00
2024-09-10 17:43:43 -04:00
return formattedData;
};
const PagespeedDetailsAreaChart = ({ data, interval }) => {
2024-09-10 14:17:59 -04:00
const theme = useTheme();
2024-09-10 17:43:43 -04:00
const memoizedData = useMemo(
() => processDataWithGaps(data, interval),
[data]
);
2024-09-10 14:17:59 -04:00
return (
2024-09-10 17:43:43 -04:00
<ResponsiveContainer width="100%" minWidth={25} height={215}>
2024-09-10 14:17:59 -04:00
<AreaChart
width="100%"
height="100%"
data={memoizedData}
margin={{ top: 10 }}
>
<CartesianGrid
stroke={theme.palette.border.light}
strokeWidth={1}
strokeOpacity={1}
fill="transparent"
vertical={false}
/>
<XAxis
stroke={theme.palette.border.dark}
dataKey="createdAt"
2024-09-10 17:43:43 -04:00
tickFormatter={(timestamp) =>
formatDate(new Date(timestamp), {
year: undefined,
month: undefined,
day: undefined,
})
}
2024-09-10 14:17:59 -04:00
tick={{
2024-09-10 17:43:43 -04:00
fontSize: 11,
2024-09-10 14:17:59 -04:00
fontWeight: 100,
opacity: 0.8,
stroke: theme.palette.text.tertiary,
}}
tickLine={false}
2024-09-10 17:43:43 -04:00
minTickGap={20}
2024-09-10 14:17:59 -04:00
height={18}
2024-09-10 17:43:43 -04:00
interval="preserveEnd"
/>
<Tooltip
cursor={{ stroke: theme.palette.border.light }}
content={<CustomToolTip />}
2024-09-10 14:17:59 -04:00
/>
<defs>
{Object.values(config).map(({ id, color }) => {
const startColor = theme.palette[color].main;
const endColor = theme.palette[color].light;
return (
<linearGradient id={id} x1="0" y1="0" x2="0" y2="1" key={id}>
<stop offset="0%" stopColor={startColor} stopOpacity={0.8} />
<stop offset="100%" stopColor={endColor} stopOpacity={0} />
</linearGradient>
);
})}
</defs>
{Object.keys(config).map((key) => {
const { color } = config[key];
const strokeColor = theme.palette[color].main;
2024-09-10 17:43:43 -04:00
const bgColor = theme.palette.background.main;
2024-09-10 14:17:59 -04:00
return (
<Area
2024-09-10 17:43:43 -04:00
connectNulls
2024-09-10 14:17:59 -04:00
key={key}
dataKey={key}
stackId={1}
stroke={strokeColor}
strokeWidth={1.5}
fill={`url(#${config[key].id})`}
2024-09-10 17:43:43 -04:00
activeDot={{ stroke: bgColor, fill: strokeColor, r: 4.5 }}
2024-09-10 14:17:59 -04:00
/>
);
})}
</AreaChart>
</ResponsiveContainer>
);
};
export default PagespeedDetailsAreaChart;