From c0c1cf64acf4ac50b98a3fae912ddae76e69064e Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Wed, 11 Sep 2024 17:41:37 -0400 Subject: [PATCH] Refactored pie chart --- .../PageSpeed/Details/Charts/PieChart.jsx | 298 ++++++++++++++++++ Client/src/Pages/PageSpeed/Details/index.jsx | 250 +++------------ Client/src/Pages/PageSpeed/Details/styled.jsx | 2 +- Client/src/Utils/Theme/lightTheme.js | 2 +- 4 files changed, 338 insertions(+), 214 deletions(-) create mode 100644 Client/src/Pages/PageSpeed/Details/Charts/PieChart.jsx diff --git a/Client/src/Pages/PageSpeed/Details/Charts/PieChart.jsx b/Client/src/Pages/PageSpeed/Details/Charts/PieChart.jsx new file mode 100644 index 000000000..be3b0cbb8 --- /dev/null +++ b/Client/src/Pages/PageSpeed/Details/Charts/PieChart.jsx @@ -0,0 +1,298 @@ +import PropTypes from "prop-types"; +import { PieChart as MuiPieChart } from "@mui/x-charts/PieChart"; +import { useDrawingArea } from "@mui/x-charts"; +import { useState } from "react"; +import { useTheme } from "@emotion/react"; +import { Box } from "@mui/material"; + +/** + * Renders a centered label within a pie chart. + * + * @param {Object} props + * @param {string | number} props.value - The value to display in the label. + * @param {string} props.color - The color of the text. + * @returns {JSX.Element} + */ +const PieCenterLabel = ({ value, color, setExpand }) => { + const { width, height } = useDrawingArea(); + return ( + setExpand(true)} + > + + + {value} + + + ); +}; + +PieCenterLabel.propTypes = { + value: PropTypes.oneOfType([PropTypes.string, PropTypes.number]), + color: PropTypes.string, + setExpand: PropTypes.func, +}; + +/** + * A component that renders a label on a pie chart slice. + * The label is positioned relative to the center of the pie chart and is optionally highlighted. + * + * @param {Object} props + * @param {number} props.value - The value to display inside the pie slice. + * @param {number} props.startAngle - The starting angle of the pie slice in degrees. + * @param {number} props.endAngle - The ending angle of the pie slice in degrees. + * @param {string} props.color - The color of the label text when highlighted. + * @param {boolean} props.highlighted - Determines if the label should be highlighted or not. + * @returns {JSX.Element} + */ +const PieValueLabel = ({ value, startAngle, endAngle, color, highlighted }) => { + const { width, height } = useDrawingArea(); + + // Compute the midpoint angle in radians + const angle = (((startAngle + endAngle) / 2) * Math.PI) / 180; + const radius = height / 3.5; // length from center of the circle to where the text is positioned + + // Calculate x and y positions + const x = Math.sin(angle) * radius; + const y = -Math.cos(angle) * radius; + + return ( + + + +{value} + + + ); +}; + +const PieChart = ({ audits }) => { + const theme = useTheme(); + + /** + * Weight constants for different performance metrics. + * @type {Object} + */ + const weights = { + fcp: 10, + si: 10, + lcp: 25, + tbt: 30, + cls: 25, + }; + + /** + * Retrieves color properties based on the performance value. + * + * @param {number} value - The performance score used to determine the color properties. + * @returns {{stroke: string, strokeBg: string, text: string, bg: string}} The color properties for the given performance value. + */ + const getColors = (value) => { + if (value >= 90 && value <= 100) + return { + stroke: theme.palette.success.main, + strokeBg: theme.palette.success.light, + text: theme.palette.success.text, + bg: theme.palette.success.bg, + }; + else if (value >= 50 && value < 90) + return { + stroke: theme.palette.warning.main, + strokeBg: theme.palette.warning.light, + text: theme.palette.warning.text, + bg: theme.palette.warning.bg, + }; + else if (value >= 0 && value < 50) + return { + stroke: theme.palette.error.text, + strokeBg: theme.palette.error.light, + text: theme.palette.error.text, + bg: theme.palette.error.bg, + }; + return { + stroke: theme.palette.unresolved.main, + strokeBg: theme.palette.unresolved.light, + text: theme.palette.unresolved.main, + bg: theme.palette.unresolved.bg, + }; + }; + + /** + * Calculates and formats the data needed for rendering a pie chart based on audit scores and weights. + * This function generates properties for each pie slice, including angles, radii, and colors. + * It also calculates performance based on the weighted values. + * + * @returns {Array} An array of objects, each representing the properties for a slice of the pie chart. + * @returns {number} performance - A variable updated with the rounded sum of weighted values. + */ + 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: 73, + outerRadius: 80, + cornerRadius: 2, + highlightScope: { faded: "global", highlighted: "series" }, + faded: { + innerRadius: 63, + outerRadius: 70, + additionalRadius: -20, + arcLabelRadius: 5, + }, + cx: pieSize.width / 2, + }); + + performance += Math.floor(value); + startAngle = endAngle + padding; + } + }); + + return props; + }; + + const pieSize = { width: 210, height: 200 }; + const pieData = getPieData(audits); + const colorMap = getColors(performance); + + const [highlightedItem, setHighLightedItem] = useState(null); + const [expand, setExpand] = useState(false); + return ( + setExpand(false)}> + {expand ? ( + + + {pieData?.map((pie) => ( + + ))} + + ) : ( + + + + )} + + ); +}; + +export default PieChart; diff --git a/Client/src/Pages/PageSpeed/Details/index.jsx b/Client/src/Pages/PageSpeed/Details/index.jsx index f87b6dcf8..f5ab4c987 100644 --- a/Client/src/Pages/PageSpeed/Details/index.jsx +++ b/Client/src/Pages/PageSpeed/Details/index.jsx @@ -7,7 +7,6 @@ import { Tooltip, Typography, } from "@mui/material"; -import { PieChart } from "@mui/x-charts/PieChart"; import { useDrawingArea } from "@mui/x-charts"; import { useEffect, useState } from "react"; import { useTheme } from "@emotion/react"; @@ -30,6 +29,7 @@ import PulseDot from "../../../Components/Animated/PulseDot"; import PagespeedDetailsAreaChart from "./Charts/AreaChart"; import "./index.css"; import Checkbox from "../../../Components/Inputs/Checkbox"; +import PieChart from "./Charts/PieChart"; /** * Renders a centered label within a pie chart. @@ -151,125 +151,6 @@ const PageSpeedDetails = () => { fetchMonitor(); }, [monitorId, authToken, navigate]); - /** - * Weight constants for different performance metrics. - * @type {Object} - */ - const weights = { - fcp: 10, - si: 10, - lcp: 25, - tbt: 30, - cls: 25, - }; - - /** - * Retrieves color properties based on the performance value. - * - * @param {number} value - The performance score used to determine the color properties. - * @returns {{stroke: string, text: string, bg: string}} The color properties for the given performance value. - */ - const getColors = (value) => { - if (value >= 90 && value <= 100) - return { - stroke: theme.palette.success.main, - strokeBg: theme.palette.success.light, - text: theme.palette.success.text, - bg: theme.palette.success.bg, - shape: "circle", - }; - else if (value >= 50 && value < 90) - return { - stroke: theme.palette.warning.main, - strokeBg: theme.palette.warning.bg, - text: theme.palette.warning.text, - bg: theme.palette.warning.light, - shape: "square", - }; - else if (value >= 0 && value < 50) - return { - stroke: theme.palette.error.text, - strokeBg: theme.palette.error.light, - text: theme.palette.error.text, - bg: theme.palette.error.bg, - shape: "circle", - }; - return { - stroke: theme.palette.unresolved.main, - strokeBg: theme.palette.unresolved.light, - text: theme.palette.unresolved.main, - bg: theme.palette.unresolved.bg, - shape: "", - }; - }; - - /** - * Calculates and formats the data needed for rendering a pie chart based on audit scores and weights. - * This function generates properties for each pie slice, including angles, radii, and colors. - * It also calculates performance based on the weighted values. - * - * @returns {Array} An array of objects, each representing the properties for a slice of the pie chart. - * @returns {number} performance - A variable updated with the rounded sum of weighted values. - */ - 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: 73, - outerRadius: 80, - cornerRadius: 2, - highlightScope: { faded: "global", highlighted: "series" }, - faded: { - innerRadius: 63, - outerRadius: 70, - additionalRadius: -20, - arcLabelRadius: 5, - }, - cx: pieSize.width / 2, - }); - - performance += Math.floor(value); - startAngle = endAngle + padding; - } - }); - - return props; - }; - - const pieSize = { width: 210, height: 200 }; - const pieData = getPieData(audits); - const colorMap = getColors(performance); - - const [highlightedItem, setHighLightedItem] = useState(null); - const [expand, setExpand] = useState(false); - let loading = Object.keys(monitor).length === 0; const data = monitor?.checks ? [...monitor.checks].reverse() : []; @@ -293,6 +174,38 @@ const PageSpeedDetails = () => { setMetrics((prev) => ({ ...prev, [id]: !prev[id] })); }; + /** + * Retrieves color properties based on the performance value. + * + * @param {number} value - The performance score used to determine the color properties. + * @returns {{stroke: string, text: string, shape: string}} The color properties for the given performance value. + */ + const getColors = (value) => { + if (value >= 90 && value <= 100) + return { + stroke: theme.palette.success.main, + text: theme.palette.success.text, + shape: "circle", + }; + else if (value >= 50 && value < 90) + return { + stroke: theme.palette.warning.main, + text: theme.palette.warning.text, + shape: "square", + }; + else if (value >= 0 && value < 50) + return { + stroke: theme.palette.error.text, + text: theme.palette.error.text, + shape: "circle", + }; + return { + stroke: theme.palette.unresolved.main, + text: theme.palette.unresolved.main, + shape: "", + }; + }; + return ( {loading ? ( @@ -499,7 +412,8 @@ const PageSpeedDetails = () => { @@ -512,98 +426,10 @@ const PageSpeedDetails = () => { textAlign="center" minWidth="300px" flex={1} - px={theme.spacing(20)} - py={theme.spacing(8)} + p={theme.spacing(12)} > - setExpand(false)}> - {expand ? ( - - - {pieData?.map((pie) => ( - - ))} - - ) : ( - - - - )} - - + + Values are estimated and may vary.{" "} ({ fontSize: 15, fontWeight: 500, }, - "& p": { color: theme.palette.text.secondary }, + "& p": { color: theme.palette.text.tertiary }, "& > :nth-of-type(1)": { gridColumn: 1, gridRow: 1, diff --git a/Client/src/Utils/Theme/lightTheme.js b/Client/src/Utils/Theme/lightTheme.js index f00ece091..92e7f7817 100644 --- a/Client/src/Utils/Theme/lightTheme.js +++ b/Client/src/Utils/Theme/lightTheme.js @@ -51,7 +51,7 @@ const lightTheme = createTheme({ text: "#DC6803", main: "#fdb022", light: "#ffecbc", - bg: "#fffcf5", + bg: "#fef8ea", border: "#fec84b", }, percentage: {