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 React from "react";
import { RadialBarChart, RadialBar, ResponsiveContainer } from "recharts";
import PropTypes from "prop-types";
2024-11-01 13:39:02 -04:00
const DATA = [{ value: 100 }];
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 DEFAULT_HEADER_FONT = { fontSize: "11px" };
const DEFAULT_SUBHEADER_FONT = { fontSize: "9px" };
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,
theme,
containerHeight,
gaugeHeader,
gaugeSubheader,
}) => {
2024-11-01 13:39:02 -04:00
const header = gaugeHeader ?? DEFAULT_HEADER_FONT;
const subheader = gaugeSubheader ?? DEFAULT_SUBHEADER_FONT;
const myProgressValue =
progressValue < 0 ? 0 : progressValue > DATA[0].value ? DATA[0].value : progressValue;
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,
theme: PropTypes.object.isRequired,
containerHeight: PropTypes.number,
gaugeHeader: PropTypes.object,
gaugeSubheader: PropTypes.object,
2024-10-29 10:24:03 -04:00
};
export default Gauge;