fn axis_extent(&self, axis: Axis3) -> usize {
self.axis_dimensions(axis).map(|d| d.len()).product()
}
-
- fn axis_label_depth(&self, axis: Axis3) -> usize {
- self.axis_dimensions(axis).map(|d| d.label_depth()).sum()
- }
}
/// Dimensions.
pub fn len(&self) -> usize {
self.data_leaves.len()
}
-
- pub fn label_depth(&self) -> usize {
- if self.hide_all_labels {
- 0
- } else {
- self.root.label_depth
- }
- }
}
#[derive(Clone, Debug)]
pub struct Group {
parent: Option<Weak<Group>>,
name: Box<Value>,
- label_depth: usize,
- extra_depth: usize,
/// The child categories.
///
/// only one or even (pathologically) none.
children: Vec<Category>,
- /// Position for the group's own label, if any.
- pub show_label: Option<LabelPosition>,
+ /// Whether to show the group's label.
+ pub show_label: bool,
}
impl Group {
self.hide_all_labels = true;
self
}
- fn build(mut self, label_position: LabelPosition) -> Dimension {
+ fn build(mut self) -> Dimension {
let mut leaves = Vec::with_capacity(self.len);
- self.root.assign_label_depth(label_position, true);
- let root = self.root.build(label_position, None, &mut leaves);
+ let root = self.root.build(None, &mut leaves);
Dimension {
root,
presentation_order: (0..leaves.len()).collect(),
name: Box<Value>,
children: Vec<CategoryBuilder>,
show_label: Option<bool>,
- label_depth: usize,
- extra_depth: usize,
- label_position: Option<LabelPosition>,
}
impl GroupBuilder {
name: Box::new(name),
children: Vec::new(),
show_label: None,
- label_depth: 0,
- extra_depth: 0,
- label_position: None,
}
}
pub fn push<T>(&mut self, value: T)
fn len(&self) -> usize {
self.children.iter().map(|category| category.len()).sum()
}
- 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);
- }
- let depth = self
- .children
- .iter()
- .map(|c| c.label_depth())
- .max()
- .unwrap_or_default();
- for child in self.children.iter_mut() {
- let extra_depth = depth - child.label_depth();
- if extra_depth > 0 {
- child.distribute_extra_depth(extra_depth);
- }
- child.set_label_depth(depth);
- }
- // 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) {
- if self.children.is_empty() {
- self.extra_depth += extra_depth;
- } else {
- for child in self.children.iter_mut() {
- child.distribute_extra_depth(extra_depth);
- }
- }
- }
- fn build(
- self,
- label_position: LabelPosition,
- parent: Option<Weak<Group>>,
- leaves: &mut Vec<Arc<Leaf>>,
- ) -> Arc<Group> {
+ fn build(self, parent: Option<Weak<Group>>, leaves: &mut Vec<Arc<Leaf>>) -> Arc<Group> {
Arc::new_cyclic(|weak| Group {
name: self.name,
- label_depth: self.label_depth,
- extra_depth: self.extra_depth,
children: self
.children
.into_iter()
- .map(|c| c.build(label_position, weak.clone(), leaves))
+ .map(|c| c.build(weak.clone(), leaves))
.collect(),
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)
+ self.show_label.unwrap_or(parent.is_some())
},
parent,
})
Leaf {
name: Box<Value>,
class: Option<Class>,
- extra_depth: usize,
},
}
CategoryBuilder::Leaf { .. } => 1,
}
}
- fn build(
- self,
- label_position: LabelPosition,
- parent: Weak<Group>,
- leaves: &mut Vec<Arc<Leaf>>,
- ) -> Category {
+ fn build(self, parent: Weak<Group>, leaves: &mut Vec<Arc<Leaf>>) -> Category {
match self {
- Self::Group(group) => {
- Category::Group(group.build(label_position, Some(parent), leaves))
- }
- Self::Leaf {
- name,
- class,
- extra_depth,
- } => {
+ Self::Group(group) => Category::Group(group.build(Some(parent), leaves)),
+ Self::Leaf { name, class } => {
let leaf = Arc::new(Leaf {
parent,
name,
- extra_depth,
class,
});
leaves.push(leaf.clone());
}
}
}
- fn assign_label_depth(&mut self, label_position: LabelPosition) {
- match self {
- CategoryBuilder::Group(group) => group.assign_label_depth(label_position, false),
- CategoryBuilder::Leaf { .. } => (),
- }
- }
- fn distribute_extra_depth(&mut self, extra: usize) {
- match self {
- CategoryBuilder::Group(group) => group.distribute_extra_depth(extra),
- CategoryBuilder::Leaf { extra_depth, .. } => *extra_depth += extra,
- }
- }
- fn label_depth(&self) -> usize {
- match self {
- CategoryBuilder::Group(group) => group.label_depth,
- CategoryBuilder::Leaf { .. } => 1,
- }
- }
- fn set_label_depth(&mut self, depth: usize) {
- match self {
- CategoryBuilder::Group(group) => group.label_depth = depth,
- CategoryBuilder::Leaf { .. } => (),
- }
- }
}
impl From<GroupBuilder> for CategoryBuilder {
Self::Leaf {
name: Box::new(name),
class: None,
- extra_depth: 0,
}
}
}
Self::Leaf {
name: Box::new(name),
class: Some(class),
- extra_depth: 0,
}
}
}
} else {
LabelPosition::Nested
};
- let d = d.build(label_position);
+ let d = d.build();
axes[axis].dimensions.push(dimensions.len());
dimensions.push(d);
}
pub struct Leaf {
parent: Weak<Group>,
name: Box<Value>,
- extra_depth: usize,
/// Default format for values in this category.
class: Option<Class>,
Self {
parent: Weak::new(),
name: Box::new(name),
- extra_depth: 0,
class: None,
}
}
fn show_label(&self) -> bool {
match self {
- Category::Group(group) => group.show_label.is_some(),
+ Category::Group(group) => group.show_label,
Category::Leaf(_) => true,
}
}
trait CategoryTrait {
fn name(&self) -> &Value;
- fn label_depth(&self) -> usize;
- fn extra_depth(&self) -> usize;
fn parent(&self) -> Option<Arc<Group>>;
}
&self.name
}
- fn label_depth(&self) -> usize {
- self.label_depth
- }
-
- fn extra_depth(&self) -> usize {
- self.extra_depth
- }
-
fn parent(&self) -> Option<Arc<Group>> {
self.parent.as_ref().and_then(|parent| parent.upgrade())
}
&self.name
}
- fn label_depth(&self) -> usize {
- 1
- }
-
- fn extra_depth(&self) -> usize {
- self.extra_depth
- }
-
fn parent(&self) -> Option<Arc<Group>> {
Some(self.parent.upgrade().unwrap())
}
}
}
- fn label_depth(&self) -> usize {
- match self {
- Category::Group(group) => group.label_depth(),
- Category::Leaf(leaf) => leaf.label_depth(),
- }
- }
-
- fn extra_depth(&self) -> usize {
- match self {
- Category::Group(group) => group.extra_depth(),
- Category::Leaf(leaf) => leaf.extra_depth(),
- }
- }
-
fn parent(&self) -> Option<Arc<Group>> {
match self {
Category::Group(group) => group.parent(),