Fixed hacky positioning and added pie sections

This commit is contained in:
Daniel Cojocea committed 2024-08-05 13:18:00 -04:00
1 parent 84fa9cc7c6
commit 03375067d0
3 files changed
+143 -40

No files matched your search

@@ -51,3 +51,17 @@
width: 22px; width: 22px;
height: 22px; height: 22px;
} }
.page-speed-details .MuiPieArcLabel-root {
font-size: var(--env-var-font-size-small-plus);
transition: fill 300ms ease;
}
.page-speed-details .MuiPieArcLabel-faded {
fill: rgba(0, 0, 0, 0.3);
}
.page-speed-details .pie-label {
transition: all 400ms ease;
}
.page-speed-details .MuiPieArc-root {
stroke: inherit;
}
+105 -36
View File
@@ -59,14 +59,15 @@ const StatBox = ({ icon, title, value }) => {
* @returns {JSX.Element} * @returns {JSX.Element}
*/ */
const PieCenterLabel = ({ value, color }) => { const PieCenterLabel = ({ value, color }) => {
const { width, height, left, top } = useDrawingArea(); const { width, height } = useDrawingArea();
return ( return (
<text <text
x={left + width - 1} className="pie-label"
y={top + height / 2 + 2} x={width / 2}
y={height / 2}
style={{ style={{
fill: color, fill: color,
fontSize: "55px", fontSize: "45px",
textAnchor: "middle", textAnchor: "middle",
dominantBaseline: "central", dominantBaseline: "central",
userSelect: "none", userSelect: "none",
@@ -114,7 +115,7 @@ const PageSpeedDetails = () => {
title: "Cumulative Layout Shift", title: "Cumulative Layout Shift",
description: description:
"Cumulative Layout Shift measures the movement of visible elements within the viewport.", "Cumulative Layout Shift measures the movement of visible elements within the viewport.",
score: 1, score: 0.1,
scoreDisplayMode: "numeric", scoreDisplayMode: "numeric",
displayValue: "0", displayValue: "0",
numericValue: 0, numericValue: 0,
@@ -126,7 +127,7 @@ const PageSpeedDetails = () => {
title: "Speed Index", title: "Speed Index",
description: description:
"Speed Index shows how quickly the contents of a page are visibly populated.", "Speed Index shows how quickly the contents of a page are visibly populated.",
score: 1, score: 0.9,
scoreDisplayMode: "numeric", scoreDisplayMode: "numeric",
displayValue: "0.6s", displayValue: "0.6s",
numericValue: 567.8934352052013, numericValue: 567.8934352052013,
@@ -138,7 +139,7 @@ const PageSpeedDetails = () => {
title: "First Contentful Paint", title: "First Contentful Paint",
description: description:
"First Contentful Paint marks the time at which the first text or image is painted.", "First Contentful Paint marks the time at which the first text or image is painted.",
score: 0.1, score: 0.8,
scoreDisplayMode: "numeric", scoreDisplayMode: "numeric",
displayValue: "0.4s", displayValue: "0.4s",
numericValue: 419, numericValue: 419,
@@ -150,7 +151,7 @@ const PageSpeedDetails = () => {
title: "Largest Contentful Paint", title: "Largest Contentful Paint",
description: description:
"Largest Contentful Paint marks the time at which the largest text or image is painted.", "Largest Contentful Paint marks the time at which the largest text or image is painted.",
score: 1, score: 0.6,
scoreDisplayMode: "numeric", scoreDisplayMode: "numeric",
displayValue: "0.4s", displayValue: "0.4s",
numericValue: 422.5, numericValue: 422.5,
@@ -162,7 +163,7 @@ const PageSpeedDetails = () => {
title: "Total Blocking Time", title: "Total Blocking Time",
description: description:
"Sum of all time periods between FCP and Time to Interactive", "Sum of all time periods between FCP and Time to Interactive",
score: 1, score: 0.9,
scoreDisplayMode: "numeric", scoreDisplayMode: "numeric",
displayValue: "20ms", displayValue: "20ms",
numericValue: 16, numericValue: 16,
@@ -188,24 +189,6 @@ const PageSpeedDetails = () => {
cls: 25, cls: 25,
}; };
/**
* Calculates the performance score based on the provided audit data and weights.
*
* @param {Object} audits - An object containing audit data.
* @param {Object} audits.<metric> - An object for each performance metric.
* @param {number} audits.<metric>.score - The score for the specific metric.
* @returns {number} The calculated performance score, rounded to the nearest integer.
*/
const calculatePerformance = (audits) => {
let sum = 0;
Object.keys(audits).forEach((key) => {
if (audits[key].score) sum += audits[key].score * weights[key];
});
return Math.round(sum);
};
var performance = calculatePerformance(data.audits);
/** /**
* Retrieves color properties based on the performance value. * Retrieves color properties based on the performance value.
* *
@@ -219,8 +202,64 @@ const PageSpeedDetails = () => {
return theme.pie.default; return theme.pie.default;
}; };
let performance = 0;
const getPieData = (audits) => {
let props = [];
let startAngle = 0;
const padding = 3; // padding between arcs
const max = 360 - padding * (Object.keys(audits).length - 1); // _id is a child of audits
Object.keys(audits).forEach((key) => {
if (audits[key].score) {
let value = audits[key].score * weights[key];
let endAngle = startAngle + (weights[key] * max) / 100;
let theme = getColors(audits[key].score * 100);
props.push({
id: key,
data: [
{
value: value,
color: theme.stroke,
label: key.toUpperCase(),
},
{
value: weights[key] - value,
color: theme.strokeBg,
label: "",
},
],
arcLabel: (item) => `${item.label}`,
arcLabelRadius: 95,
startAngle: startAngle,
endAngle: endAngle,
innerRadius: 70,
outerRadius: 80,
cornerRadius: 3,
highlightScope: { faded: "global", highlighted: "series" },
faded: {
innerRadius: 60,
outerRadius: 70,
additionalRadius: -20,
arcLabelRadius: 5,
},
cx: pieSize.width / 2,
});
performance += Math.round(value);
startAngle = endAngle + padding;
}
});
return props;
};
const pieSize = { width: 300, height: 205 };
const pieData = getPieData(data.audits);
const colorMap = getColors(performance); const colorMap = getColors(performance);
const [highlightedItem, setHighLightedItem] = useState(null);
return ( return (
<Stack className="page-speed-details" gap={theme.gap.large}> <Stack className="page-speed-details" gap={theme.gap.large}>
<Button <Button
@@ -338,10 +377,8 @@ const PageSpeedDetails = () => {
color: colorMap.bg, color: colorMap.bg,
}, },
], ],
color: "red",
outerRadius: 65, outerRadius: 65,
cx: "100%", cx: pieSize.width / 2,
cy: "50%",
}, },
{ {
data: [ data: [
@@ -350,24 +387,56 @@ const PageSpeedDetails = () => {
color: colorMap.stroke, color: colorMap.stroke,
}, },
], ],
color: "red",
innerRadius: 60, innerRadius: 60,
outerRadius: 70, outerRadius: 70,
paddingAngle: 5, paddingAngle: 5,
cornerRadius: 5, cornerRadius: 5,
startAngle: 0, startAngle: 0,
endAngle: (performance / 100) * 360, endAngle: (performance / 100) * 360,
cx: "100%", cx: pieSize.width / 2,
cy: "50%",
}, },
]} ]}
width={200} width={pieSize.width}
height={160} height={pieSize.height}
margin={{ left: 0, top: 0, right: 0, bottom: 0 }}
tooltip={{ trigger: "none" }} tooltip={{ trigger: "none" }}
> >
<PieCenterLabel value={performance} color={colorMap.text} /> <PieCenterLabel value={performance} color={colorMap.text} />
</PieChart> </PieChart>
<Typography component="h2">Performance</Typography> <PieChart
series={[
{
data: [
{
value: 100,
color: colorMap.bg,
},
],
outerRadius: 65,
cx: pieSize.width / 2,
},
...pieData,
]}
width={pieSize.width}
height={pieSize.height}
margin={{ left: 0, top: 0, right: 0, bottom: 0 }}
highlightedItem={highlightedItem}
onHighlightChange={setHighLightedItem}
slotProps={{
legend: { hidden: true },
}}
tooltip={{ trigger: "none" }}
sx={{
"&:has(.MuiPieArcLabel-faded) .pie-label": {
fill: "rgba(0,0,0,0) !important",
},
}}
>
<PieCenterLabel value={performance} color={colorMap.text} />
</PieChart>
<Typography component="h2" mt={theme.gap.xs}>
Performance
</Typography>
<Typography>Values are estimated and may vary.</Typography> <Typography>Values are estimated and may vary.</Typography>
</Stack> </Stack>
</Box> </Box>
+24 -4
View File
@@ -135,10 +135,30 @@ const theme = createTheme({
}, },
}, },
pie: { pie: {
green: { stroke: "#17b26a", text: "#079455", bg: "#ecfdf3" }, green: {
yellow: { stroke: "#fdb022", text: "#dc6803", bg: "#fffcf5" }, stroke: "#17b26a",
red: { stroke: "#f04438", text: "#f04438", bg: "#ffeaea" }, strokeBg: "#d4f4e1",
default: { stroke: "#4e5ba6", text: "#4e5ba6", bg: "#f2f4f7" }, text: "#079455",
bg: "#ecfdf3",
},
yellow: {
stroke: "#fdb022",
strokeBg: "rgba(255, 192, 34, 0.3)",
text: "#dc6803",
bg: "#fffcf5",
},
red: {
stroke: "#f04438",
strokeBg: "#ffecea",
text: "#f04438",
bg: "#ffeaea",
},
default: {
stroke: "#4e5ba6",
strokeBg: "#f2f4f7",
text: "#4e5ba6",
bg: "#f2f4f7",
},
}, },
}); });