docx charts: outline unfilled bars, honour no-fill areas, keep source-linked label formats

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Sonnet 5.5 committed 2026-11-15 13:30:44 +01:00
1 parent 8761999c9b
commit b4463c6203
5 files changed
+91 -12

No files matched your search

+3 -2
View File
@@ -162,8 +162,9 @@ pub(super) struct Series {
pub(super) x_fmt: Option<String>,
/// Data labels on or off for this series (`c:dLbls` of its own), when it says.
pub(super) labels: Option<bool>,
/// The labels' own number format (`c:dLbls/c:numFmt` not linked to the source), if any.
pub(super) label_fmt: Option<String>,
/// 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<Option<String>>,
/// The data labels' `c:dLblPos`, when the file gives a known one.
pub(super) label_pos: Option<LabelPos>,
/// The marker's own fill (`c:marker/c:spPr`); without it markers take the automatic colour.
+10 -7
View File
@@ -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<bool> {
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<String> {
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<Option<String>> {
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<Pos> {
+8 -2
View File
@@ -468,7 +468,11 @@ fn bars(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<DataL
let (p0, p1) = (p0.min(p1), p0.max(p1));
let segs = if g.horiz { rect_segs(p0, a, p1, a + thick) } else { rect_segs(a, p0, a + thick, p1) };
if !s.no_fill {
fill(out, segs, c.point_colour(j, i));
fill(out, segs.clone(), c.point_colour(j, i));
}
// An explicit outline shows without a fill too (the series' colour is the line's here).
if let (true, true, Some(w)) = (s.line, s.no_fill, s.line_w) {
stroke(out, segs, c.point_colour(j, i), w);
}
if let Some(text) = label_of(s, i) {
let mid = a + thick / 2.0;
@@ -626,7 +630,9 @@ fn areas(out: &mut Vec<GraphicItem>, c: &Chart, g: &Frame, labels: &mut Vec<Data
segs.push(PathSeg::Line(g.cat_x(i), g.val(*b)));
}
segs.push(PathSeg::Close);
fill(out, segs, c.series_colour(j));
if !s.no_fill {
fill(out, segs, c.series_colour(j));
}
for (i, (t, b)) in top.iter().zip(&base).enumerate() {
if let Some(text) = label_of(s, i) {
labels.push(DataLabel { x: g.cat_x(i), y: g.val((t + b) / 2.0), text, inside: true, pos: LabelPos::Above, off: 0.0 });
+1 -1
View File
@@ -140,7 +140,7 @@ pub(super) fn label_of(s: &Series, i: usize) -> Option<String> {
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),
+69
View File
@@ -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<GraphicItem> {
let s = ser(0, "S", None, &["Q1", "Q2"], &["4", "8"]).replace("<c:cat>", &format!("{sp}<c:cat>"));
let plot = match kind {
"area" => format!(
r#"<c:areaChart><c:grouping val="standard"/>{s}<c:axId val="1"/><c:axId val="2"/></c:areaChart><c:catAx><c:axId val="1"/></c:catAx><c:valAx><c:axId val="2"/></c:valAx>"#
),
dir => format!(
r#"<c:barChart><c:barDir val="{dir}"/><c:grouping val="clustered"/>{s}<c:axId val="1"/><c:axId val="2"/></c:barChart><c:catAx><c:axId val="1"/></c:catAx><c:valAx><c:axId val="2"/></c:valAx>"#
),
};
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#"<c:spPr><a:noFill/><a:ln w="25400"><a:solidFill><a:srgbClr val="FF0000"/></a:solidFill></a:ln></c:spPr>"#;
let no_line = r#"<c:spPr><a:noFill/><a:ln><a:noFill/></a:ln></c:spPr>"#;
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#"<c:spPr><a:solidFill><a:srgbClr val="FF0000"/></a:solidFill></c:spPr>"#;
let none = r#"<c:spPr><a:noFill/><a:ln w="25400"><a:solidFill><a:srgbClr val="FF0000"/></a:solidFill></a:ln></c:spPr>"#;
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#"<c:dLbls><c:numFmt formatCode="{fmt}" sourceLinked="{{}}"/><c:showVal val="1"/></c:dLbls>"#);
let shown = |items: &[GraphicItem]| -> Vec<String> {
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("<c:cat>", &format!("{series_dl}<c:cat>"));
let chart_dl = dl("0%").replace("{}", "0");
let plot = format!(
r#"<c:lineChart><c:grouping val="standard"/>{s}{chart_dl}<c:axId val="1"/><c:axId val="2"/></c:lineChart><c:catAx><c:axId val="1"/></c:catAx><c:valAx><c:axId val="2"/></c:valAx>"#
);
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#"<c:dLbls><c:showVal val="1"/></c:dLbls>"#);
assert!(inherited.contains(&"320%".to_string()), "{inherited:?}");
}