Refactor default prop values into util file
continuous-integration/drone/push Build is passing Details

This commit is contained in:
e26chiu 2022-11-14 12:53:03 -05:00
parent aa25b8451b
commit 4ee9ed3d8d
2 changed files with 71 additions and 58 deletions

View File

@ -13,6 +13,12 @@ import {
} from "data/demographics";
import { pageRoutes } from "data/routes";
import React from "react";
import {
pieChartWidth,
DefaultProp,
barGraphMargin,
barGraphWidth,
} from "utils/defaultProps";
import { useWindowDimensions } from "utils/getWindowDimensions";
import { useIsMobile } from "utils/isMobile";
@ -30,14 +36,17 @@ export default function Demographics() {
const { width } = useWindowDimensions();
const isMobile = useIsMobile();
const defaultGraphWidth = isMobile ? width / 1.25 : width / 2;
const defaultPieChartWidth = isMobile ? width / 1.25 : width / 3;
const defaultGraphHeight = 500;
const defaultBarGraphProps = {
width: barGraphWidth(isMobile, width),
height: DefaultProp.graphHeight,
margin: barGraphMargin,
};
const defaultBarGraphMargin = { top: 20, bottom: 80, left: 60, right: 20 };
const defaultLabelSize = 24;
const defaultLabelWidth = 100;
const defaultPieChartProps = {
width: pieChartWidth(isMobile, width),
labelWidth: DefaultProp.labelWidth,
labelTextSize: DefaultProp.labelSize,
};
return (
<div className={styles.page}>
@ -52,11 +61,7 @@ export default function Demographics() {
bodyText="There are a total of 106 respondents of the CS Class Profile. Interestingly, there are a huge number of students that are just in CS, partially due to the overwhelming number of people in CS as seen in the total demographics."
>
<div className={styles.graphContainer}>
<PieChart
data={D1}
width={defaultPieChartWidth}
labelWidth={defaultLabelWidth}
/>
<PieChart data={D1} {...defaultPieChartProps} />
</div>
</ComponentWrapper>
@ -67,23 +72,13 @@ export default function Demographics() {
noBackground
>
<div className={styles.graphContainer}>
<PieChart
data={D2}
width={defaultPieChartWidth}
labelWidth={defaultLabelWidth}
labelTextSize={defaultLabelSize}
/>
<PieChart data={D2} {...defaultPieChartProps} />
</div>
</ComponentWrapper>
<ComponentWrapper heading="Please indicate the pronouns that you use.">
<div className={styles.graphContainer}>
<PieChart
data={D3}
width={defaultPieChartWidth}
labelWidth={defaultLabelWidth}
labelTextSize={defaultLabelSize}
/>
<PieChart data={D3} {...defaultPieChartProps} />
</div>
</ComponentWrapper>
@ -93,24 +88,14 @@ export default function Demographics() {
align="left"
noBackground
>
<BarGraphVertical
data={D4}
width={defaultGraphWidth}
height={defaultGraphHeight}
margin={defaultBarGraphMargin}
/>
<BarGraphVertical data={D4} {...defaultBarGraphProps} />
</ComponentWrapper>
<ComponentWrapper
heading="What was your high school admissions average?"
bodyText="With a mean average of roughly 95.5%, getting into any of these programs is no easy feat. That makes everyone special from the day they were admitted into the school! :)"
>
<BarGraphVertical
data={D5}
width={defaultGraphWidth}
height={defaultGraphHeight}
margin={defaultBarGraphMargin}
/>
<BarGraphVertical data={D5} {...defaultBarGraphProps} />
</ComponentWrapper>
<ComponentWrapper
@ -121,9 +106,7 @@ export default function Demographics() {
>
<BarGraphVertical
data={D6}
width={defaultGraphWidth}
height={defaultGraphHeight}
margin={defaultBarGraphMargin}
{...defaultBarGraphProps}
widthAlternatingLabel={700}
/>
</ComponentWrapper>
@ -136,7 +119,7 @@ export default function Demographics() {
<WordCloud
data={D7}
width={isMobile ? width / 1.5 : 800}
height={defaultGraphHeight}
height={DefaultProp.graphHeight}
/>
</ComponentWrapper>
@ -145,12 +128,7 @@ export default function Demographics() {
bodyText="Theres quite a bit of spread in this chart! The real question is, how many of us are matching our parents levels of education? Find out later in the Class Profile…"
noBackground
>
<BarGraphVertical
data={D8}
width={defaultGraphWidth}
height={defaultGraphHeight}
margin={defaultBarGraphMargin}
/>
<BarGraphVertical data={D8} {...defaultBarGraphProps} />
</ComponentWrapper>
<ComponentWrapper
@ -161,9 +139,7 @@ export default function Demographics() {
<BarGraphVertical
// TODO: change when histogram component is ready
data={D9}
width={defaultGraphWidth}
height={defaultGraphHeight}
margin={defaultBarGraphMargin}
{...defaultBarGraphProps}
/>
</ComponentWrapper>
@ -173,12 +149,7 @@ export default function Demographics() {
align="left"
noBackground
>
<BarGraphVertical
data={D10}
width={defaultGraphWidth}
height={defaultGraphHeight}
margin={defaultBarGraphMargin}
/>
<BarGraphVertical data={D10} {...defaultBarGraphProps} />
</ComponentWrapper>
<ComponentWrapper
@ -188,9 +159,9 @@ export default function Demographics() {
>
<BarGraphHorizontal
data={D11}
width={defaultGraphWidth}
height={defaultGraphHeight}
margin={{ ...defaultBarGraphMargin, ...{ left: 220 } }}
width={barGraphWidth(isMobile, width)}
height={DefaultProp.graphHeight}
margin={{ ...barGraphMargin, ...{ left: 220 } }}
/>
</ComponentWrapper>

42
utils/defaultProps.ts Normal file
View File

@ -0,0 +1,42 @@
// Only static number props are included for this list
const propNames = ["graphHeight", "labelSize", "labelWidth"] as const;
// This type is needed for smart autocomplete
type PropName = typeof propNames[number];
const mobileBarGraphFactor = 1.25;
const desktopBarGraphFactor = 2;
const mobilePieChartFactor = 1.25;
const desktopPieChartFactor = 3;
const graphWidth = (
isMobile: boolean,
width: number,
isPieChart: boolean
): number => {
const mobileFactor = isPieChart ? mobilePieChartFactor : mobileBarGraphFactor;
const desktopFactor = isPieChart
? desktopPieChartFactor
: desktopBarGraphFactor;
return isMobile ? width / mobileFactor : width / desktopFactor;
};
export const barGraphWidth = (isMobile: boolean, width: number) =>
graphWidth(isMobile, width, false);
export const pieChartWidth = (isMobile: boolean, width: number) =>
graphWidth(isMobile, width, true);
export const barGraphMargin = {
top: 20,
bottom: 80,
left: 60,
right: 20,
};
export const DefaultProp: { [key in PropName]: number } = {
graphHeight: 500,
labelSize: 24,
labelWidth: 100,
};