Files
Checkmate/Client/src/Components/Charts/CustomGauge/index.jsx
T

118 lines
3.0 KiB
React
Raw Normal View History

2024-11-19 11:42:42 +08:00
import { useTheme } from "@emotion/react";
2024-11-20 12:13:46 +08:00
import { useEffect, useState, useMemo } from "react";
2024-11-19 11:42:42 +08:00
import { Box, Typography } from "@mui/material";
import PropTypes from "prop-types";
import "./index.css";
2024-11-28 17:54:04 -05:00
const MINIMUM_VALUE = 0;
const MAXIMUM_VALUE = 100;
2024-11-19 11:42:42 +08:00
/**
* A Performant SVG based circular gauge
*
* @component
* @param {Object} props - Component properties
* @param {number} [props.progress=0] - Progress percentage (0-100)
* @param {number} [props.radius=60] - Radius of the gauge circle
* @param {number} [props.strokeWidth=15] - Width of the gauge stroke
2024-11-28 17:54:04 -05:00
* @param {number} [props.threshold=50] - Threshold for color change
2024-11-19 11:42:42 +08:00
*
* @example
* <CustomGauge
* progress={75}
* radius={50}
* strokeWidth={10}
2024-11-28 17:54:04 -05:00
* threshold={50}
2024-11-19 11:42:42 +08:00
* />
*
* @returns {React.ReactElement} Rendered CustomGauge component
*/
2024-11-28 17:54:04 -05:00
const CustomGauge = ({ progress = 0, radius = 70, strokeWidth = 15, threshold = 50 }) => {
const theme = useTheme();
2024-11-19 11:42:42 +08:00
// Calculate the length of the stroke for the circle
2024-11-20 12:13:46 +08:00
const { circumference, totalSize, strokeLength } = useMemo(
() => ({
circumference: 2 * Math.PI * radius,
totalSize: radius * 2 + strokeWidth * 2,
strokeLength: (progress / 100) * (2 * Math.PI * radius),
}),
[radius, strokeWidth, progress]
);
2024-11-19 11:42:42 +08:00
// Handle initial animation
2024-11-28 17:54:04 -05:00
const [offset, setOffset] = useState(circumference);
2024-11-19 11:42:42 +08:00
useEffect(() => {
setOffset(circumference);
const timer = setTimeout(() => {
setOffset(circumference - strokeLength);
}, 100);
return () => clearTimeout(timer);
}, [progress, circumference, strokeLength]);
2024-11-28 17:54:04 -05:00
const progressWithinRange = Math.max(MINIMUM_VALUE, Math.min(progress, MAXIMUM_VALUE));
const fillColor =
progressWithinRange > threshold
2025-01-21 10:57:34 -08:00
? theme.palette.error.lowContrast // CAIO_REVIEW
: theme.palette.accent.main; // CAIO_REVIEW
2024-11-28 17:54:04 -05:00
2024-11-19 11:42:42 +08:00
return (
<Box
className="radial-chart"
width={radius}
height={radius}
>
<svg
viewBox={`0 0 ${totalSize} ${totalSize}`}
width={radius}
height={radius}
>
<circle
className="radial-chart-base"
2025-01-21 10:57:34 -08:00
stroke={theme.palette.primary.lowContrast} // CAIO_REVIEW
2024-11-19 11:42:42 +08:00
strokeWidth={strokeWidth}
fill="none"
cx={totalSize / 2} // Center the circle
cy={totalSize / 2} // Center the circle
r={radius}
/>
<circle
className="radial-chart-progress"
2024-11-28 17:54:04 -05:00
stroke={fillColor}
2024-11-19 11:42:42 +08:00
strokeWidth={strokeWidth}
strokeDasharray={`${circumference} ${circumference}`}
strokeDashoffset={offset}
strokeLinecap="round"
fill="none"
cx={totalSize / 2}
cy={totalSize / 2}
r={radius}
/>
</svg>
<Typography
className="radial-chart-text"
style={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%, -50%)",
...theme.typography.body2,
fill: theme.typography.body2.color,
}}
>
2024-11-28 17:54:04 -05:00
{`${progressWithinRange.toFixed(1)}%`}
2024-11-19 11:42:42 +08:00
</Typography>
</Box>
);
};
export default CustomGauge;
CustomGauge.propTypes = {
progress: PropTypes.number,
radius: PropTypes.number,
strokeWidth: PropTypes.number,
2024-11-28 17:54:04 -05:00
threshold: PropTypes.number,
2024-11-19 11:42:42 +08:00
};