From 0edccaf1bc46f21bc35cc3b40f85a5a13b5bbae7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 14 Nov 2026 18:59:56 +0100 Subject: [PATCH] docx: category gridlines follow line and area points when the axis crosses mid Co-Authored-By: Claude Sonnet 5.5 --- crates/docx/src/read/chart/plot.rs | 21 ++++++++++------ crates/docx/src/read/chart/tests.rs | 38 +++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/crates/docx/src/read/chart/plot.rs b/crates/docx/src/read/chart/plot.rs index 50108b8dc..eae203546 100644 --- a/crates/docx/src/read/chart/plot.rs +++ b/crates/docx/src/read/chart/plot.rs @@ -220,7 +220,7 @@ pub(super) fn cartesian(out: &mut Vec, c: &Chart, area: Rect) { draw_grid(out, &g, &ax.vticks); } if c.cat.grid { - draw_cat_grid(out, &g, &ax.xticks, c.kind == Kind::Scatter); + draw_cat_grid(out, &g, &ax.xticks, c.kind); } let mut labels = Vec::new(); match c.kind { @@ -274,11 +274,12 @@ fn draw_grid(out: &mut Vec, g: &Frame, ticks: &[(f64, String)]) { } } -/// Gridlines along the category axis: across the plot at each category's centre, at the x ticks -/// of a scatter chart. -fn draw_cat_grid(out: &mut Vec, g: &Frame, xticks: &[(f64, String)], scatter: bool) { +/// Gridlines along the category axis: across the plot at each category's centre (where line and +/// area points sit, on the tick marks when the axis crosses "midCat"), at the x ticks of a +/// scatter chart. +fn draw_cat_grid(out: &mut Vec, g: &Frame, xticks: &[(f64, String)], kind: Kind) { let plot = g.plot; - if scatter { + if kind == Kind::Scatter { for (t, _) in xticks { let p = g.scat_x(*t); stroke(out, seg(p, plot.y, p, plot.bottom()), GRID, 0.75); @@ -286,8 +287,14 @@ fn draw_cat_grid(out: &mut Vec, g: &Frame, xticks: &[(f64, String)] return; } for i in 0..g.n { - let p = g.band_start(i) + g.band() / 2.0; - let segs = if g.horiz { seg(plot.x, p, plot.right(), p) } else { seg(p, plot.y, p, plot.bottom()) }; + let segs = if g.horiz { + let p = g.band_start(i) + g.band() / 2.0; + seg(plot.x, p, plot.right(), p) + } else { + // Bars stay in their bands; only line and area points move onto the ticks. + let p = if matches!(kind, Kind::Line | Kind::Area) { g.cat_x(i) } else { g.band_start(i) + g.band() / 2.0 }; + seg(p, plot.y, p, plot.bottom()) + }; stroke(out, segs, GRID, 0.75); } } diff --git a/crates/docx/src/read/chart/tests.rs b/crates/docx/src/read/chart/tests.rs index 528f46ee2..74573b5d8 100644 --- a/crates/docx/src/read/chart/tests.rs +++ b/crates/docx/src/read/chart/tests.rs @@ -618,3 +618,41 @@ fn bar_category_labels_start_after_the_axis_title() { 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:?}"); } + +#[test] +fn category_gridlines_follow_the_points_when_the_axis_crosses_mid() { + let line = |cross: &str| { + let plot = format!( + r#"{}"#, + ser(0, "L", Some("FF0000"), &["Q1", "Q2", "Q3"], &["1", "5", "3"]), + ); + let items = chart_items(&space(&plot), &theme(), 300.0, 200.0); + let grid: Vec = items + .iter() + .filter_map(|it| match it { + GraphicItem::Path { segs, stroke: Some(c), .. } if *c == GRID => match segs.first() { + Some(PathSeg::Move(x, _)) => Some(*x), + _ => None, + }, + _ => None, + }) + .collect(); + let pts: Vec = items + .iter() + .filter_map(|it| match it { + GraphicItem::Path { segs, stroke: Some(c), .. } if *c == Rgb(255, 0, 0) => Some( + segs.iter().filter_map(|s| if let PathSeg::Move(x, _) | PathSeg::Line(x, _) = s { Some(*x) } else { None }).collect::>(), + ), + _ => None, + }) + .next() + .unwrap_or_default(); + (grid, pts) + }; + for cross in ["midCat", "between"] { + let (grid, pts) = line(cross); + assert_eq!(grid.len(), 3, "{cross}"); + assert_eq!(pts.len(), 3, "{cross}"); + assert!(grid.iter().zip(&pts).all(|(g, p)| (g - p).abs() < 1e-3), "{cross}: grid {grid:?} points {pts:?}"); + } +}