feat: updating CustomGauge

This commit is contained in:
Caio Cabral committed 2024-11-28 17:54:04 -05:00
1 parent fdcece7248
commit 221ec01543
2 files changed
+22 -15

No files matched your search

@@ -4,6 +4,9 @@ import { Box, Typography } from "@mui/material";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
import "./index.css"; import "./index.css";
const MINIMUM_VALUE = 0;
const MAXIMUM_VALUE = 100;
/** /**
* A Performant SVG based circular gauge * A Performant SVG based circular gauge
* *
@@ -11,25 +14,21 @@ import "./index.css";
* @param {Object} props - Component properties * @param {Object} props - Component properties
* @param {number} [props.progress=0] - Progress percentage (0-100) * @param {number} [props.progress=0] - Progress percentage (0-100)
* @param {number} [props.radius=60] - Radius of the gauge circle * @param {number} [props.radius=60] - Radius of the gauge circle
* @param {string} [props.color="#000000"] - Color of the progress stroke
* @param {number} [props.strokeWidth=15] - Width of the gauge stroke * @param {number} [props.strokeWidth=15] - Width of the gauge stroke
* @param {number} [props.threshold=50] - Threshold for color change
* *
* @example * @example
* <CustomGauge * <CustomGauge
* progress={75} * progress={75}
* radius={50} * radius={50}
* color="#00ff00"
* strokeWidth={10} * strokeWidth={10}
* threshold={50}
* /> * />
* *
* @returns {React.ReactElement} Rendered CustomGauge component * @returns {React.ReactElement} Rendered CustomGauge component
*/ */
const CustomGauge = ({ const CustomGauge = ({ progress = 0, radius = 70, strokeWidth = 15, threshold = 50 }) => {
progress = 0, const theme = useTheme();
radius = 60,
color = "#000000",
strokeWidth = 15,
}) => {
// Calculate the length of the stroke for the circle // Calculate the length of the stroke for the circle
const { circumference, totalSize, strokeLength } = useMemo( const { circumference, totalSize, strokeLength } = useMemo(
() => ({ () => ({
@@ -39,10 +38,9 @@ const CustomGauge = ({
}), }),
[radius, strokeWidth, progress] [radius, strokeWidth, progress]
); );
const [offset, setOffset] = useState(circumference);
const theme = useTheme();
// Handle initial animation // Handle initial animation
const [offset, setOffset] = useState(circumference);
useEffect(() => { useEffect(() => {
setOffset(circumference); setOffset(circumference);
const timer = setTimeout(() => { const timer = setTimeout(() => {
@@ -52,6 +50,13 @@ const CustomGauge = ({
return () => clearTimeout(timer); return () => clearTimeout(timer);
}, [progress, circumference, strokeLength]); }, [progress, circumference, strokeLength]);
const progressWithinRange = Math.max(MINIMUM_VALUE, Math.min(progress, MAXIMUM_VALUE));
const fillColor =
progressWithinRange > threshold
? theme.palette.primary.main
: theme.palette.percentage.uptimePoor;
return ( return (
<Box <Box
className="radial-chart" className="radial-chart"
@@ -74,7 +79,7 @@ const CustomGauge = ({
/> />
<circle <circle
className="radial-chart-progress" className="radial-chart-progress"
stroke={color} stroke={fillColor}
strokeWidth={strokeWidth} strokeWidth={strokeWidth}
strokeDasharray={`${circumference} ${circumference}`} strokeDasharray={`${circumference} ${circumference}`}
strokeDashoffset={offset} strokeDashoffset={offset}
@@ -97,7 +102,7 @@ const CustomGauge = ({
fill: theme.typography.body2.color, fill: theme.typography.body2.color,
}} }}
> >
{`${progress.toFixed(2)}%`} {`${progressWithinRange.toFixed(1)}%`}
</Typography> </Typography>
</Box> </Box>
); );
@@ -108,6 +113,6 @@ export default CustomGauge;
CustomGauge.propTypes = { CustomGauge.propTypes = {
progress: PropTypes.number, progress: PropTypes.number,
radius: PropTypes.number, radius: PropTypes.number,
color: PropTypes.string,
strokeWidth: PropTypes.number, strokeWidth: PropTypes.number,
threshold: PropTypes.number,
}; };
+4 -2
View File
@@ -27,6 +27,7 @@ import { Gauge } from "../../Components/Charts/Gauge";
import { Pagination } from "./components/TablePagination"; import { Pagination } from "./components/TablePagination";
// import { getInfrastructureMonitorsByTeamId } from "../../Features/InfrastructureMonitors/infrastructureMonitorsSlice"; // import { getInfrastructureMonitorsByTeamId } from "../../Features/InfrastructureMonitors/infrastructureMonitorsSlice";
import { networkService } from "../../Utils/NetworkService.js"; import { networkService } from "../../Utils/NetworkService.js";
import CustomGauge from "../../Components/Charts/CustomGauge/index.jsx";
// const ROWS = Array.from(Array(20).keys()).map(() => mockedData); // const ROWS = Array.from(Array(20).keys()).map(() => mockedData);
@@ -246,10 +247,11 @@ function Infrastructure(/* {isAdmin} */) {
</Stack> </Stack>
</TableCell> </TableCell>
<TableCell align="center"> <TableCell align="center">
<Gauge {/* <Gauge
progressValue={row.cpu} progressValue={row.cpu}
containerWidth={60} containerWidth={60}
/> /> */}
<CustomGauge progress={row.cpu} />
</TableCell> </TableCell>
<TableCell align="center"> <TableCell align="center">
<Gauge <Gauge