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

111 lines
2.6 KiB
React
Raw Normal View History

2024-11-17 15:56:42 -05:00
import { RadialBarChart, RadialBar, ResponsiveContainer } from "recharts";
2024-10-29 10:24:03 -04:00
import PropTypes from "prop-types";
2024-11-05 10:33:20 -05:00
import { useTheme } from "@emotion/react";
2024-10-29 10:24:03 -04:00
2024-11-29 10:17:09 -05:00
/* TODO delete component */
2024-11-07 10:52:19 +08:00
const MINIMUM_VALUE = 0;
const MAXIMUM_VALUE = 100;
2024-11-17 15:56:42 -05:00
const CHART_MAXIMUM_DATA = {
value: MAXIMUM_VALUE,
fill: "transparent",
};
2024-11-01 13:44:55 -04:00
const PROGRESS_THRESHOLD = 50;
2024-11-16 22:48:46 -05:00
const DEFAULT_WIDTH = 60;
2024-10-29 21:35:58 -04:00
2024-11-17 15:56:42 -05:00
const RADIUS_SIZE = "90%";
2024-11-16 22:48:46 -05:00
const START_ANGLE = 90;
const CHART_RANGE = 360;
2024-11-07 10:52:19 +08:00
const RADIAL_BAR_CHART_PROPS = {
2024-11-17 15:56:42 -05:00
innerRadius: RADIUS_SIZE,
outerRadius: RADIUS_SIZE,
2024-11-16 22:48:46 -05:00
barSize: 6,
startAngle: START_ANGLE,
endAngle: START_ANGLE - CHART_RANGE,
margin: { top: 0, right: 0, bottom: 0, left: 0 },
2024-11-01 13:39:02 -04:00
};
2024-11-17 15:56:42 -05:00
const RADIAL_BAR_PROPS = {
2024-11-16 22:48:46 -05:00
dataKey: "value",
cornerRadius: 8,
2024-11-06 12:48:01 -05:00
};
2024-11-17 15:56:42 -05:00
Gauge.propTypes = {
progressValue: PropTypes.number.isRequired,
width: PropTypes.number,
};
2024-11-07 10:52:19 +08:00
/**
* A circular gauge component that displays a progress value and text.
* The gauge fills based on the progress value and changes color at a 50% threshold.
*
* @component
* @param {Object} props - Component props
* @param {number} props.progressValue - The value to display in the gauge (0-100)
2024-11-17 15:56:42 -05:00
* @param {number} props.width - Width of the gauge container in pixels
2024-11-07 10:52:19 +08:00
* @returns {JSX.Element} A circular gauge chart with progress value and text
*
* @example
* <Gauge
* progressValue={75}
2024-11-17 15:56:42 -05:00
* width={200}
2024-11-07 10:52:19 +08:00
* />
*/
2024-11-17 15:56:42 -05:00
function Gauge({ progressValue, width = DEFAULT_WIDTH }) {
2024-11-07 10:52:19 +08:00
const theme = useTheme();
2024-11-05 10:33:20 -05:00
const myProgressValue = Math.max(MINIMUM_VALUE, Math.min(progressValue, MAXIMUM_VALUE));
2024-11-17 15:56:42 -05:00
const chartColor =
progressValue > PROGRESS_THRESHOLD
? theme.palette.primary.main
: theme.palette.percentage.uptimePoor;
2024-11-16 22:48:46 -05:00
const chartData = [
2024-11-17 15:56:42 -05:00
CHART_MAXIMUM_DATA,
2024-11-16 22:48:46 -05:00
{
value: myProgressValue,
2024-11-17 15:56:42 -05:00
fill: chartColor,
2024-11-16 22:48:46 -05:00
},
];
2024-11-01 13:39:02 -04:00
2024-10-29 10:24:03 -04:00
return (
2024-11-16 22:48:46 -05:00
<ResponsiveContainer
aspect={1}
width={width}
style={{ marginInline: "auto" }}
2024-11-16 22:48:46 -05:00
>
2024-10-29 10:24:03 -04:00
<RadialBarChart
2024-11-07 10:52:19 +08:00
{...RADIAL_BAR_CHART_PROPS}
2024-11-17 15:56:42 -05:00
data={chartData}
2024-10-29 10:24:03 -04:00
>
<RadialBar
2024-11-17 15:56:42 -05:00
{...RADIAL_BAR_PROPS}
2024-10-29 21:35:58 -04:00
fill={
2024-11-01 13:44:55 -04:00
progressValue > PROGRESS_THRESHOLD
2024-10-29 21:35:58 -04:00
? theme.palette.primary.main
: theme.palette.percentage.uptimePoor
}
background={{ fill: theme.palette.background.fill }}
2024-11-16 22:48:46 -05:00
label={{
position: "center",
content: () => (
<text
x="50%"
y="50%"
textAnchor="middle"
dominantBaseline="middle"
style={{
...theme.typography.body2,
fill: theme.typography.body2.color,
2024-11-16 22:48:46 -05:00
}}
2024-11-17 15:56:42 -05:00
>
{`${myProgressValue}%`}
</text>
2024-11-16 22:48:46 -05:00
),
}}
2024-10-29 10:24:03 -04:00
/>
</RadialBarChart>
</ResponsiveContainer>
);
2024-11-17 15:56:42 -05:00
}
2024-10-29 10:24:03 -04:00
2024-11-16 22:48:46 -05:00
export { Gauge };