- 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 { RadialBarChart, RadialBar, ResponsiveContainer } from "recharts";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
const data = [{ value: 100 }];
|
const DATA = [{ value: 100 }];
|
||||||
const commonRadialBarProps = {
|
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,
|
minAngle: 15,
|
||||||
clockWise: true,
|
clockWise: true,
|
||||||
dataKey: "value",
|
dataKey: "value",
|
||||||
cornerRadius: 0,
|
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 = ({
|
const Gauge = ({
|
||||||
progressValue,
|
progressValue,
|
||||||
displayText,
|
displayText,
|
||||||
@@ -18,49 +42,44 @@ const Gauge = ({
|
|||||||
gaugeHeader,
|
gaugeHeader,
|
||||||
gaugeSubheader,
|
gaugeSubheader,
|
||||||
}) => {
|
}) => {
|
||||||
const header = gaugeHeader ?? { fontSize: "11px" };
|
const header = gaugeHeader ?? DEFAULT_HEADER_FONT;
|
||||||
const subheader = gaugeSubheader ?? { fontSize: "9px" };
|
const subheader = gaugeSubheader ?? DEFAULT_SUBHEADER_FONT;
|
||||||
|
const myProgressValue =
|
||||||
|
progressValue < 0 ? 0 : progressValue > DATA[0].value ? DATA[0].value : progressValue;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ResponsiveContainer height={containerHeight ?? 160}>
|
<ResponsiveContainer height={containerHeight ?? DEFAULT_CONTAINER_HEIGHT}>
|
||||||
<RadialBarChart
|
<RadialBarChart
|
||||||
cx="50%"
|
{...RADIALBARCHART_PROPS}
|
||||||
cy="50%"
|
data={DATA}
|
||||||
innerRadius="45%"
|
|
||||||
outerRadius="100%"
|
|
||||||
startAngle={90}
|
|
||||||
endAngle={-270}
|
|
||||||
data={data}
|
|
||||||
>
|
>
|
||||||
<RadialBar
|
<RadialBar
|
||||||
{...commonRadialBarProps}
|
{...COMMON_RADIALBAR_PROPS}
|
||||||
fill={
|
fill={
|
||||||
progressValue > 50
|
progressValue > PRGRESS_THRESHOLD
|
||||||
? theme.palette.primary.main
|
? theme.palette.primary.main
|
||||||
: theme.palette.percentage.uptimePoor
|
: theme.palette.percentage.uptimePoor
|
||||||
}
|
}
|
||||||
background={{ fill: theme.palette.background.fill }}
|
background={{ fill: theme.palette.background.fill }}
|
||||||
data={[{ value: progressValue }]}
|
data={[{ value: myProgressValue }]}
|
||||||
/>
|
/>
|
||||||
<RadialBar
|
<RadialBar
|
||||||
{...commonRadialBarProps}
|
{...COMMON_RADIALBAR_PROPS}
|
||||||
data={data}
|
data={DATA}
|
||||||
style={{
|
style={RADIALBAR_OVERLAY_PROPS}
|
||||||
transform: "rotate(180deg)",
|
|
||||||
position: "absolute",
|
|
||||||
top: "0",
|
|
||||||
left: "0",
|
|
||||||
}}
|
|
||||||
/>
|
/>
|
||||||
<text
|
<text
|
||||||
x="50%"
|
{...TEXT_POSITIONS.value}
|
||||||
y="45%"
|
role="text"
|
||||||
|
aria-label={`${myProgressValue}%`}
|
||||||
style={{ ...theme.palette.gaugeHeaderCommon, ...header }}
|
style={{ ...theme.palette.gaugeHeaderCommon, ...header }}
|
||||||
>
|
>
|
||||||
{`${progressValue}%`}
|
{`${myProgressValue}%`}
|
||||||
</text>
|
</text>
|
||||||
<text
|
<text
|
||||||
x="50%"
|
{...TEXT_POSITIONS.label}
|
||||||
y="55%"
|
role="text"
|
||||||
|
aria-label={`${displayText}`}
|
||||||
style={{ ...theme.palette.gaugeHeaderCommon, ...subheader }}
|
style={{ ...theme.palette.gaugeHeaderCommon, ...subheader }}
|
||||||
>
|
>
|
||||||
{`${displayText}`}
|
{`${displayText}`}
|
||||||
|
|||||||
Reference in new issue
Block a user