let row_label_position = if h == Axis2::Y
&& pt.look.row_label_position == LabelPosition::Corner
- && !headings.is_empty()
- && headings[0].move_dimension_labels_to_corner()
+ && headings
+ .iter_mut()
+ .map(|heading| heading.move_dimension_labels_to_corner())
+ .filter(|x| *x)
+ .count()
+ > 0
{
LabelPosition::Corner
} else {
vrules[0] = true;
vrules[n_columns] = true;
- // The first heading takes the axis's row label position, the remainder
- // are all nested.
- let dimension_label_positions = std::iter::once(self.row_label_position)
- .chain(std::iter::repeat(LabelPosition::Nested));
-
let mut v_ofs = 0;
- for ((index, heading), dimension_label_position) in self
- .headings
- .iter()
- .enumerate()
- .zip(dimension_label_positions)
- {
+ for (index, heading) in self.headings.iter().enumerate() {
let inner = index == self.headings.len() - 1;
heading.render(
table,
rotate_inner_labels,
rotate_outer_labels,
inner,
- dimension_label_position,
+ self.row_label_position,
);
v_ofs += heading.height;
if !inner {
Arc::new(look)
}
-fn d2(title: &str, axes: [Axis3; 2]) -> PivotTable {
- let mut a = GroupBuilder::new(Value::new_text("a"));
+fn d2(title: &str, axes: [Axis3; 2], dimension_labels: bool) -> PivotTable {
+ let mut a = GroupBuilder::new(Value::new_text("a")).with_show_label(dimension_labels);
for name in ["a1", "a2", "a3"] {
a.push(Value::new_text(name));
}
let d1 = DimensionBuilder::new(axes[0], a);
- let mut b = GroupBuilder::new(Value::new_text("b"));
+ let mut b = GroupBuilder::new(Value::new_text("b")).with_show_label(dimension_labels);
for name in ["b1", "b2", "b3"] {
b.push(Value::new_text(name));
}
pt.with_look(test_look()).build()
}
+#[track_caller]
+fn assert_rendering(pivot_table: PivotTable, expected: &str) {
+ let actual = pivot_table.to_string();
+ if actual != expected {
+ panic!("Unexpected pivot table rendering.\nActual:\n{actual}\nExpected:\n{expected}");
+ }
+}
+
#[test]
fn d2_cc() {
- assert_eq!(
- d2("Columns", [Axis3::X, Axis3::X]).to_string(),
+ assert_rendering(
+ d2("Columns", [Axis3::X, Axis3::X], false),
"\
Columns
╭────────┬────────┬────────╮
├──┼──┼──┼──┼──┼──┼──┼──┼──┤
│ 0│ 1│ 2│ 3│ 4│ 5│ 6│ 7│ 8│
╰──┴──┴──┴──┴──┴──┴──┴──┴──╯
-"
+",
+ );
+}
+
+#[test]
+fn d2_cc_with_dim_labels() {
+ assert_rendering(
+ d2("Columns", [Axis3::X, Axis3::X], true),
+ "\
+Columns
+╭──────────────────────────╮
+│ b │
+├────────┬────────┬────────┤
+│ b1 │ b2 │ b3 │
+├────────┼────────┼────────┤
+│ a │ a │ a │
+├──┬──┬──┼──┬──┬──┼──┬──┬──┤
+│a1│a2│a3│a1│a2│a3│a1│a2│a3│
+├──┼──┼──┼──┼──┼──┼──┼──┼──┤
+│ 0│ 1│ 2│ 3│ 4│ 5│ 6│ 7│ 8│
+╰──┴──┴──┴──┴──┴──┴──┴──┴──╯
+",
);
}
#[test]
fn d2_rr() {
- assert_eq!(
- d2("Rows", [Axis3::Y, Axis3::Y]).to_string(),
+ assert_rendering(
+ d2("Rows", [Axis3::Y, Axis3::Y], false),
"\
Rows
╭─────┬─╮
│ a2│7│
│ a3│8│
╰─────┴─╯
-"
+",
+ );
+}
+
+#[test]
+fn d2_rr_with_dim_labels() {
+ assert_rendering(
+ d2("Rows - Corner", [Axis3::Y, Axis3::Y], true),
+ "\
+Rows - Corner
+╭─────┬─╮
+│b a │ │
+├─────┼─┤
+│b1 a1│0│
+│ a2│1│
+│ a3│2│
+├─────┼─┤
+│b2 a1│3│
+│ a2│4│
+│ a3│5│
+├─────┼─┤
+│b3 a1│6│
+│ a2│7│
+│ a3│8│
+╰─────┴─╯
+",
);
}
#[test]
fn d2_cr() {
- assert_eq!(
- d2("Column x Row", [Axis3::X, Axis3::Y]).to_string(),
+ assert_rendering(
+ d2("Column x Row", [Axis3::X, Axis3::Y], false),
"\
Column x Row
╭──┬──┬──┬──╮
│b2│ 3│ 4│ 5│
│b3│ 6│ 7│ 8│
╰──┴──┴──┴──╯
-"
+",
);
}
#[test]
fn d2_rc() {
- assert_eq!(
- d2("Row x Column", [Axis3::Y, Axis3::X]).to_string(),
+ assert_rendering(
+ d2("Row x Column", [Axis3::Y, Axis3::X], false),
"\
Row x Column
╭──┬──┬──┬──╮
│a2│ 1│ 4│ 7│
│a3│ 2│ 5│ 8│
╰──┴──┴──┴──╯
-"
+",
);
}