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

124 lines
3.2 KiB
React
Raw Normal View History

2024-10-29 10:24:03 -04:00
import { RadialBarChart, RadialBar, ResponsiveContainer } from "recharts";
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-07 10:52:19 +08:00
const MINIMUM_VALUE = 0;
const MAXIMUM_VALUE = 100;
2024-11-05 10:33:20 -05:00
const DATA = [{ value: MAXIMUM_VALUE }];
2024-11-01 13:44:55 -04:00
const PROGRESS_THRESHOLD = 50;
2024-11-01 13:39:02 -04:00
const DEFAULT_CONTAINER_HEIGHT = 160;
const TEXT_POSITIONS = {
value: { x: "50%", y: "45%" },
label: { x: "50%", y: "55%" },
};
2024-11-07 10:52:19 +08:00
const COMMON_RADIAL_BAR_PROPS = {
2024-10-29 21:35:58 -04:00
minAngle: 15,
clockWise: true,
dataKey: "value",
cornerRadius: 0,
2024-10-29 21:35:58 -04:00
};
2024-11-07 10:52:19 +08:00
const RADIAL_BAR_CHART_PROPS = {
2024-11-01 13:39:02 -04:00
cx: "50%",
cy: "50%",
innerRadius: "45%",
outerRadius: "100%",
startAngle: 90,
endAngle: -270,
};
2024-11-07 10:52:19 +08:00
const RADIAL_BAR_OVERLAY_PROPS = {
2024-11-01 13:39:02 -04:00
transform: "rotate(180deg)",
position: "absolute",
top: "0",
left: "0",
};
2024-11-06 12:48:01 -05:00
const COMMON_HEADER_PROPS = {
textAnchor: "middle",
dominantBaseline: "middle",
};
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)
* @param {string} props.displayText - The text to display below the progress value
* @param {number} [props.containerHeight=160] - Height of the gauge container in pixels
* @param {Object} [props.gaugeHeader] - Custom styles to apply to the progress value text
* @param {Object} [props.gaugeSubheader] - Custom styles to apply to the display text
*
* @returns {JSX.Element} A circular gauge chart with progress value and text
*
* @example
* <Gauge
* progressValue={75}
* displayText="Completion"
* containerHeight={200}
* gaugeHeader={{ fontSize: '24px' }}
* gaugeSubheader={{ fill: 'gray' }}
* />
*/
const Gauge = ({
progressValue,
displayText,
containerHeight,
gaugeHeader,
gaugeSubheader,
}) => {
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-01 13:39:02 -04:00
2024-10-29 10:24:03 -04:00
return (
2024-11-01 13:39:02 -04:00
<ResponsiveContainer height={containerHeight ?? DEFAULT_CONTAINER_HEIGHT}>
2024-10-29 10:24:03 -04:00
<RadialBarChart
2024-11-07 10:52:19 +08:00
{...RADIAL_BAR_CHART_PROPS}
2024-11-01 13:39:02 -04:00
data={DATA}
2024-10-29 10:24:03 -04:00
>
<RadialBar
2024-11-07 10:52:19 +08:00
{...COMMON_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-01 13:39:02 -04:00
data={[{ value: myProgressValue }]}
2024-10-29 10:24:03 -04:00
/>
<RadialBar
2024-11-07 10:52:19 +08:00
{...COMMON_RADIAL_BAR_PROPS}
2024-11-01 13:39:02 -04:00
data={DATA}
2024-11-07 10:52:19 +08:00
style={RADIAL_BAR_OVERLAY_PROPS}
2024-10-29 10:24:03 -04:00
/>
<text
2024-11-01 13:39:02 -04:00
{...TEXT_POSITIONS.value}
role="text"
aria-label={`${myProgressValue}%`}
2024-11-07 10:52:19 +08:00
style={{ ...COMMON_HEADER_PROPS, ...theme.chart.header, ...gaugeHeader }}
2024-10-29 10:24:03 -04:00
>
2024-11-01 13:39:02 -04:00
{`${myProgressValue}%`}
2024-10-29 10:24:03 -04:00
</text>
<text
2024-11-01 13:39:02 -04:00
{...TEXT_POSITIONS.label}
role="text"
aria-label={`${displayText}`}
2024-11-07 10:52:19 +08:00
style={{ ...COMMON_HEADER_PROPS, ...theme.chart.subheader, ...gaugeSubheader }}
2024-10-29 10:24:03 -04:00
>
{`${displayText}`}
</text>
</RadialBarChart>
</ResponsiveContainer>
);
};
Gauge.propTypes = {
2024-10-29 14:03:41 -04:00
progressValue: PropTypes.number.isRequired,
displayText: PropTypes.string.isRequired,
containerHeight: PropTypes.number,
gaugeHeader: PropTypes.object,
gaugeSubheader: PropTypes.object,
2024-10-29 10:24:03 -04:00
};
export default Gauge;