2024-11-16 22:48:46 -05:00
|
|
|
import { RadialBarChart, RadialBar, ResponsiveContainer, Text, Legend } 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-07 10:52:19 +08:00
|
|
|
const MINIMUM_VALUE = 0;
|
|
|
|
|
const MAXIMUM_VALUE = 100;
|
2024-11-01 13:44:55 -04:00
|
|
|
const PROGRESS_THRESHOLD = 50;
|
2024-11-16 22:48:46 -05:00
|
|
|
const DEFAULT_WIDTH = 60;
|
|
|
|
|
// const DEFAULT_CONTAINER_HEIGHT = 160;
|
|
|
|
|
// const TEXT_POSITIONS = {
|
|
|
|
|
// value: { x: "50%", y: "45%" },
|
|
|
|
|
// label: { x: "50%", y: "55%" },
|
|
|
|
|
// };
|
2024-10-29 21:35:58 -04:00
|
|
|
|
2024-11-16 22:48:46 -05:00
|
|
|
const RADIUS = "90%";
|
|
|
|
|
const START_ANGLE = 90;
|
|
|
|
|
const CHART_RANGE = 360;
|
2024-11-07 10:52:19 +08:00
|
|
|
const RADIAL_BAR_CHART_PROPS = {
|
2024-11-16 22:48:46 -05:00
|
|
|
innerRadius: RADIUS,
|
|
|
|
|
outerRadius: RADIUS,
|
|
|
|
|
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-16 22:48:46 -05:00
|
|
|
const COMMON_RADIAL_BAR_PROPS = {
|
|
|
|
|
dataKey: "value",
|
|
|
|
|
cornerRadius: 8,
|
2024-11-06 12:48:01 -05:00
|
|
|
};
|
|
|
|
|
|
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' }}
|
|
|
|
|
* />
|
|
|
|
|
*/
|
2024-11-01 11:06:25 -04:00
|
|
|
const Gauge = ({
|
|
|
|
|
progressValue,
|
2024-11-16 22:48:46 -05:00
|
|
|
containerWidth,
|
|
|
|
|
// displayText,
|
|
|
|
|
// containerHeight,
|
|
|
|
|
// gaugeHeader,
|
|
|
|
|
// gaugeSubheader,
|
2024-11-01 11:06:25 -04:00
|
|
|
}) => {
|
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-16 22:48:46 -05:00
|
|
|
const chartData = [
|
|
|
|
|
{
|
|
|
|
|
value: MAXIMUM_VALUE,
|
|
|
|
|
fill: "transparent",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
value: myProgressValue,
|
|
|
|
|
fill:
|
|
|
|
|
progressValue > PROGRESS_THRESHOLD
|
|
|
|
|
? theme.palette.primary.main
|
|
|
|
|
: theme.palette.percentage.uptimePoor,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
const width = containerWidth ?? DEFAULT_WIDTH;
|
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}
|
|
|
|
|
|
|
|
|
|
/* height={containerHeight ?? DEFAULT_CONTAINER_HEIGHT}
|
|
|
|
|
width={containerWidth} */
|
|
|
|
|
>
|
2024-10-29 10:24:03 -04:00
|
|
|
<RadialBarChart
|
2024-11-07 10:52:19 +08:00
|
|
|
{...RADIAL_BAR_CHART_PROPS}
|
2024-11-16 22:48:46 -05:00
|
|
|
data={chartData /* [{ ...DATA, fill: "#666666" }, { value: myProgressValue }] */}
|
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-16 22:48:46 -05:00
|
|
|
label={{
|
|
|
|
|
position: "center",
|
|
|
|
|
fill: "#000000",
|
|
|
|
|
content: () => (
|
|
|
|
|
<text
|
|
|
|
|
x="50%"
|
|
|
|
|
y="50%"
|
|
|
|
|
textAnchor="middle"
|
|
|
|
|
dominantBaseline="middle"
|
|
|
|
|
style={{
|
|
|
|
|
fontSize: "12px",
|
|
|
|
|
fill: "#000000",
|
|
|
|
|
}}
|
|
|
|
|
>{`${myProgressValue}%`}</text>
|
|
|
|
|
),
|
|
|
|
|
}}
|
|
|
|
|
/* data={[{ value: myProgressValue }]} */
|
2024-10-29 10:24:03 -04:00
|
|
|
/>
|
2024-11-16 22:48:46 -05: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-11-16 22:48:46 -05:00
|
|
|
/> */}
|
|
|
|
|
{/* TODO what is the text tag? */}
|
|
|
|
|
{/* <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}`}
|
2024-11-16 22:48:46 -05:00
|
|
|
</text> */}
|
2024-10-29 10:24:03 -04:00
|
|
|
</RadialBarChart>
|
|
|
|
|
</ResponsiveContainer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Gauge.propTypes = {
|
2024-10-29 14:03:41 -04:00
|
|
|
progressValue: PropTypes.number.isRequired,
|
|
|
|
|
displayText: PropTypes.string.isRequired,
|
2024-11-01 11:06:25 -04:00
|
|
|
containerHeight: PropTypes.number,
|
|
|
|
|
gaugeHeader: PropTypes.object,
|
|
|
|
|
gaugeSubheader: PropTypes.object,
|
2024-10-29 10:24:03 -04:00
|
|
|
};
|
2024-11-16 22:48:46 -05:00
|
|
|
export { Gauge };
|