From 7f0faa5b2e94c94ce21b684be7db6b8a8b555955 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Tue, 9 Aug 2022 18:00:22 -0400 Subject: [PATCH] Code review --- components/PieChart.module.css | 8 ++-- components/PieChart.tsx | 83 +++++++++++++++++++++++++++------- 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/components/PieChart.module.css b/components/PieChart.module.css index e7e3414..29bb6dc 100644 --- a/components/PieChart.module.css +++ b/components/PieChart.module.css @@ -3,14 +3,12 @@ } .labelPath { - fill: var(--primary-background); fill-opacity: 0; } .pieText, .labelText { fill: var(--label); - font-size: 40px; font-weight: 800; } @@ -20,9 +18,9 @@ .group:hover > .piePath { fill: var(--primary-accent); - filter: drop-shadow(0px 0px 6px var(--primary-accent)); + filter: drop-shadow(0px 0px calc(6rem / 16) var(--primary-accent)); } -.group:hover > .pieText { - display: inline +.group:hover .pieText { + display: inline; } \ No newline at end of file diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 606a310..d860115 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -1,15 +1,28 @@ import { Group } from "@visx/group"; import Pie, { ProvidedProps } from "@visx/shape/lib/shapes/Pie"; +import { Text } from "@visx/text"; import React from "react"; import styles from "./PieChart.module.css"; interface PieChartProps { data: PieChartData[]; + /** Width of the entire graph, including labels, in pixels. */ width: number; + /** Width of the outer ring of labels, in pixels. */ labelWidth: number; + /** Distance between pie slices, in pixels */ padRadius?: number; + /** Distance of gap in center of pie graph, in pixels */ innerRadius?: number; + /** Font size of text inside the pie, in pixels*/ + pieTextSize?: number; + /** Font size of labels outside the pie, in pixels */ + labelTextSize?: number; + /** X-axis offset of the pie text, in pixels */ + pieTextXOffset?: number; + /** Y-axis offset of the pie text, in pixels */ + pieTextYOffset?: number; className?: string; } @@ -19,18 +32,23 @@ interface PieChartData { } export function PieChart({ + data, width, labelWidth, padRadius = width * 0.35, innerRadius = width * 0.015, - ...props + pieTextSize = 40, + labelTextSize = 40, + pieTextXOffset = 0, + pieTextYOffset = 10, + className, }: PieChartProps) { const pieWidth = width * 0.5 - labelWidth; return ( - + d.value} cornerRadius={10} padAngle={0.075} @@ -38,47 +56,78 @@ export function PieChart({ innerRadius={innerRadius} outerRadius={pieWidth} > - {(pie) => } + {(pie) => ( + + )} d.value} innerRadius={pieWidth} outerRadius={width * 0.5} > - {(pie) => } + {(pie) => ( + + )} ); } -type PieSlicesProps = ProvidedProps & { +type PieSliceProps = ProvidedProps & { isLabel: boolean; + pieTextSize: number; + labelTextSize: number; + pieTextXOffset: number; + pieTextYOffset: number; }; -export function PieSlices({ isLabel, ...props }: PieSlicesProps) { +export function PieSlice({ + path, + arcs, + isLabel, + pieTextSize, + labelTextSize, + pieTextXOffset, + pieTextYOffset, +}: PieSliceProps) { return ( <> - {props.arcs.map((arc) => { - const [centroidX, centroidY] = props.path.centroid(arc); - const pathArc = props.path(arc) as string; + {arcs.map((arc) => { + const [centroidX, centroidY] = path.centroid(arc); + const pathArc = path(arc) as string; return ( - + - {isLabel ? `${arc.data.category}` : `${arc.data.value}%`} - - + + ); })}