}
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,
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>,
Self {
name: Box::new(name),
children: Vec::new(),
- show_label: true,
+ show_label: None,
label_depth: 0,
extra_depth: 0,
label_position: None,
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);
}
}
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) {
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,
.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,
})
}
}
}
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;
}
#[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));
}