Skip to content

Commit 330aadb

Browse files
space out barplots correctly
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 69104f1 commit 330aadb

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

client/src/components/Barplot.jsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const MARGIN = { top: 16, right: 48, bottom: 8, left: 160 };
55
const MIN_WIDTH = 220;
66
const MIN_HEIGHT = 240;
77
const MAX_BANDWIDTH = 20;
8-
const BAND_PADDING = 0.1;
8+
const BAND_PADDING = 0.2;
99

1010
/**
1111
* React wrapper around the original D3 barplot from client/js/main.js (barplot_new).
@@ -65,23 +65,29 @@ export default function Barplot({ data, onTickClick, disabled }) {
6565
.attr("width", outerW)
6666
.attr("height", outerH);
6767

68-
const idealRange = (MAX_BANDWIDTH * plotData.length) / (1 - BAND_PADDING);
69-
const rangeHeight = Math.min(height, idealRange);
70-
const yOffset = Math.max(0, (height - rangeHeight) / 2);
68+
// Fill the full height. If bandwidth would exceed MAX_BANDWIDTH, increase
69+
// padding instead of shrinking the range — bars stay capped, gaps absorb
70+
// the extra space.
71+
const n = plotData.length;
72+
const naturalBandwidth = (height / (n + BAND_PADDING)) * (1 - BAND_PADDING);
73+
const bandPadding =
74+
naturalBandwidth > MAX_BANDWIDTH
75+
? (height - MAX_BANDWIDTH * n) / (height + MAX_BANDWIDTH)
76+
: BAND_PADDING;
7177

7278
const svg = d3
7379
.select(svgEl)
7480
.append("g")
75-
.attr("transform", `translate(${MARGIN.left},${MARGIN.top + yOffset})`)
81+
.attr("transform", `translate(${MARGIN.left},${MARGIN.top})`)
7682
.attr("fill", "white");
7783

7884
const xMax = d3.max(plotData, (d) => d.prob) || 1;
7985
const xScale = d3.scaleLinear().domain([0, xMax]).range([0, width]);
8086
const yScale = d3
8187
.scaleBand()
8288
.domain(plotData.map((d) => d.text))
83-
.range([0, rangeHeight])
84-
.padding(BAND_PADDING);
89+
.range([0, height])
90+
.padding(bandPadding);
8591

8692
const yAxis = svg.append("g").call(d3.axisLeft(yScale));
8793
const tickFontSize = Math.min(15, Math.max(5, yScale.bandwidth() * 0.9));

0 commit comments

Comments
 (0)