-use std::{ops::Range, sync::Arc};
+use std::{iter::zip, ops::Range, sync::Arc};
use enum_map::{enum_map, EnumMap};
use itertools::Itertools;
}
pub fn output_layers(&self, layer_indexes: &[usize]) -> Option<Table> {
- self.create_aux_table_if_nonempty(
- Area::Layers,
- self.nonempty_layer_dimensions()
- .collect::<Vec<_>>()
- .into_iter()
- .enumerate()
- .map(|(i, dimension)| dimension.nth_leaf(layer_indexes[i]).unwrap().name.clone()),
- )
+ let mut layers = Vec::new();
+ for (dimension, &layer_index) in zip(
+ self.axes[Axis3::Z]
+ .dimensions
+ .iter()
+ .map(|index| &self.dimensions[*index]),
+ layer_indexes,
+ ) {
+ if !dimension.is_empty() {
+ layers.push(dimension.nth_leaf(layer_index).unwrap().name.clone());
+ }
+ }
+ layers.reverse();
+
+ self.create_aux_table_if_nonempty(Area::Layers, layers.into_iter())
}
pub fn output_caption(&self) -> Option<Table> {
);
}
+#[test]
+fn d3() {
+ let a = (
+ Axis3::Z,
+ Dimension::new(Group::new("a").with("a1").with("a2").with("a3")),
+ );
+ let b = (
+ Axis3::Z,
+ Dimension::new(Group::new("b").with("b1").with("b2").with("b3").with("b4")),
+ );
+ let c = (
+ Axis3::X,
+ Dimension::new(
+ Group::new("c")
+ .with("c1")
+ .with("c2")
+ .with("c3")
+ .with("c4")
+ .with("c5"),
+ ),
+ );
+ let mut pt =
+ PivotTable::new("Column x b1 x a1", vec![a, b, c]).with_look(Arc::new(test_look()));
+ let mut i = 0;
+ for c in 0..5 {
+ for b in 0..4 {
+ for a in 0..3 {
+ pt.insert(&[a, b, c], Value::new_integer(Some(i as f64)));
+ i += 1;
+ }
+ }
+ }
+ assert_rendering(
+ &pt,
+ "\
+Column x b1 x a1
+b1
+a1
+╭──┬──┬──┬──┬──╮
+│c1│c2│c3│c4│c5│
+├──┼──┼──┼──┼──┤
+│ 0│12│24│36│48│
+╰──┴──┴──┴──┴──╯
+",
+ );
+
+ let pt = pt.with_layer(&[0, 1]).with_title("Column x b2 x a1");
+ assert_rendering(
+ &pt,
+ "\
+Column x b2 x a1
+b2
+a1
+╭──┬──┬──┬──┬──╮
+│c1│c2│c3│c4│c5│
+├──┼──┼──┼──┼──┤
+│ 3│15│27│39│51│
+╰──┴──┴──┴──┴──╯
+",
+ );
+
+ let pt = pt.with_layer(&[1, 2]).with_title("Column x b3 x a2");
+ assert_rendering(
+ &pt,
+ "\
+Column x b3 x a2
+b3
+a2
+╭──┬──┬──┬──┬──╮
+│c1│c2│c3│c4│c5│
+├──┼──┼──┼──┼──┤
+│ 7│19│31│43│55│
+╰──┴──┴──┴──┴──╯
+",
+ );
+}
+
#[test]
fn title_and_caption() {
let pivot_table =