From 40d27e4ededfcb0eb85d0017b129575760721258 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 18:59:56 +0100 Subject: [PATCH] docx: bar category labels start after the axis title strip Co-Authored-By: Claude Sonnet 5.5 --- crates/docx/src/read/chart/plot.rs | 11 +++++++---- crates/docx/src/read/chart/tests.rs | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/crates/docx/src/read/chart/plot.rs b/crates/docx/src/read/chart/plot.rs index 09fe672d0..50108b8dc 100644 --- a/crates/docx/src/read/chart/plot.rs +++ b/crates/docx/src/read/chart/plot.rs @@ -142,6 +142,8 @@ impl Axes { /// The room around the plot for axis labels, and the category labels as they'll be drawn. struct Margins { left: f32, + /// Part of `left` taken by the rotated category-axis title (bars): the labels start after it. + title: f32, top: f32, bottom: f32, /// Category labels in lines (none for scatter charts or a deleted category axis). @@ -159,16 +161,17 @@ fn axis_margins(c: &Chart, ax: &Axes, area: Rect) -> Margins { let scatter = c.kind == Kind::Scatter; let (cat_on, val_on) = (!c.cat.deleted, !c.val.deleted); let (cat_labels, val_labels) = (cat_on && c.cat.labels, val_on && c.val.labels); + let title = title_left(c, cat_on, val_on, lh); let left = if c.horizontal { if cat_labels { widest(&c.cats, fs).min(area.w * 0.4) + GAP } else { 0.0 } } else if val_labels { ax.vticks.iter().map(|(_, s)| text_w(s, fs)).fold(0.0, f32::max) + GAP } else { 0.0 - } + title_left(c, cat_on, val_on, lh); + } + title; let band_w = (area.w - left).max(1.0) / c.n as f32; let wrapped: Vec> = if cat_labels && !scatter { - let width = if c.horizontal { left - GAP } else { band_w }; + let width = if c.horizontal { left - title - GAP } else { band_w }; c.cats.iter().map(|s| wrap(s, width, fs)).collect() } else { Vec::new() @@ -184,7 +187,7 @@ fn axis_margins(c: &Chart, ax: &Axes, area: Rect) -> Margins { 0.0 }) + title_bottom(c, cat_on, val_on, lh); let top = if !c.horizontal && val_labels { lh / 2.0 } else { 0.0 }; - Margins { left, top, bottom, wrapped, skip } + Margins { left, title, top, bottom, wrapped, skip } } /// Room for the vertical axis title on the plot's left: one line when the axis that sits there @@ -332,7 +335,7 @@ fn draw_category_labels(out: &mut Vec, c: &Chart, g: &Frame, xticks for (i, lines) in m.wrapped.iter().enumerate() { let top = g.band_start(i) + g.band() / 2.0 - lines.len() as f32 * lh / 2.0; for (k, s) in lines.iter().enumerate() { - text(out, Rect { x: area.x, y: top + k as f32 * lh, w: m.left - GAP, h: lh }, s, fs, TextAlign::Right, false); + text(out, Rect { x: area.x + m.title, y: top + k as f32 * lh, w: m.left - m.title - GAP, h: lh }, s, fs, TextAlign::Right, false); } } } else { diff --git a/crates/docx/src/read/chart/tests.rs b/crates/docx/src/read/chart/tests.rs index 8b26f40c4..528f46ee2 100644 --- a/crates/docx/src/read/chart/tests.rs +++ b/crates/docx/src/read/chart/tests.rs @@ -594,3 +594,27 @@ fn series_without_a_usable_c_idx_take_their_place() { let big = [renumbered(&a, &format!(r#""#, u32::MAX))]; assert_eq!(column_colours(&big).0.len(), 1); } + +#[test] +fn bar_category_labels_start_after_the_axis_title() { + let long = "A category label that is much too long for its column"; + let title = r#"Region"#; + let plot = format!( + r#"{}{title}"#, + ser(0, "S", Some("FF0000"), &[long, "Short"], &["4", "8"]), + ); + let items = chart_items(&space(&plot), &theme(), 300.0, 200.0); + let rect_of = |f: &dyn Fn(&str, &TextAlign) -> bool| -> Vec<[f32; 4]> { + items + .iter() + .filter_map(|it| if let GraphicItem::Text { rect, text, align, .. } = it { f(text, align).then_some(*rect) } else { None }) + .collect() + }; + let title_rect = rect_of(&|t, _| t == "Region"); + assert_eq!(title_rect.len(), 1); + let strip_end = title_rect[0][0] + title_rect[0][2]; + let labels = rect_of(&|t, a| matches!(a, TextAlign::Right) && t != "Region" && (long.contains(t) || t == "Short")); + assert!(!labels.is_empty()); + assert!(labels.iter().all(|r| r[0] >= strip_end - 1e-3), "labels {labels:?} overlap the title strip ending at {strip_end}"); + assert!(labels.iter().all(|r| r[2] <= 0.4 * 300.0), "{labels:?}"); +}