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

100 lines
2.4 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-05 10:33:20 -05:00
const MINIMUM_VALUE = 0 ;
const MAXIMUM_VALUE = 100 ;
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;
2024-11-05 10:33:20 -05:00
const DEFAULT_HEADER = { fontSize: "11px" };
const DEFAULT_SUBHEADER = { fontSize: "9px" };
2024-11-01 13:39:02 -04:00
const TEXT_POSITIONS = {
value: { x: "50%", y: "45%" },
label: { x: "50%", y: "55%" },
};
const COMMON_RADIALBAR_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-01 13:39:02 -04:00
const RADIALBARCHART_PROPS = {
cx: "50%",
cy: "50%",
innerRadius: "45%",
outerRadius: "100%",
startAngle: 90,
endAngle: -270,
};
const RADIALBAR_OVERLAY_PROPS = {
transform: "rotate(180deg)",
position: "absolute",
top: "0",
left: "0",
};
const Gauge = ({
progressValue,
displayText,
containerHeight,
gaugeHeader,
gaugeSubheader,
}) => {
2024-11-05 10:33:20 -05:00
const theme = useTheme();
const header = gaugeHeader ?? DEFAULT_HEADER;
const subheader = gaugeSubheader ?? DEFAULT_SUBHEADER;
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-01 13:39:02 -04:00
{...RADIALBARCHART_PROPS}
data={DATA}
2024-10-29 10:24:03 -04:00
>
<RadialBar
2024-11-01 13:39:02 -04:00
{...COMMON_RADIALBAR_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-01 13:39:02 -04:00
{...COMMON_RADIALBAR_PROPS}
data={DATA}
style={RADIALBAR_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}%`}
style={{ ...theme.palette.gaugeHeaderCommon, ...header }}
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}`}
style={{ ...theme.palette.gaugeHeaderCommon, ...subheader }}
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;