From: Ben Pfaff Date: Sun, 6 Apr 2025 16:57:02 +0000 (-0700) Subject: Dimension labels not shown by default X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=239df515ac9c7656ba12a6b7b47b7e7cc89f477e;p=pspp Dimension labels not shown by default --- diff --git a/rust/pspp/src/output/pivot/mod.rs b/rust/pspp/src/output/pivot/mod.rs index b8d0b8d074..ff4f1f7826 100644 --- a/rust/pspp/src/output/pivot/mod.rs +++ b/rust/pspp/src/output/pivot/mod.rs @@ -421,7 +421,7 @@ impl DimensionBuilder { } fn build(mut self, level: usize, top_index: usize, label_position: LabelPosition) -> Dimension { let mut leaves = Vec::with_capacity(self.len); - self.root.assign_label_depth(label_position); + self.root.assign_label_depth(label_position, true); let root = self.root.build(label_position, None, &mut leaves); Dimension { level, @@ -438,7 +438,7 @@ impl DimensionBuilder { pub struct GroupBuilder { name: Box, children: Vec, - show_label: bool, + show_label: Option, label_depth: usize, extra_depth: usize, label_position: Option, @@ -449,7 +449,7 @@ impl GroupBuilder { Self { name: Box::new(name), children: Vec::new(), - show_label: true, + show_label: None, label_depth: 0, extra_depth: 0, label_position: None, @@ -469,13 +469,17 @@ impl GroupBuilder { self } pub fn with_label_hidden(mut self) -> Self { - self.show_label = false; + self.show_label = Some(false); + self + } + pub fn with_label_shown(mut self) -> Self { + self.show_label = Some(true); self } fn len(&self) -> usize { self.children.iter().map(|category| category.len()).sum() } - fn assign_label_depth(&mut self, label_position: LabelPosition) { + fn assign_label_depth(&mut self, label_position: LabelPosition, is_root: bool) { for child in self.children.iter_mut() { child.assign_label_depth(label_position); } @@ -492,7 +496,9 @@ impl GroupBuilder { } child.set_label_depth(depth); } - self.label_position = self.show_label.then_some(label_position); + // By default, nested group labels are shown, but not dimension root labels. + let show_label = self.show_label.unwrap_or(!is_root); + self.label_position = show_label.then_some(label_position); self.label_depth = depth + (self.label_position == Some(LabelPosition::Nested)) as usize; } fn distribute_extra_depth(&mut self, extra_depth: usize) { @@ -511,7 +517,6 @@ impl GroupBuilder { leaves: &mut Vec>, ) -> Arc { Arc::new_cyclic(|weak| Group { - parent, name: self.name, label_depth: self.label_depth, extra_depth: self.extra_depth, @@ -521,7 +526,12 @@ impl GroupBuilder { .enumerate() .map(|(group_index, c)| c.build(label_position, weak.clone(), group_index, leaves)) .collect(), - show_label: self.show_label.then_some(label_position), + show_label: { + // By default, nested group labels are shown, but not dimension root labels. + let show_label = self.show_label.unwrap_or(parent.is_some()); + show_label.then_some(label_position) + }, + parent, }) } } @@ -578,7 +588,7 @@ impl CategoryBuilder { } fn assign_label_depth(&mut self, label_position: LabelPosition) { match self { - CategoryBuilder::Group(group) => group.assign_label_depth(label_position), + CategoryBuilder::Group(group) => group.assign_label_depth(label_position, false), CategoryBuilder::Leaf { label_depth, .. } => { *label_depth = 1; } diff --git a/rust/pspp/src/output/pivot/test.rs b/rust/pspp/src/output/pivot/test.rs index 0ef37dfb24..e6079ed015 100644 --- a/rust/pspp/src/output/pivot/test.rs +++ b/rust/pspp/src/output/pivot/test.rs @@ -42,13 +42,13 @@ fn pivot_table_1d() { #[test] fn pivot_table_2d() { - let mut a = GroupBuilder::new(Value::new_text("a")); + let mut a = GroupBuilder::new(Value::new_text("a")).with_label_shown(); for name in ["a1", "a2", "a3"] { a.push(Value::new_text(name)); } let d1 = DimensionBuilder::new(Axis3::X, a); - let mut b = GroupBuilder::new(Value::new_text("b")); + let mut b = GroupBuilder::new(Value::new_text("b")).with_label_shown(); for name in ["b1", "b2", "b3"] { b.push(Value::new_text(name)); }