Skip to content

Commit fd4e8c9

Browse files
authored
Boxplot width always uses bandwidth-expression instead of band (#291)
* also use expression-based width for user-provided width * match bars as well * Don't mess with binned variant
1 parent ad99073 commit fd4e8c9

1 file changed

Lines changed: 24 additions & 27 deletions

File tree

src/writer/vegalite/layer.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -270,22 +270,25 @@ impl GeomRenderer for BarRenderer {
270270
layer: &Layer,
271271
_context: &RenderContext,
272272
) -> Result<()> {
273-
let width = match layer.parameters.get("width") {
274-
Some(ParameterValue::Number(w)) => *w,
275-
_ => 0.9,
273+
let width = match layer.adjusted_width {
274+
// The adjusted width comes from position adjustments
275+
Some(adjusted) => adjusted,
276+
_ => match layer.parameters.get("width") {
277+
// Fallback to width parameter value if there is no adjustment
278+
Some(ParameterValue::Number(n)) => *n,
279+
_ => 0.9,
280+
},
276281
};
277282

278283
// For horizontal bars, use "height" for band size; for vertical, use "width"
279284
let is_horizontal = is_transposed(layer);
285+
let axis = if is_horizontal { "y" } else { "x" };
280286

281-
// For dodged bars, use expression-based size with the adjusted width
282-
// For non-dodged bars, use band-relative size
283-
let size_value = if let Some(adjusted) = layer.adjusted_width {
284-
// Use bandwidth expression for dodged bars
285-
let axis = if is_horizontal { "y" } else { "x" };
286-
json!({"expr": format!("bandwidth('{}') * {}", axis, adjusted)})
287-
} else {
288-
json!({"band": width})
287+
let size_value = match layer_spec["encoding"][axis]["bin"].as_str() {
288+
// I don't think binned scales obey 'band', but they don't tolerate the 'expr' option.
289+
Some("binned") => json!({"band": width}),
290+
// Use expression-based size with the adjusted width
291+
_ => json!({"expr": format!("bandwidth('{}') * {}", axis, width)}),
289292
};
290293

291294
layer_spec["mark"] = if is_horizontal {
@@ -1741,25 +1744,19 @@ impl BoxplotRenderer {
17411744

17421745
let value_var1 = if is_horizontal { "x" } else { "y" };
17431746
let value_var2 = if is_horizontal { "x2" } else { "y2" };
1747+
let axis = if is_horizontal { "y" } else { "x" };
17441748

17451749
// Get width parameter
1746-
let base_width = layer
1747-
.parameters
1748-
.get("width")
1749-
.and_then(|v| match v {
1750-
ParameterValue::Number(n) => Some(*n),
1751-
_ => None,
1752-
})
1753-
.unwrap_or(0.9);
1754-
1755-
// For dodged boxplots, use expression-based width with adjusted_width
1756-
// For non-dodged boxplots, use band-relative width
1757-
let axis = if is_horizontal { "y" } else { "x" };
1758-
let width_value = if let Some(adjusted) = layer.adjusted_width {
1759-
json!({"expr": format!("bandwidth('{}') * {}", axis, adjusted)})
1760-
} else {
1761-
json!({"band": base_width})
1750+
let width = match layer.adjusted_width {
1751+
// The adjusted width comes from position adjustments
1752+
Some(adjusted) => adjusted,
1753+
_ => match layer.parameters.get("width") {
1754+
// Fallback to width parameter value if there is no adjustment
1755+
Some(ParameterValue::Number(n)) => *n,
1756+
_ => 0.9,
1757+
},
17621758
};
1759+
let width_value = json!({"expr": format!("bandwidth('{}') * {}", axis, width)});
17631760

17641761
// Helper to create filter transform for source selection
17651762
let make_source_filter = |type_suffix: &str| -> Value {

0 commit comments

Comments
 (0)