From b4463c620348f649b2907e35aaef901010130842 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 15 Nov 2026 13:30:44 +0100 Subject: [PATCH] docx charts: outline unfilled bars, honour no-fill areas, keep source-linked label formats Co-Authored-By: Claude Sonnet 5.5 --- crates/docx/src/read/chart/model.rs | 5 ++- crates/docx/src/read/chart/parse.rs | 17 ++++--- crates/docx/src/read/chart/plot.rs | 10 ++++- crates/docx/src/read/chart/polar.rs | 2 +- crates/docx/src/read/chart/tests.rs | 69 +++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 12 deletions(-) diff --git a/crates/docx/src/read/chart/model.rs b/crates/docx/src/read/chart/model.rs index 37d06e31f..13da1d654 100644 --- a/crates/docx/src/read/chart/model.rs +++ b/crates/docx/src/read/chart/model.rs @@ -162,8 +162,9 @@ pub(super) struct Series { pub(super) x_fmt: Option, /// Data labels on or off for this series (`c:dLbls` of its own), when it says. pub(super) labels: Option, - /// The labels' own number format (`c:dLbls/c:numFmt` not linked to the source), if any. - pub(super) label_fmt: Option, + /// The labels' number format (`c:dLbls/c:numFmt`): `None` when not given (the chart type's + /// applies), `Some(None)` when linked to the source, else `Some(Some(code))`. + pub(super) label_fmt: Option>, /// The data labels' `c:dLblPos`, when the file gives a known one. pub(super) label_pos: Option, /// The marker's own fill (`c:marker/c:spPr`); without it markers take the automatic colour. diff --git a/crates/docx/src/read/chart/parse.rs b/crates/docx/src/read/chart/parse.rs index 67b09d795..522d50b3b 100644 --- a/crates/docx/src/read/chart/parse.rs +++ b/crates/docx/src/read/chart/parse.rs @@ -44,6 +44,7 @@ impl Chart { s.values.resize(n, None); s.xs.resize(n, None); s.labels = Some(s.labels.or(type_labels).unwrap_or(false)); + // Absent (`None`) inherits the chart type's; explicitly source-linked (`Some(None)`) does not. if s.label_fmt.is_none() { s.label_fmt.clone_from(&type_fmt); } @@ -191,13 +192,15 @@ fn show_values(d: Option<&El>) -> Option { d.child("c:showVal").map(|v| v.attr("val").is_none_or(truthy)) } -/// The number format of data labels (`c:dLbls/c:numFmt`) when it is not linked to the source. -fn label_format(d: Option<&El>) -> Option { - d?.child("c:numFmt") - .filter(|f| f.attr("sourceLinked") != Some("1")) - .and_then(|f| f.attr("formatCode")) - .filter(|f| f.chars().count() <= craft_wordmodel::chart::MAX_FMT) - .map(str::to_string) +/// The number format of data labels (`c:dLbls/c:numFmt`): `None` when the labels say nothing, +/// `Some(None)` when they are linked to the source (so a chart-level format does not apply), +/// `Some(Some(code))` for their own format code. +fn label_format(d: Option<&El>) -> Option> { + let f = d?.child("c:numFmt")?; + if f.attr("sourceLinked").is_some_and(truthy) { + return Some(None); + } + f.attr("formatCode").filter(|f| f.chars().count() <= craft_wordmodel::chart::MAX_FMT).map(|f| Some(f.to_string())) } fn legend_pos(chart: &El) -> Option { diff --git a/crates/docx/src/read/chart/plot.rs b/crates/docx/src/read/chart/plot.rs index f671f1d49..22d05b554 100644 --- a/crates/docx/src/read/chart/plot.rs +++ b/crates/docx/src/read/chart/plot.rs @@ -468,7 +468,11 @@ fn bars(out: &mut Vec, c: &Chart, g: &Frame, labels: &mut Vec, c: &Chart, g: &Frame, labels: &mut Vec Option { return None; } let v = s.raw.get(i).copied().flatten()?; - let fmt = s.label_fmt.as_deref().or(s.fmt.as_deref()).filter(|f| !f.eq_ignore_ascii_case("General")); + let fmt = s.label_fmt.as_ref().map_or(s.fmt.as_deref(), |f| f.as_deref().or(s.fmt.as_deref())).filter(|f| !f.eq_ignore_ascii_case("General")); Some(match fmt { Some(f) => format_value(v, f, 1.0), None => general_value(v), diff --git a/crates/docx/src/read/chart/tests.rs b/crates/docx/src/read/chart/tests.rs index e5d7e4db6..ae80c028e 100644 --- a/crates/docx/src/read/chart/tests.rs +++ b/crates/docx/src/read/chart/tests.rs @@ -830,3 +830,72 @@ fn horizontal_value_axis_title_gets_its_own_strip() { let p = plot_left(&items).unwrap(); assert!(p.is_finite() && p <= 300.0 * 0.3, "{p}"); } + +/// A one-series chart whose series carries `sp` as its `c:spPr`. +fn chart_with_sp(kind: &str, sp: &str) -> Vec { + let s = ser(0, "S", None, &["Q1", "Q2"], &["4", "8"]).replace("", &format!("{sp}")); + let plot = match kind { + "area" => format!( + r#"{s}"# + ), + dir => format!( + r#"{s}"# + ), + }; + chart_items(&space(&plot), &theme(), 300.0, 200.0) +} + +/// The series' closed shapes: (filled, stroked with the given width). +fn series_shapes(items: &[GraphicItem], width: f32) -> (usize, usize) { + let closed = |segs: &[PathSeg]| segs.iter().any(|s| matches!(s, PathSeg::Close)); + let fills = items.iter().filter(|it| matches!(it, GraphicItem::Path { segs, fill: Some(_), .. } if closed(segs))).count(); + let strokes = items + .iter() + .filter(|it| matches!(it, GraphicItem::Path { segs, fill: None, stroke: Some(_), stroke_width, .. } if closed(segs) && (*stroke_width - width).abs() < 0.01)) + .count(); + (fills, strokes) +} + +#[test] +fn unfilled_bars_keep_their_explicit_outline() { + let outlined = r#""#; + let no_line = r#""#; + for dir in ["col", "bar"] { + // Two points, two outlines, no fill. + assert_eq!(series_shapes(&chart_with_sp(dir, outlined), 2.0), (0, 2), "{dir}"); + // Control: no fill and no line draws nothing. + assert_eq!(series_shapes(&chart_with_sp(dir, no_line), 2.0), (0, 0), "{dir}"); + } +} + +#[test] +fn no_fill_area_series_is_not_painted() { + let filled = r#""#; + let none = r#""#; + assert_eq!(series_shapes(&chart_with_sp("area", filled), 2.0).0, 1); + assert_eq!(series_shapes(&chart_with_sp("area", none), 2.0).0, 0); +} + +#[test] +fn source_linked_series_labels_do_not_inherit_the_chart_format() { + let dl = |fmt: &str| format!(r#""#); + let shown = |items: &[GraphicItem]| -> Vec { + items.iter().filter_map(|it| if let GraphicItem::Text { text, .. } = it { Some(text.clone()) } else { None }).collect() + }; + // Chart-level 0%; the series' own labels are `linked` (or have no labels element at all). + let run = |series_dl: &str| { + let s = ser(0, "S", Some("FF0000"), &["Q1", "Q2", "Q3"], &["3.2", "4.1", "5"]).replace("", &format!("{series_dl}")); + let chart_dl = dl("0%").replace("{}", "0"); + let plot = format!( + r#"{s}{chart_dl}"# + ); + shown(&chart_items(&space(&plot), &theme(), 300.0, 200.0)) + }; + for linked in ["1", "true"] { + let own = run(&dl("General").replace("{}", linked)); + assert!(own.contains(&"3.2".to_string()) && !own.contains(&"320%".to_string()), "sourceLinked={linked}: {own:?}"); + } + // Control: a series without a label format still takes the chart's. + let inherited = run(r#""#); + assert!(inherited.contains(&"320%".to_string()), "{inherited:?}"); +}