- update based on review
1. Extract out all hardcoded values to consts 2. Add arial labels to text 3. Handle error boundary when progressValue is not within 0 -100
This commit is contained in:
1 parent
a2d31124ff
commit
b389275963
1 file changed
+47
-28
@@ -2,14 +2,38 @@ import React from "react";
|
||||
import { RadialBarChart, RadialBar, ResponsiveContainer } from "recharts";
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
const data = [{ value: 100 }];
|
||||
const commonRadialBarProps = {
|
||||
const DATA = [{ value: 100 }];
|
||||
const PRGRESS_THRESHOLD = 50;
|
||||
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 = {
|
||||
minAngle: 15,
|
||||
clockWise: true,
|
||||
dataKey: "value",
|
||||
cornerRadius: 0,
|
||||
};
|
||||
|
||||
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,
|
||||
@@ -18,49 +42,44 @@ const Gauge = ({
|
||||
gaugeHeader,
|
||||
gaugeSubheader,
|
||||
}) => {
|
||||
const header = gaugeHeader ?? { fontSize: "11px" };
|
||||
const subheader = gaugeSubheader ?? { fontSize: "9px" };
|
||||
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;
|
||||
|
||||
return (
|
||||
<ResponsiveContainer height={containerHeight ?? 160}>
|
||||
<ResponsiveContainer height={containerHeight ?? DEFAULT_CONTAINER_HEIGHT}>
|
||||
<RadialBarChart
|
||||
cx="50%"
|
||||
cy="50%"
|
||||
innerRadius="45%"
|
||||
outerRadius="100%"
|
||||
startAngle={90}
|
||||
endAngle={-270}
|
||||
data={data}
|
||||
{...RADIALBARCHART_PROPS}
|
||||
data={DATA}
|
||||
>
|
||||
<RadialBar
|
||||
{...commonRadialBarProps}
|
||||
{...COMMON_RADIALBAR_PROPS}
|
||||
fill={
|
||||
progressValue > 50
|
||||
progressValue > PRGRESS_THRESHOLD
|
||||
? theme.palette.primary.main
|
||||
: theme.palette.percentage.uptimePoor
|
||||
}
|
||||
background={{ fill: theme.palette.background.fill }}
|
||||
data={[{ value: progressValue }]}
|
||||
data={[{ value: myProgressValue }]}
|
||||
/>
|
||||
<RadialBar
|
||||
{...commonRadialBarProps}
|
||||
data={data}
|
||||
style={{
|
||||
transform: "rotate(180deg)",
|
||||
position: "absolute",
|
||||
top: "0",
|
||||
left: "0",
|
||||
}}
|
||||
{...COMMON_RADIALBAR_PROPS}
|
||||
data={DATA}
|
||||
style={RADIALBAR_OVERLAY_PROPS}
|
||||
/>
|
||||
<text
|
||||
x="50%"
|
||||
y="45%"
|
||||
{...TEXT_POSITIONS.value}
|
||||
role="text"
|
||||
aria-label={`${myProgressValue}%`}
|
||||
style={{ ...theme.palette.gaugeHeaderCommon, ...header }}
|
||||
>
|
||||
{`${progressValue}%`}
|
||||
{`${myProgressValue}%`}
|
||||
</text>
|
||||
<text
|
||||
x="50%"
|
||||
y="55%"
|
||||
{...TEXT_POSITIONS.label}
|
||||
role="text"
|
||||
aria-label={`${displayText}`}
|
||||
style={{ ...theme.palette.gaugeHeaderCommon, ...subheader }}
|
||||
>
|
||||
{`${displayText}`}
|
||||
|
||||
Reference in new issue
Block a user