Skip to content

Commit 21b26a5

Browse files
authored
violin: treat side = 'both' case correctly (#288)
1 parent d48dd1e commit 21b26a5

1 file changed

Lines changed: 33 additions & 13 deletions

File tree

src/writer/vegalite/layer.rs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,19 +1312,21 @@ impl GeomRenderer for ViolinRenderer {
13121312
let is_horizontal = is_transposed(layer);
13131313

13141314
// It'll be implemented as an offset.
1315-
let mut violin_offset = format!("[datum.{offset}, -datum.{offset}]", offset = offset_col);
1316-
if let Some(ParameterValue::String(side)) = layer.parameters.get("side") {
1317-
let positive = if is_horizontal {
1318-
matches!(side.as_str(), "bottom" | "left")
1319-
} else {
1320-
matches!(side.as_str(), "top" | "right")
1321-
};
1322-
violin_offset = if positive {
1323-
format!("[datum.{offset}]", offset = offset_col)
1324-
} else {
1325-
format!("[-datum.{offset}]", offset = offset_col)
1326-
};
1327-
}
1315+
let violin_offset = match layer.parameters.get("side") {
1316+
Some(ParameterValue::String(side)) if side != "both" => {
1317+
let positive = if is_horizontal {
1318+
matches!(side.as_str(), "bottom" | "left")
1319+
} else {
1320+
matches!(side.as_str(), "top" | "right")
1321+
};
1322+
if positive {
1323+
format!("[datum.{offset}]", offset = offset_col)
1324+
} else {
1325+
format!("[-datum.{offset}]", offset = offset_col)
1326+
}
1327+
}
1328+
_ => format!("[datum.{offset}, -datum.{offset}]", offset = offset_col),
1329+
};
13281330

13291331
// Continuous axis column for order calculation:
13301332
// - Vertical: pos2 (y-axis has continuous density values)
@@ -3289,6 +3291,24 @@ mod tests {
32893291
expr
32903292
);
32913293

3294+
// Explicit "both" - mirrors on both sides (vertical orientation)
3295+
let expr = get_violin_offset_expr(Some("both"), false);
3296+
assert!(
3297+
expr.contains(&format!("[datum.{}, -datum.{}]", offset_col, offset_col))
3298+
|| expr.contains(&format!("[-datum.{}, datum.{}]", offset_col, offset_col)),
3299+
"Explicit 'both' should mirror both sides (vertical): {}",
3300+
expr
3301+
);
3302+
3303+
// Explicit "both" - mirrors on both sides (horizontal orientation)
3304+
let expr = get_violin_offset_expr(Some("both"), true);
3305+
assert!(
3306+
expr.contains(&format!("[datum.{}, -datum.{}]", offset_col, offset_col))
3307+
|| expr.contains(&format!("[-datum.{}, datum.{}]", offset_col, offset_col)),
3308+
"Explicit 'both' should mirror both sides (horizontal): {}",
3309+
expr
3310+
);
3311+
32923312
// Vertical orientation (default): x=nominal, y=quantitative
32933313
// "left" and "bottom" - only negative offset
32943314
assert_eq!(

0 commit comments

Comments
 (0)