Add BoxPlot component (Closes #6) #34

Merged
e26chiu merged 9 commits from boxplot-component into main 2022-09-03 11:13:59 -04:00
5 changed files with 193 additions and 114 deletions
Showing only changes of commit dd0360e99d - Show all commits

View File

@ -1,5 +1,30 @@
.boxplot {
fill: var(--primary-accent-light);
}
.boxplot:hover {
fill: var(--primary-accent);
fill-opacity: 0.9;
filter: drop-shadow(0 0 calc(4rem / 16) var(--primary-accent));
}
.tooltip {
background-color: var(--label);
color: var(--primary-background);
padding: calc(10rem / 16);
}
.tooltip .category {
margin: 10px 0 0 0;
font-weight: 700;
e26chiu marked this conversation as resolved
Review

NIT: calc(10rem / 16)

NIT: `calc(10rem / 16)`
}
.tooltip .toolTipData {
margin-top: 5px;
margin-bottom: 10px;
font-size: 16px;
}
.tooltip .toolTipData p {
margin: 0;
padding: 0;
}

View File

@ -5,7 +5,7 @@ import { Stats } from "@visx/mock-data/lib/generators/genStats";
import { Point } from "@visx/point";
import { scaleBand, scaleLinear } from "@visx/scale";
import { Line } from "@visx/shape";
import { BoxPlot } from "@visx/stats";
import { BoxPlot as VisxBoxPlot } from "@visx/stats";
import {
withTooltip,
Tooltip,
@ -18,98 +18,131 @@ import { Color } from "utils/Color";
import styles from "./Boxplot.module.css";
const DEFAULT_LABEL_SIZE = 16;
const TICK_LABEL_FONT_SIZE = 800;
interface BoxPlotData {
a258wang marked this conversation as resolved
Review

NIT: maybe name this label or name or something, just to be a bit more descriptive?

NIT: maybe name this `label` or `name` or something, just to be a bit more descriptive?
Review

The name was left as x since the boxPlot property in the Stats object takes in a field named x. With the changes (of changing x to a more descriptive name), inserting the boxplot data would look like this:

boxPlot: {
      x: data[i].category,
      min: data[i].min,
      firstQuartile: data[i].firstQuartile,
      median: data[i].median,
      thirdQuartile: data[i].thirdQuartile,
      max: data[i].max,
      outliers: [],
},

since the BoxPlotData would have a different label name for x. If we don't implement the changes, inserting boxplot data would look like this:

boxPlot: data[i]

I was wondering whether the name change was really necessary. We could add a comment next to the field x in the interface BoxPlotData to explain its purpose.

The name was left as `x` since the `boxPlot` property in the `Stats` object takes in a field named `x`. With the changes (of changing `x` to a more descriptive name), inserting the boxplot data would look like this: ``` boxPlot: { x: data[i].category, min: data[i].min, firstQuartile: data[i].firstQuartile, median: data[i].median, thirdQuartile: data[i].thirdQuartile, max: data[i].max, outliers: [], }, ``` since the `BoxPlotData` would have a different label name for `x`. If we don't implement the changes, inserting boxplot data would look like this: ``` boxPlot: data[i] ``` I was wondering whether the name change was really necessary. We could add a comment next to the field `x` in the `interface BoxPlotData` to explain its purpose.
Review

We should be able to do

boxPlot: { ...data[i], x: data[i].category }

which is fairly concise, in my opinion. But ultimately it's up to you - a descriptive JSDoc comment in the interface would be a good idea either way. 🙂

We should be able to do ```typescript boxPlot: { ...data[i], x: data[i].category } ``` which is fairly concise, in my opinion. But ultimately it's up to you - a descriptive JSDoc comment in the interface would be a good idea either way. 🙂
x: string;
category: string;
min: number;
median: number;
max: number;
firstQuartile: number;
thirdQuartile: number;
outliers: number[];
outliers?: number[];
}
interface TooltipData {
name?: string;
min?: number;
median?: number;
max?: number;
firstQuartile?: number;
thirdQuartile?: number;
}
type TooltipData = Omit<BoxPlotData, "outliers">;
export type StatsPlotProps = {
width: number;
height: number;
data: BoxPlotData[];
/** Width of the entire graph, in pixels, greater than 10. */
width: number;
/** Height of the entire graph, in pixels. */
height: number;
/** Distance between the edge of the graph and the area where the bars are drawn, in pixels. */
margin: {
top: number;
left: number;
};
strokeWidth: number;
strokeDashArray: string;
/** Width of the lines in the graph, in px. */
strokeWidth?: number;
/** Length of the dashes and the gaps in the graph, in px. */
strokeDashArray?: string;
/** Number of ticks for the value (y-)axis */
numTicksLeftAxis?: number;
/** Distance between the boxplot and the top of the grid, in px. */
plotTopOffset?: number;
/** Distance between the left axis labels and the start of the lines of the graph, in px. */
valueAxisLeftOffset?: number;
/** Distance between the top and the first label of the y axis, in px. */
valueAxisLabelTopOffset?: number;
/** Distance between the left and the labels of the y axis, in px. */
valueAxisLabelLeftOffset?: number;
categoryAxisLabelSize?: number;
/** Distance between the left and the start of the first label of the x axis, in px. */
categoryAxisLabelLeftOffset?: number;
valueAxisLabelSize?: number;
/** Distance between the top and the column lines of the grid of the graph, in px. */
gridColumnTopOffset?: number;
/** Distance between the top and the row lines of the grid of the graph, in px. */
gridRowTopOffset?: number;
/** Distance between the top of the point in the boxplot and the start of the tooltip box, in px. */
toolTipTopOffset?: number;
/** Distance between the left of the point in the boxplot and the start of the tooltip box, in px. */
toolTipLeftOffset?: number;
/** Font size of the category (x-)axis labels */
categoryAxisLabelSize?: number;
/** Font size of the value (y-)axis labels */
valueAxisLabelSize?: number;
/** Font size of the text in the tool tip box */
toolTipFontSize?: number;
/** Grid column line extension, in px. */
lineExtension?: number;
/** Factor multiplied with the compressed width to determine the box width, in px. */
boxPlotWidthFactor?: number;
/** Factor multiplied with the compressed width to determine the distance between boxes, in px. */
boxPlotLeftOffset?: number;
};
export default withTooltip<StatsPlotProps, TooltipData>(
export const BoxPlot = withTooltip<StatsPlotProps, TooltipData>(
({
width,
height,
data,
margin,
strokeWidth,
strokeDashArray,
tooltipOpen,
tooltipLeft,
tooltipTop,
tooltipData,
showTooltip,
hideTooltip,
strokeWidth = 2.5,
strokeDashArray = "10,4",
numTicksLeftAxis = 6,
plotTopOffset = 10,
valueAxisLeftOffset = 40,
gridColumnTopOffset = -20,
gridRowTopOffset = 10,
valueAxisLabelTopOffset = 15,
valueAxisLabelLeftOffset = 10,
categoryAxisLabelLeftOffset = 30,
toolTipTopOffset = 20,
toolTipLeftOffset = 5,
categoryAxisLabelSize = DEFAULT_LABEL_SIZE,
valueAxisLabelSize = DEFAULT_LABEL_SIZE,
toolTipFontSize = DEFAULT_LABEL_SIZE,
lineExtension = 10,
boxPlotWidthFactor = 0.4,
boxPlotLeftOffset = 0.3,
}: StatsPlotProps & WithTooltipProvidedProps<TooltipData>) => {
// bounds
const xMax = width;
const yMax = height - 120;
// formatting data
const d: Stats[] = [];
for (let i = 0; i < data.length; i++) {
d.push({
boxPlot: data[i],
const plotData: Stats[] = data.map((d) => {
return {
boxPlot: {
...d,
x: d.category,
outliers: [],
},
binData: [],
});
}
};
});
// accessors
const x = (d: Stats) => d.boxPlot.x;
const min = (d: Stats) => d.boxPlot.min;
const max = (d: Stats) => d.boxPlot.max;
const median = (d: Stats) => d.boxPlot.median;
const firstQuartile = (d: Stats) => d.boxPlot.firstQuartile;
const thirdQuartile = (d: Stats) => d.boxPlot.thirdQuartile;
const getX = (d: Stats) => d.boxPlot.x;
const getMin = (d: Stats) => d.boxPlot.min;
const getMax = (d: Stats) => d.boxPlot.max;
const getMedian = (d: Stats) => d.boxPlot.median;
const getFirstQuartile = (d: Stats) => d.boxPlot.firstQuartile;
const getThirdQuartile = (d: Stats) => d.boxPlot.thirdQuartile;
// scales
const xScale = scaleBand<string>({
range: [18, xMax - 80], // scaling is needed due to the left offset
round: true,
domain: d.map(x),
domain: plotData.map(getX),
padding: 0.4,
});
const values = d.reduce((allValues, { boxPlot }) => {
const values = plotData.reduce((allValues, { boxPlot }) => {
allValues.push(boxPlot.min, boxPlot.max);
a258wang marked this conversation as resolved
Review

I'm curious what the +32 is for? Does this number depend on any other hard-coded values and/or props?

I'm curious what the +32 is for? Does this number depend on any other hard-coded values and/or props?
return allValues;
}, [] as number[]);
@ -122,20 +155,19 @@ export default withTooltip<StatsPlotProps, TooltipData>(
domain: [minYValue, maxYValue],
});
const boxWidth = xScale.bandwidth();
const constrainedWidth = Math.min(200, boxWidth);
const constrainedWidth = Math.min(200, xScale.bandwidth());
return width < 10 ? null : (
<div style={{ position: "relative" }}>
<div>
<svg width={width} height={height}>
<Group top={margin.top} left={margin.left}>
<GridRows
top={10}
top={gridRowTopOffset}
left={valueAxisLeftOffset}
scale={yScale}
width={xMax}
height={yMax}
numTicks={6}
numTicks={numTicksLeftAxis}
stroke={Color.tertiaryBackground}
strokeWidth={strokeWidth}
strokeDasharray={strokeDashArray}
@ -143,7 +175,7 @@ export default withTooltip<StatsPlotProps, TooltipData>(
<GridColumns
a258wang marked this conversation as resolved
Review

Same as above - does the +30 depend on other hard-coded values and/or user-provided props?

Same as above - does the +30 depend on other hard-coded values and/or user-provided props?
scale={xScale}
width={xMax}
height={yMax + 32}
height={yMax + lineExtension - gridColumnTopOffset}
top={gridColumnTopOffset}
left={valueAxisLeftOffset}
stroke={Color.tertiaryBackground}
@ -153,7 +185,9 @@ export default withTooltip<StatsPlotProps, TooltipData>(
<Line
fill={Color.tertiaryBackground}
to={new Point({ x: valueAxisLeftOffset, y: gridColumnTopOffset })}
from={new Point({ x: valueAxisLeftOffset, y: yMax + 10 })}
from={
new Point({ x: valueAxisLeftOffset, y: yMax + lineExtension })
}
stroke={Color.tertiaryBackground}
strokeWidth={strokeWidth}
strokeDasharray={strokeDashArray}
@ -162,17 +196,22 @@ export default withTooltip<StatsPlotProps, TooltipData>(
fill={Color.tertiaryBackground}
to={
new Point({
x: xMax - 20 - strokeWidth,
x: xMax - margin.left - strokeWidth,
y: gridColumnTopOffset,
})
}
from={new Point({ x: xMax - 20 - strokeWidth, y: yMax + 10 })}
from={
new Point({
x: xMax - margin.left - strokeWidth,
y: yMax + lineExtension,
})
}
stroke={Color.tertiaryBackground}
strokeWidth={strokeWidth}
strokeDasharray={strokeDashArray}
/>
<AxisBottom
top={yMax + 30}
top={yMax + lineExtension - gridColumnTopOffset}
left={categoryAxisLabelLeftOffset}
scale={xScale}
hideAxisLine
@ -183,7 +222,7 @@ export default withTooltip<StatsPlotProps, TooltipData>(
tickLabelProps={() => {
return {
fill: Color.label,
fontWeight: 800,
fontWeight: TICK_LABEL_FONT_SIZE,
};
}}
/>
@ -191,7 +230,7 @@ export default withTooltip<StatsPlotProps, TooltipData>(
scale={yScale}
a258wang marked this conversation as resolved
Review

NIT: either yScale(min(d))! if we can do a non-null assertion, or simplify 0 + 40 to 40

NIT: either `yScale(min(d))!` if we can do a non-null assertion, or simplify `0 + 40` to `40`
Review

oh wait I just realized you probably meant (yScale(min(d)) ?? 0) + 40, I think the brackets might be necessary since ?? has a very low precedence when it comes to order of operations.

but comment about ! still applies, if applicable :))

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence

oh wait I just realized you probably meant `(yScale(min(d)) ?? 0) + 40`, I think the brackets might be necessary since ?? has a very low precedence when it comes to order of operations. but comment about ! still applies, if applicable :)) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
top={valueAxisLabelTopOffset}
left={valueAxisLabelLeftOffset}
numTicks={6}
numTicks={numTicksLeftAxis}
hideAxisLine
labelProps={{
fontSize: `${valueAxisLabelSize / 16}rem`,
@ -199,39 +238,47 @@ export default withTooltip<StatsPlotProps, TooltipData>(
tickLabelProps={() => {
return {
fill: Color.label,
fontWeight: 800,
fontWeight: TICK_LABEL_FONT_SIZE,
};
}}
/>
<Group top={6}>
{d.map((d: Stats, i) => (
<g key={i}>
<BoxPlot
<Group top={plotTopOffset}>
{plotData.map((d: Stats, i) => (
<Group key={i}>
<VisxBoxPlot
className={styles.boxplot}
min={min(d)}
max={max(d)}
min={getMin(d)}
max={getMax(d)}
left={
xScale(x(d))! +
0.3 * constrainedWidth +
xScale(getX(d))! +
boxPlotLeftOffset * constrainedWidth +
valueAxisLeftOffset
}
firstQuartile={firstQuartile(d)}
thirdQuartile={thirdQuartile(d)}
median={median(d)}
boxWidth={constrainedWidth * 0.4}
fill={Color.primaryAccentLight}
fillOpacity={0.8}
firstQuartile={getFirstQuartile(d)}
thirdQuartile={getThirdQuartile(d)}
median={getMedian(d)}
boxWidth={constrainedWidth * boxPlotWidthFactor}
rx={0}
ry={0}
stroke={Color.label}
strokeWidth={strokeWidth}
valueScale={yScale}
minProps={{
onMouseOver: () => {
showTooltip({
tooltipTop: yScale(min(d)) ?? 0 + 40,
tooltipLeft: xScale(x(d))! + constrainedWidth + 5,
tooltipTop:
(yScale(getMin(d)) ?? 0) + toolTipTopOffset,
tooltipLeft:
xScale(getX(d))! +
constrainedWidth +
toolTipLeftOffset,
tooltipData: {
min: min(d),
name: x(d),
min: getMin(d),
max: getMax(d),
median: getMedian(d),
firstQuartile: getFirstQuartile(d),
thirdQuartile: getThirdQuartile(d),
category: getX(d),
},
});
},
@ -242,11 +289,19 @@ export default withTooltip<StatsPlotProps, TooltipData>(
maxProps={{
onMouseOver: () => {
showTooltip({
tooltipTop: yScale(max(d)) ?? 0 + 40,
tooltipLeft: xScale(x(d))! + constrainedWidth + 5,
tooltipTop:
(yScale(getMax(d)) ?? 0) + toolTipTopOffset,
tooltipLeft:
xScale(getX(d))! +
constrainedWidth +
toolTipLeftOffset,
tooltipData: {
max: max(d),
name: x(d),
min: getMin(d),
max: getMax(d),
median: getMedian(d),
firstQuartile: getFirstQuartile(d),
thirdQuartile: getThirdQuartile(d),
category: getX(d),
},
a258wang marked this conversation as resolved
Review

NIT: does calc(10rem / 16) work here?

NIT: does `calc(10rem / 16)` work here?
});
},
@ -257,11 +312,15 @@ export default withTooltip<StatsPlotProps, TooltipData>(
boxProps={{
onMouseOver: () => {
a258wang marked this conversation as resolved
Review

NIT: calc(5rem / 16) if it works

Also just a note that we should generally prefer styling via CSS whenever possible, instead of inline styles like this. 🙂

NIT: `calc(5rem / 16)` if it works Also just a note that we should generally prefer styling via CSS whenever possible, instead of inline styles like this. 🙂
showTooltip({
tooltipTop: yScale(median(d)) ?? 0 + 20,
tooltipLeft: xScale(x(d))! + constrainedWidth + 5,
tooltipTop:
(yScale(getMedian(d)) ?? 0) + toolTipTopOffset,
tooltipLeft:
xScale(getX(d))! +
constrainedWidth +
toolTipLeftOffset,
tooltipData: {
...d.boxPlot,
name: x(d),
category: getX(d),
},
});
},
@ -271,15 +330,23 @@ export default withTooltip<StatsPlotProps, TooltipData>(
}}
medianProps={{
style: {
stroke: "white",
stroke: Color.label,
},
onMouseOver: () => {
showTooltip({
tooltipTop: yScale(median(d)) ?? 0 + 40,
tooltipLeft: xScale(x(d))! + constrainedWidth + 5,
tooltipTop:
(yScale(getMedian(d)) ?? 0) + toolTipTopOffset,
tooltipLeft:
xScale(getX(d))! +
constrainedWidth +
toolTipLeftOffset,
tooltipData: {
median: median(d),
name: x(d),
min: getMin(d),
max: getMax(d),
median: getMedian(d),
firstQuartile: getFirstQuartile(d),
thirdQuartile: getThirdQuartile(d),
category: getX(d),
},
});
},
@ -288,7 +355,7 @@ export default withTooltip<StatsPlotProps, TooltipData>(
},
}}
/>
</g>
</Group>
))}
</Group>
</Group>
@ -298,31 +365,20 @@ export default withTooltip<StatsPlotProps, TooltipData>(
<Tooltip
top={tooltipTop}
left={tooltipLeft}
style={{
...defaultTooltipStyles,
backgroundColor: Color.secondaryBackground,
color: Color.primaryText,
padding: "10px",
}}
className={styles.tooltip}
style={{ ...defaultTooltipStyles }}
>
<div>
<strong>{tooltipData.name}</strong>
</div>
<div
style={{
marginTop: "5px",
fontSize: `${toolTipFontSize / 16}rem`,
}}
>
{tooltipData.max && <div>max: {tooltipData.max}</div>}
{tooltipData.thirdQuartile && (
<div>third quartile: {tooltipData.thirdQuartile}</div>
)}
{tooltipData.median && <div>median: {tooltipData.median}</div>}
{tooltipData.firstQuartile && (
<div>first quartile: {tooltipData.firstQuartile}</div>
)}
{tooltipData.min && <div>min: {tooltipData.min}</div>}
<p className={styles.category}>{tooltipData.category}</p>
<div className={styles.toolTipData}>
{tooltipData.max ? <p>max: {tooltipData.max}</p> : null}
{tooltipData.thirdQuartile ? (
<p>third quartile: {tooltipData.thirdQuartile}</p>
) : null}
{tooltipData.median ? <p>median: {tooltipData.median}</p> : null}
{tooltipData.firstQuartile ? (
<p>first quartile: {tooltipData.firstQuartile}</p>
) : null}
{tooltipData.min ? <p>min: {tooltipData.min}</p> : null}
</div>
</Tooltip>
)}

View File

@ -67,7 +67,7 @@ export const moreMockCategoricalData = [
export const mockBoxPlotData = [
{
x: "1A",
category: "1A",
min: 20,
firstQuartile: 25,
median: 30,
@ -76,7 +76,7 @@ export const mockBoxPlotData = [
outliers: [],
},
{
x: "1B",
category: "1B",
min: 0,
firstQuartile: 20,
median: 30,
@ -85,7 +85,7 @@ export const mockBoxPlotData = [
outliers: [],
},
{
x: "2A",
category: "2A",
min: 25,
firstQuartile: 35,
median: 50,

View File

@ -1,9 +1,9 @@
import { BarGraphHorizontal, BarGraphVertical } from "components/BarGraph";
import Boxplot from "components/Boxplot";
import { BoxPlot } from "components/Boxplot";
import {
mockCategoricalData,
moreMockCategoricalData,
mockBoxPlotData
mockBoxPlotData,
} from "data/mocks";
import React from "react";
@ -14,7 +14,7 @@ import styles from "./playground.module.css";
export default function Home() {
return (
<div className={styles.page} suppressHydrationWarning>
<div className={styles.page}>
<h1>Playground</h1>
<p>Show off your components here!</p>
<ColorPalette />
@ -66,9 +66,9 @@ export default function Home() {
/>
<h2>
<code>{"<Boxplot />"}</code>
<code>{"<BoxPlot />"}</code>
</h2>
<Boxplot
<BoxPlot
width={600}
height={400}
data={mockBoxPlotData}
@ -76,8 +76,6 @@ export default function Home() {
top: 20,
left: 20,
}}
strokeWidth={2.5}
strokeDashArray="10,4"
/>
</div>
);