From e7d6d682f3abb90176a08e49c71f08a4cd997bd9 Mon Sep 17 00:00:00 2001 From: e26chiu Date: Sun, 12 Feb 2023 14:56:48 -0500 Subject: [PATCH] Fix Boxplot labels, spacing + remove hardcoded values --- components/Boxplot.tsx | 247 ++++++++++++++++++++--------------------- pages/coop.tsx | 2 +- 2 files changed, 119 insertions(+), 130 deletions(-) diff --git a/components/Boxplot.tsx b/components/Boxplot.tsx index 87a2fc4..051d12d 100644 --- a/components/Boxplot.tsx +++ b/components/Boxplot.tsx @@ -47,16 +47,10 @@ export type StatsPlotProps = { 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; - /** Distance between the left and the start of the first label of the x axis, in px. */ - categoryAxisLabelLeftOffset?: number; + /** Left margin before the start of the graph for the left axis labels, in px. */ + valueAxisLeftMargin?: number; + /** Distance between the left axis labels and the start of the graph */ + valueAxisLabelOffset?: number; /** Distance between the top and the column lines of the grid of the graph, in px. */ gridColumnTopOffset?: number; /** Distance between the top of the point in the boxplot and the start of the tooltip box, in px. */ @@ -73,6 +67,8 @@ export type StatsPlotProps = { boxPlotWidthFactor?: number; /** Factor multiplied with the compressed width to determine the distance between boxes, in px. */ boxPlotLeftOffset?: number; + /** Boxplot padding */ + boxPlotPadding?: number; }; export const BoxPlot = withTooltip( @@ -90,18 +86,16 @@ export const BoxPlot = withTooltip( strokeWidth = 2.5, strokeDashArray = "10,4", numTicksLeftAxis = 6, - plotTopOffset = 10, - valueAxisLeftOffset = 40, + valueAxisLeftMargin = 40, gridColumnTopOffset = -20, - valueAxisLabelTopOffset = 5, - valueAxisLabelLeftOffset = 10, - categoryAxisLabelLeftOffset = 30, + valueAxisLabelOffset = -10, toolTipTopOffset = 0, toolTipLeftOffset = 0, categoryAxisLabelSize = DEFAULT_LABEL_SIZE, valueAxisLabelSize = DEFAULT_LABEL_SIZE, boxPlotWidthFactor = 0.4, boxPlotLeftOffset = 0.3, + boxPlotPadding = 0.3, }: StatsPlotProps & WithTooltipProvidedProps) => { // bounds const xMax = width; @@ -128,23 +122,22 @@ export const BoxPlot = withTooltip( // scales const xScale = scaleBand({ - range: [18, xMax - 80], // scaling is needed due to the left offset + range: [margin.left, xMax - 2 * margin.left - valueAxisLeftMargin], // scaling is needed due to left margin round: true, domain: plotData.map(getX), - padding: 0.3, + padding: boxPlotPadding, }); const values = plotData.reduce((allValues, { boxPlot }) => { allValues.push(boxPlot.min, boxPlot.max); return allValues; }, [] as number[]); - const minYValue = Math.min(...values); const maxYValue = Math.max(...values); const yScale = scaleLinear({ - range: [yMax, 0], + range: [yMax - margin.top, 0], round: true, - domain: [minYValue, maxYValue], + domain: [0, maxYValue], }); const constrainedWidth = Math.min(200, xScale.bandwidth()); @@ -168,35 +161,65 @@ export const BoxPlot = withTooltip(
- - - + + + + { + return { + fill: Color.label, + fontWeight: TICK_LABEL_FONT_WEIGHT, + textAnchor: "middle", + }; + }} + /> + { + return { + fill: Color.label, + fontWeight: TICK_LABEL_FONT_WEIGHT, + textAnchor: "end", + verticalAnchor: "middle", + }; + }} + /> + + ( from={ new Point({ x: xMax - margin.left - strokeWidth, - y: yMax + plotTopOffset, + y: yMax, }) } stroke={Color.tertiaryBackground} strokeWidth={strokeWidth} strokeDasharray={strokeDashArray} /> - { - return { - fill: Color.label, - fontWeight: TICK_LABEL_FONT_WEIGHT, - }; - }} - /> - { - return { - fill: Color.label, - fontWeight: TICK_LABEL_FONT_WEIGHT, - }; - }} - /> - - {plotData.map((d: Stats, i) => ( - - { - hideTooltip(); - }, - }} - /> - - ))} - + {plotData.map((d: Stats, i) => ( + + { + hideTooltip(); + }, + }} + /> + + ))} diff --git a/pages/coop.tsx b/pages/coop.tsx index 3903998..7c83ec4 100644 --- a/pages/coop.tsx +++ b/pages/coop.tsx @@ -212,7 +212,7 @@ export default function CoopPage() { width={isMobile ? pageWidth / 1.5 : 600} height={DefaultProp.graphHeight} data={C7iv} - valueAxisLeftOffset={75} + valueAxisLeftMargin={75} margin={{ top: 20, left: 20,