From b025958cd804fb1652ebd25f961af1d8fc07d683 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Fri, 17 Jun 2022 22:37:05 -0400 Subject: [PATCH 01/17] Add initial pie and pieslice components --- .vscode/settings.json | 6 +++- components/PieChart.module.css | 8 +++++ components/PieChart.tsx | 63 ++++++++++++++++++++++++++++++++++ data/mocks.ts | 15 ++++++++ package-lock.json | 2 +- package.json | 1 + pages/playground.tsx | 9 +++++ 7 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 components/PieChart.module.css create mode 100644 components/PieChart.tsx diff --git a/.vscode/settings.json b/.vscode/settings.json index 02cf98f..12f41f3 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -41,7 +41,11 @@ "files.eol": "\n", "[markdown]": { "editor.wordWrap": "on", - "editor.quickSuggestions": false, + "editor.quickSuggestions": { + "comments": "off", + "strings": "off", + "other": "off" + }, "editor.tabSize": 4 } } diff --git a/components/PieChart.module.css b/components/PieChart.module.css new file mode 100644 index 0000000..5c85fd3 --- /dev/null +++ b/components/PieChart.module.css @@ -0,0 +1,8 @@ +.path { + fill: #354265; +} + +.path:hover { + fill: #EF839D; + filter: drop-shadow(3px 3px 2px rgba(0, 0, 0, .7)); +} \ No newline at end of file diff --git a/components/PieChart.tsx b/components/PieChart.tsx new file mode 100644 index 0000000..02a2258 --- /dev/null +++ b/components/PieChart.tsx @@ -0,0 +1,63 @@ +import { Group } from "@visx/group"; +import Pie, { ProvidedProps } from "@visx/shape/lib/shapes/Pie"; +import React, { useState } from "react"; + +import styles from "./PieChart.module.css"; + +interface PieChartProps { + data: PieChartData[]; + width: number; + height: number; + margin: { + top: number; + bottom: number; + left: number; + right: number; + }; + className?: string; +} + +interface PieChartData { + category: string; + value: number; +} + +export function PieChart(props: PieChartProps) { + return ( + + + d.value} + outerRadius={100} + cornerRadius={3} + padAngle={0.1} + > + {(pie) => } + + + + ); +} + +// path, arcs, pie +type PieSliceProps = ProvidedProps; + +export function PieSlice(props: PieSliceProps) { + return props.arcs.map((arc) => { + const [centroidX, centroidY] = props.path.centroid(arc); + const pathArc = props.path(arc); + + return ( + + + {`${arc.data.value} %`} + + ); + }); +} diff --git a/data/mocks.ts b/data/mocks.ts index f7e9235..7a00c54 100644 --- a/data/mocks.ts +++ b/data/mocks.ts @@ -35,3 +35,18 @@ export const mockCategoricalData = [ value: 29, }, ]; + +export const mockPieData = [ + { + category: "Nightingale", + value: 42, + }, + { + category: "Quail", + value: 38, + }, + { + category: "Cuckoo", + value: 19, + }, +]; diff --git a/package-lock.json b/package-lock.json index a0d527f..b7fa4a0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,11 +5,11 @@ "requires": true, "packages": { "": { - "name": "cs-2022-class-profile", "version": "0.1.0", "dependencies": { "@visx/axis": "^2.10.0", "@visx/grid": "^2.10.0", + "@visx/group": "^2.10.0", "@visx/shape": "^2.10.0", "next": "12.1.6", "react": "18.1.0", diff --git a/package.json b/package.json index 5ac9543..da3f166 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "dependencies": { "@visx/axis": "^2.10.0", "@visx/grid": "^2.10.0", + "@visx/group": "^2.10.0", "@visx/shape": "^2.10.0", "next": "12.1.6", "react": "18.1.0", diff --git a/pages/playground.tsx b/pages/playground.tsx index b31b37e..28a222d 100644 --- a/pages/playground.tsx +++ b/pages/playground.tsx @@ -1,10 +1,19 @@ +import { mockPieData } from "data/mocks"; import React from "react"; +import { PieChart } from "@/components/PieChart"; + export default function Home() { return ( <>

Playground

Show off your components here!

+ ); } -- 2.39.2 From 122d5a24509906429d53d66ab2e570f6c375fcec Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Sat, 18 Jun 2022 20:46:00 -0400 Subject: [PATCH 02/17] Add hover styles --- components/PieChart.module.css | 13 +++++++++++-- components/PieChart.tsx | 7 ++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/components/PieChart.module.css b/components/PieChart.module.css index 5c85fd3..800692e 100644 --- a/components/PieChart.module.css +++ b/components/PieChart.module.css @@ -2,7 +2,16 @@ fill: #354265; } -.path:hover { +.text { + display: none; + fill: white; +} + +.group:hover>.path { fill: #EF839D; - filter: drop-shadow(3px 3px 2px rgba(0, 0, 0, .7)); + filter: drop-shadow(0px 0px 6px #CC5773); +} + +.group:hover>.text { + display: inline } \ No newline at end of file diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 02a2258..0342ddb 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -50,13 +50,14 @@ export function PieSlice(props: PieSliceProps) { const pathArc = props.path(arc); return ( - - + + {`${arc.data.value} %`} + >{`${arc.data.value}%`} ); }); -- 2.39.2 From ea6e2d4c7a164bf6c52176b1daa5a537f94b72b2 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Sat, 18 Jun 2022 20:55:04 -0400 Subject: [PATCH 03/17] Reduce corner --- components/PieChart.module.css | 1 + components/PieChart.tsx | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/components/PieChart.module.css b/components/PieChart.module.css index 800692e..699ab34 100644 --- a/components/PieChart.module.css +++ b/components/PieChart.module.css @@ -5,6 +5,7 @@ .text { display: none; fill: white; + font-weight: 800; } .group:hover>.path { diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 0342ddb..0034c70 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -31,7 +31,7 @@ export function PieChart(props: PieChartProps) { fill="aqua" pieValue={(d: PieChartData) => d.value} outerRadius={100} - cornerRadius={3} + cornerRadius={5} padAngle={0.1} > {(pie) => } -- 2.39.2 From b931afd7cce5c162a2563cdcf46a526ce87be765 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Sat, 18 Jun 2022 22:35:39 -0400 Subject: [PATCH 04/17] Add labels --- components/PieChart.module.css | 24 +++++++--- components/PieChart.tsx | 80 +++++++++++++++++++++------------- pages/playground.tsx | 9 ++-- 3 files changed, 69 insertions(+), 44 deletions(-) diff --git a/components/PieChart.module.css b/components/PieChart.module.css index 699ab34..15e569a 100644 --- a/components/PieChart.module.css +++ b/components/PieChart.module.css @@ -1,18 +1,28 @@ -.path { +.piePath { fill: #354265; } -.text { - display: none; - fill: white; - font-weight: 800; +.labelPath { + fill: var(--primary-background); } -.group:hover>.path { +.pieText, +.labelText { + fill: white; + font-size: 20px; + font-weight: 800; + font-family: "Inconsolata", "sans-serif"; +} + +.pieText { + display: none; +} + +.group:hover>.piePath { fill: #EF839D; filter: drop-shadow(0px 0px 6px #CC5773); } -.group:hover>.text { +.group:hover>.pieText { display: inline } \ No newline at end of file diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 0034c70..91225d5 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -1,4 +1,5 @@ import { Group } from "@visx/group"; +import { Arc } from "@visx/shape"; import Pie, { ProvidedProps } from "@visx/shape/lib/shapes/Pie"; import React, { useState } from "react"; @@ -7,13 +8,7 @@ import styles from "./PieChart.module.css"; interface PieChartProps { data: PieChartData[]; width: number; - height: number; - margin: { - top: number; - bottom: number; - left: number; - right: number; - }; + textPadding: number; className?: string; } @@ -22,19 +17,31 @@ interface PieChartData { value: number; } -export function PieChart(props: PieChartProps) { +export function PieChart({ width, textPadding, ...props }: PieChartProps) { + const pieWidth = width * 0.5 - textPadding; return ( - - + + d.value} - outerRadius={100} - cornerRadius={5} - padAngle={0.1} + cornerRadius={10} + padAngle={0.075} + padRadius={width * 0.7} + innerRadius={width * 0.03} + outerRadius={pieWidth} > - {(pie) => } + {(pie) => } + + d.value} + innerRadius={pieWidth} + outerRadius={width * 0.5} + > + {(pie) => } @@ -42,23 +49,34 @@ export function PieChart(props: PieChartProps) { } // path, arcs, pie -type PieSliceProps = ProvidedProps; +type PieSlicesProps = ProvidedProps & { + isLabel: boolean; +}; -export function PieSlice(props: PieSliceProps) { - return props.arcs.map((arc) => { - const [centroidX, centroidY] = props.path.centroid(arc); - const pathArc = props.path(arc); +export function PieSlices({ isLabel, ...props }: PieSlicesProps) { + return ( + <> + {props.arcs.map((arc) => { + const [centroidX, centroidY] = props.path.centroid(arc); + const pathArc = props.path(arc); - return ( - - - {`${arc.data.value}%`} - - ); - }); + return ( + + + + {isLabel ? `${arc.data.category}` : `${arc.data.value}%`} + + + ); + })} + + ); } diff --git a/pages/playground.tsx b/pages/playground.tsx index 28a222d..e2b7201 100644 --- a/pages/playground.tsx +++ b/pages/playground.tsx @@ -8,12 +8,9 @@ export default function Home() { <>

