Added onHover events to pie chart

This commit is contained in:
Daniel Cojocea committed 2024-08-05 17:51:53 -04:00
1 parent 6ed7e77079
commit 8b8bfb8b2e
1 file changed
+63 -45
+63 -45
View File
@@ -58,13 +58,16 @@ const StatBox = ({ icon, title, value }) => {
* @param {string} props.color - The color of the text. * @param {string} props.color - The color of the text.
* @returns {JSX.Element} * @returns {JSX.Element}
*/ */
const PieCenterLabel = ({ value, color }) => { const PieCenterLabel = ({ value, color, setExpand }) => {
const { width, height } = useDrawingArea(); const { width, height } = useDrawingArea();
return ( return (
<g
transform={`translate(${width / 2}, ${height / 2})`}
onMouseEnter={() => setExpand(true)}
>
<circle cx={0} cy={0} r={width / 3} fill="transparent" />
<text <text
className="pie-label" className="pie-label"
x={width / 2}
y={height / 2}
style={{ style={{
fill: color, fill: color,
fontSize: "45px", fontSize: "45px",
@@ -75,6 +78,7 @@ const PieCenterLabel = ({ value, color }) => {
> >
{value} {value}
</text> </text>
</g>
); );
}; };
@@ -305,11 +309,13 @@ const PageSpeedDetails = () => {
return props; return props;
}; };
const pieSize = { width: 300, height: 205 }; const pieSize = { width: 200, height: 200 };
const pieData = getPieData(data.audits); const pieData = getPieData(data.audits);
const colorMap = getColors(performance); const colorMap = getColors(performance);
const [highlightedItem, setHighLightedItem] = useState(null); const [highlightedItem, setHighLightedItem] = useState(null);
const [expand, setExpand] = useState(false);
console.log(expand);
return ( return (
<Stack className="page-speed-details" gap={theme.gap.large}> <Stack className="page-speed-details" gap={theme.gap.large}>
@@ -419,6 +425,53 @@ const PageSpeedDetails = () => {
<Typography component="h2">Performance report</Typography> <Typography component="h2">Performance report</Typography>
<Box p={theme.gap.ml}> <Box p={theme.gap.ml}>
<Stack mx="auto" width="fit-content" alignItems="center"> <Stack mx="auto" width="fit-content" alignItems="center">
<Box onMouseLeave={() => setExpand(false)}>
{expand ? (
<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 }}
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}
setExpand={setExpand}
/>
{pieData?.map((pie) => (
<PieValueLabel
key={pie.id}
value={pie.data[0].value}
startAngle={pie.startAngle}
endAngle={pie.endAngle}
color={pie.data[0].color}
highlighted={highlightedItem?.seriesId === pie.id}
/>
))}
</PieChart>
) : (
<PieChart <PieChart
series={[ series={[
{ {
@@ -452,49 +505,14 @@ const PageSpeedDetails = () => {
margin={{ left: 0, top: 0, right: 0, bottom: 0 }} margin={{ left: 0, top: 0, right: 0, bottom: 0 }}
tooltip={{ trigger: "none" }} tooltip={{ trigger: "none" }}
> >
<PieCenterLabel value={performance} color={colorMap.text} /> <PieCenterLabel
</PieChart> value={performance}
<PieChart color={colorMap.text}
series={[ setExpand={setExpand}
{
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} />
{pieData?.map((pie) => (
<PieValueLabel
key={pie.id}
value={pie.data[0].value}
startAngle={pie.startAngle}
endAngle={pie.endAngle}
color={pie.data[0].color}
highlighted={highlightedItem?.seriesId === pie.id}
/> />
))}
</PieChart> </PieChart>
)}
</Box>
<Typography component="h2" mt={theme.gap.xs}> <Typography component="h2" mt={theme.gap.xs}>
Performance Performance
</Typography> </Typography>