Dimension labels not shown by default
authorBen Pfaff <blp@cs.stanford.edu>
Sun, 6 Apr 2025 16:57:02 +0000 (09:57 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 6 Apr 2025 16:57:02 +0000 (09:57 -0700)
rust/pspp/src/output/pivot/mod.rs
rust/pspp/src/output/pivot/test.rs

index b8d0b8d0740b12ef7f8c7bc9b0c5a215eb9808d3..ff4f1f782695f11da690dad70b339659b995d4d4 100644 (file)
@@ -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<Value>,
     children: Vec<CategoryBuilder>,
-    show_label: bool,
+    show_label: Option<bool>,
     label_depth: usize,
     extra_depth: usize,
     label_position: Option<LabelPosition>,
@@ -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<Leaf>>,
     ) -> Arc<Group> {
         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;
             }
index 0ef37dfb2494720029325020136cfdda5a993fb6..e6079ed0152a8d9a59aa71840b3d16e35e60b26a 100644 (file)
@@ -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));
     }