Playground

Show off your components here!

- +
+ +
); } -- 2.39.2 From dfa31a0bd79a814bcc63ca9042f60930534e4ebf Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Sat, 18 Jun 2022 22:50:08 -0400 Subject: [PATCH 05/17] Cleanup --- components/PieChart.module.css | 3 +-- components/PieChart.tsx | 9 ++++----- data/mocks.ts | 2 +- pages/playground.tsx | 2 +- 4 files changed, 7 insertions(+), 9 deletions(-) diff --git a/components/PieChart.module.css b/components/PieChart.module.css index 15e569a..dddf23b 100644 --- a/components/PieChart.module.css +++ b/components/PieChart.module.css @@ -9,9 +9,8 @@ .pieText, .labelText { fill: white; - font-size: 20px; + font-size: 40px; font-weight: 800; - font-family: "Inconsolata", "sans-serif"; } .pieText { diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 91225d5..41df69c 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -1,7 +1,6 @@ import { Group } from "@visx/group"; -import { Arc } from "@visx/shape"; import Pie, { ProvidedProps } from "@visx/shape/lib/shapes/Pie"; -import React, { useState } from "react"; +import React from "react"; import styles from "./PieChart.module.css"; @@ -28,8 +27,8 @@ export function PieChart({ width, textPadding, ...props }: PieChartProps) { pieValue={(d: PieChartData) => d.value} cornerRadius={10} padAngle={0.075} - padRadius={width * 0.7} - innerRadius={width * 0.03} + padRadius={width * 0.35} + innerRadius={width * 0.015} outerRadius={pieWidth} > {(pie) => } @@ -58,7 +57,7 @@ export function PieSlices({ isLabel, ...props }: PieSlicesProps) { <> {props.arcs.map((arc) => { const [centroidX, centroidY] = props.path.centroid(arc); - const pathArc = props.path(arc); + const pathArc = props.path(arc) as string; return ( diff --git a/data/mocks.ts b/data/mocks.ts index 7a00c54..5b9827e 100644 --- a/data/mocks.ts +++ b/data/mocks.ts @@ -47,6 +47,6 @@ export const mockPieData = [ }, { category: "Cuckoo", - value: 19, + value: 20, }, ]; diff --git a/pages/playground.tsx b/pages/playground.tsx index e2b7201..897013b 100644 --- a/pages/playground.tsx +++ b/pages/playground.tsx @@ -9,7 +9,7 @@ export default function Home() {

Playground

Show off your components here!

- +
); -- 2.39.2 From 846f4549b0392691ee92e1bdfcc1a5b4e3e54edc Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Sat, 18 Jun 2022 22:51:19 -0400 Subject: [PATCH 06/17] Remove unnecessary comment --- components/PieChart.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 41df69c..363b878 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -47,7 +47,6 @@ export function PieChart({ width, textPadding, ...props }: PieChartProps) { ); } -// path, arcs, pie type PieSlicesProps = ProvidedProps & { isLabel: boolean; }; -- 2.39.2 From a5fa8bd52a2b9152503870f3d9233dc8125514d9 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Sat, 18 Jun 2022 22:55:05 -0400 Subject: [PATCH 07/17] Change variable name --- components/PieChart.tsx | 6 +++--- pages/playground.tsx | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 363b878..ad9bed6 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -7,7 +7,7 @@ import styles from "./PieChart.module.css"; interface PieChartProps { data: PieChartData[]; width: number; - textPadding: number; + labelWidth: number; className?: string; } @@ -16,8 +16,8 @@ interface PieChartData { value: number; } -export function PieChart({ width, textPadding, ...props }: PieChartProps) { - const pieWidth = width * 0.5 - textPadding; +export function PieChart({ width, labelWidth, ...props }: PieChartProps) { + const pieWidth = width * 0.5 - labelWidth; return ( diff --git a/pages/playground.tsx b/pages/playground.tsx index 897013b..de0f729 100644 --- a/pages/playground.tsx +++ b/pages/playground.tsx @@ -9,7 +9,7 @@ export default function Home() {

Playground

Show off your components here!

- +
); -- 2.39.2 From c1a6ac2194076feb53c25e81ab5dbec907cfc5cb Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 6 Jul 2022 19:44:48 -0400 Subject: [PATCH 08/17] Use global vars --- components/PieChart.module.css | 6 +++--- components/PieChart.tsx | 14 +++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/components/PieChart.module.css b/components/PieChart.module.css index dddf23b..c45a588 100644 --- a/components/PieChart.module.css +++ b/components/PieChart.module.css @@ -8,7 +8,7 @@ .pieText, .labelText { - fill: white; + fill: var(--label); font-size: 40px; font-weight: 800; } @@ -18,8 +18,8 @@ } .group:hover>.piePath { - fill: #EF839D; - filter: drop-shadow(0px 0px 6px #CC5773); + fill: var(--primary-accent); + filter: drop-shadow(0px 0px 6px var(--primary-accent)); } .group:hover>.pieText { diff --git a/components/PieChart.tsx b/components/PieChart.tsx index ad9bed6..8eeea0d 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -8,6 +8,8 @@ interface PieChartProps { data: PieChartData[]; width: number; labelWidth: number; + padRadius?: number; + innerRadius?: number; className?: string; } @@ -16,7 +18,13 @@ interface PieChartData { value: number; } -export function PieChart({ width, labelWidth, ...props }: PieChartProps) { +export function PieChart({ + width, + labelWidth, + padRadius = width * 0.35, + innerRadius = width * 0.015, + ...props +}: PieChartProps) { const pieWidth = width * 0.5 - labelWidth; return ( @@ -27,8 +35,8 @@ export function PieChart({ width, labelWidth, ...props }: PieChartProps) { pieValue={(d: PieChartData) => d.value} cornerRadius={10} padAngle={0.075} - padRadius={width * 0.35} - innerRadius={width * 0.015} + padRadius={padRadius} + innerRadius={innerRadius} outerRadius={pieWidth} > {(pie) => } -- 2.39.2 From 165c1d1061cbcd3426be7ac3dc8393d5296de32f Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 6 Jul 2022 19:55:29 -0400 Subject: [PATCH 09/17] Add y-offset --- components/PieChart.tsx | 6 +++--- data/mocks.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 8eeea0d..30586ae 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -74,11 +74,11 @@ export function PieSlices({ isLabel, ...props }: PieSlicesProps) { /> - {isLabel ? `${arc.data.category}` : `${arc.data.value}%`} + {isLabel ? `${arc.data.category}` : `${arc.data.value}%`}
); diff --git a/data/mocks.ts b/data/mocks.ts index 5b9827e..e6acbca 100644 --- a/data/mocks.ts +++ b/data/mocks.ts @@ -43,10 +43,10 @@ export const mockPieData = [ }, { category: "Quail", - value: 38, + value: 48, }, { category: "Cuckoo", - value: 20, + value: 10, }, ]; -- 2.39.2 From 4cc58ca27ac2465ef19ec2c4abbbdd5a55f7a75e Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 6 Jul 2022 19:56:06 -0400 Subject: [PATCH 10/17] Spacing --- components/PieChart.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 30586ae..2fa41b9 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -78,7 +78,7 @@ export function PieSlices({ isLabel, ...props }: PieSlicesProps) { y={isLabel ? centroidY : centroidY + 10} textAnchor="middle" > - {isLabel ? `${arc.data.category}` : `${arc.data.value}%`} + {isLabel ? `${arc.data.category}` : `${arc.data.value}%`}
); -- 2.39.2 From 4fdaefef826cc29be0a4055c0d9d5772697308fb Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 13 Jul 2022 20:07:14 -0400 Subject: [PATCH 11/17] Change margins_ --- pages/playground.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/playground.tsx b/pages/playground.tsx index e5df03d..520210d 100644 --- a/pages/playground.tsx +++ b/pages/playground.tsx @@ -11,7 +11,7 @@ export default function Home() {

Playground

Show off your components here!

- +
-- 2.39.2 From fc3512f72f8b8593a0d06231533626d95bdbc3d9 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 27 Jul 2022 23:27:09 -0400 Subject: [PATCH 12/17] Fix shadow not working --- components/PieChart.module.css | 7 ++++--- components/PieChart.tsx | 2 -- pages/playground.tsx | 6 ++++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/components/PieChart.module.css b/components/PieChart.module.css index c45a588..e7e3414 100644 --- a/components/PieChart.module.css +++ b/components/PieChart.module.css @@ -1,9 +1,10 @@ .piePath { - fill: #354265; + fill: var(--tertiary-background); } .labelPath { fill: var(--primary-background); + fill-opacity: 0; } .pieText, @@ -17,11 +18,11 @@ display: none; } -.group:hover>.piePath { +.group:hover > .piePath { fill: var(--primary-accent); filter: drop-shadow(0px 0px 6px var(--primary-accent)); } -.group:hover>.pieText { +.group:hover > .pieText { display: inline } \ No newline at end of file diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 2fa41b9..606a310 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -31,7 +31,6 @@ export function PieChart({ d.value} cornerRadius={10} padAngle={0.075} @@ -43,7 +42,6 @@ export function PieChart({ d.value} innerRadius={pieWidth} outerRadius={width * 0.5} diff --git a/pages/playground.tsx b/pages/playground.tsx index 9232289..6c1b881 100644 --- a/pages/playground.tsx +++ b/pages/playground.tsx @@ -1,6 +1,5 @@ -import { mockPieData } from "data/mocks"; import { BarGraphHorizontal, BarGraphVertical } from "components/BarGraph"; -import { mockCategoricalData } from "data/mocks"; +import { mockPieData, mockCategoricalData } from "data/mocks"; import React from "react"; import { PieChart } from "@/components/PieChart"; @@ -14,6 +13,9 @@ export default function Home() {

Playground

Show off your components here!

+

+ {""} +

-- 2.39.2 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 13/17] 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}%`} - - + + ); })} -- 2.39.2 From 29fdbffab185a6518fbe02031b7e7d5df127ac3b Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 31 Aug 2022 19:50:22 -0400 Subject: [PATCH 14/17] Some refactoring --- components/PieChart.tsx | 66 +++++++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/components/PieChart.tsx b/components/PieChart.tsx index d860115..7df31ee 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -59,9 +59,7 @@ export function PieChart({ {(pie) => ( @@ -73,16 +71,7 @@ export function PieChart({ innerRadius={pieWidth} outerRadius={width * 0.5} > - {(pie) => ( - - )} + {(pie) => } @@ -90,9 +79,7 @@ export function PieChart({ } type PieSliceProps = ProvidedProps & { - isLabel: boolean; pieTextSize: number; - labelTextSize: number; pieTextXOffset: number; pieTextYOffset: number; }; @@ -100,9 +87,7 @@ type PieSliceProps = ProvidedProps & { export function PieSlice({ path, arcs, - isLabel, pieTextSize, - labelTextSize, pieTextXOffset, pieTextYOffset, }: PieSliceProps) { @@ -114,18 +99,49 @@ export function PieSlice({ return ( - + - {isLabel ? `${arc.data.category}` : `${arc.data.value}%`} + {`${arc.data.value}%`} + + + ); + })} + + ); +} + +type PieSliceLabelProps = ProvidedProps & { + labelTextSize: number; +}; + +export function PieSliceLabel({ + path, + arcs, + labelTextSize, +}: PieSliceLabelProps) { + return ( + <> + {arcs.map((arc) => { + const [centroidX, centroidY] = path.centroid(arc); + const pathArc = path(arc) as string; + + return ( + + + + {`${arc.data.category}`} ); -- 2.39.2 From 28a39696db0a4ae7b81800408337765fe06acaed Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 31 Aug 2022 20:13:02 -0400 Subject: [PATCH 15/17] Refactor into PieSlice and PieSliceLabel --- components/PieChart.tsx | 45 +++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 7df31ee..1d89baa 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -17,12 +17,20 @@ interface PieChartProps { 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; + /** Accessor function to get value to display as pie text from datum */ + getPieDisplayValueFromDatum?: (datum: PieChartData) => string; + /** Font size of labels outside the pie, in pixels */ + labelTextSize?: number; + /** X-axis offset of the label text, in pixels */ + labelTextXOffset?: number; + /** Y-axis offset of the label text, in pixels */ + labelTextYOffset?: number; + /** Accessor function to get value to display as label text from datum */ + getLabelDisplayValueFromDatum?: (datum: PieChartData) => string; className?: string; } @@ -38,9 +46,13 @@ export function PieChart({ padRadius = width * 0.35, innerRadius = width * 0.015, pieTextSize = 40, - labelTextSize = 40, pieTextXOffset = 0, pieTextYOffset = 10, + getPieDisplayValueFromDatum = (datum: PieChartData) => `${datum.value}%`, + labelTextSize = 40, + labelTextXOffset = 0, + labelTextYOffset = 0, + getLabelDisplayValueFromDatum = (datum: PieChartData) => `${datum.category}`, className, }: PieChartProps) { const pieWidth = width * 0.5 - labelWidth; @@ -62,6 +74,7 @@ export function PieChart({ pieTextSize={pieTextSize} pieTextXOffset={pieTextXOffset} pieTextYOffset={pieTextYOffset} + getPieDisplayValueFromDatum={getPieDisplayValueFromDatum} /> )} @@ -71,7 +84,15 @@ export function PieChart({ innerRadius={pieWidth} outerRadius={width * 0.5} > - {(pie) => } + {(pie) => ( + + )} @@ -82,6 +103,7 @@ type PieSliceProps = ProvidedProps & { pieTextSize: number; pieTextXOffset: number; pieTextYOffset: number; + getPieDisplayValueFromDatum: (datum: PieChartData) => string; }; export function PieSlice({ @@ -90,6 +112,7 @@ export function PieSlice({ pieTextSize, pieTextXOffset, pieTextYOffset, + getPieDisplayValueFromDatum, }: PieSliceProps) { return ( <> @@ -107,7 +130,7 @@ export function PieSlice({ textAnchor="middle" fontSize={pieTextSize} > - {`${arc.data.value}%`} + {`${getPieDisplayValueFromDatum(arc.data)}`} ); @@ -118,12 +141,18 @@ export function PieSlice({ type PieSliceLabelProps = ProvidedProps & { labelTextSize: number; + labelTextXOffset: number; + labelTextYOffset: number; + getLabelDisplayValueFromDatum: (datum: PieChartData) => string; }; export function PieSliceLabel({ path, arcs, labelTextSize, + labelTextXOffset, + labelTextYOffset, + getLabelDisplayValueFromDatum, }: PieSliceLabelProps) { return ( <> @@ -136,12 +165,12 @@ export function PieSliceLabel({ - {`${arc.data.category}`} + {`${getLabelDisplayValueFromDatum(arc.data)}`} ); -- 2.39.2 From 321a455d02e85407bd050c75853f3038422f9557 Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 7 Sep 2022 20:49:11 -0400 Subject: [PATCH 16/17] Code review fixes --- components/PieChart.tsx | 22 +++++++++++----------- pages/playground.tsx | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/components/PieChart.tsx b/components/PieChart.tsx index 1d89baa..536ccfc 100644 --- a/components/PieChart.tsx +++ b/components/PieChart.tsx @@ -9,27 +9,27 @@ interface PieChartProps { data: PieChartData[]; /** Width of the entire graph, including labels, in pixels. */ width: number; - /** Width of the outer ring of labels, in pixels. */ + /** Width of the outer ring of labels, in pixels. Label text may be cut off if specified value is too small. */ labelWidth: number; - /** Distance between pie slices, in pixels */ + /** Distance between pie slices, in pixels. */ padRadius?: number; - /** Distance of gap in center of pie graph, in pixels */ + /** Distance of gap in center of pie graph, in pixels. */ innerRadius?: number; - /** Font size of text inside the pie, in pixels*/ + /** Font size of text inside the pie, in pixels. */ pieTextSize?: number; - /** X-axis offset of the pie text, in pixels */ + /** X-axis offset of the pie text, in pixels. */ pieTextXOffset?: number; - /** Y-axis offset of the pie text, in pixels */ + /** Y-axis offset of the pie text, in pixels. */ pieTextYOffset?: number; - /** Accessor function to get value to display as pie text from datum */ + /** Accessor function to get value to display as pie text from datum. */ getPieDisplayValueFromDatum?: (datum: PieChartData) => string; - /** Font size of labels outside the pie, in pixels */ + /** Font size of labels outside the pie, in pixels. */ labelTextSize?: number; - /** X-axis offset of the label text, in pixels */ + /** X-axis offset of the label text, in pixels. */ labelTextXOffset?: number; - /** Y-axis offset of the label text, in pixels */ + /** Y-axis offset of the label text, in pixels. */ labelTextYOffset?: number; - /** Accessor function to get value to display as label text from datum */ + /** Accessor function to get value to display as label text from datum. */ getLabelDisplayValueFromDatum?: (datum: PieChartData) => string; className?: string; } diff --git a/pages/playground.tsx b/pages/playground.tsx index c23fd9f..b36e9c7 100644 --- a/pages/playground.tsx +++ b/pages/playground.tsx @@ -17,7 +17,7 @@ export default function Home() { return (

Playground

-

Show off your components here!

`` +

Show off your components here!

{""}

-- 2.39.2 From 0bb0341b51a16723c45eced5cb4d7f3472d7ec0a Mon Sep 17 00:00:00 2001 From: Jared He <66887902+jaredjhe@users.noreply.github.com> Date: Wed, 7 Sep 2022 22:07:14 -0400 Subject: [PATCH 17/17] Add the PieChart --- pages/playground.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/pages/playground.tsx b/pages/playground.tsx index 0d9224d..426da06 100644 --- a/pages/playground.tsx +++ b/pages/playground.tsx @@ -10,6 +10,7 @@ import { } from "data/mocks"; import React from "react"; +import { PieChart } from "@/components/PieChart"; import { QuotationCarousel } from "@/components/QuotationCarousel"; import { ColorPalette } from "../components/ColorPalette"; -- 2.39.2