XLSX: 100% stacked lines on a combo chart's secondary axis keep their kind when saved

This commit is contained in:
dotCooCoo
2026-10-28 06:10:02 -07:00
parent 8e81dade33
commit 4550a1eba7
2 changed files with 33 additions and 6 deletions
+13 -6
View File
@@ -817,12 +817,19 @@ pub fn write_chart(wb: &Workbook, si: usize, ch: &Chart, book_name: &str) -> Str
s.push_str(&group_xml(G::Line100 { markers: true }, &stacked, ch, (1, 2), wb, si));
}
if !sec.is_empty() {
let g = match sec.first().and_then(|(_, x)| x.kind).map(group_for) {
Some(b @ G::Bar { .. }) => b,
_ => G::Line { stacked: false, markers: false },
};
s.push_str(&group_xml(g, &sec, ch, (3, 4), wb, si));
secondary = Some(g);
if let Some(b @ G::Bar { .. }) = sec.first().and_then(|(_, x)| x.kind).map(group_for) {
s.push_str(&group_xml(b, &sec, ch, (3, 4), wb, si));
secondary = Some(b);
} else {
let sec_plain: Vec<(usize, &Series)> = sec.iter().filter(|(i, x)| !pct(*i, x)).copied().collect();
let sec_stacked: Vec<(usize, &Series)> = sec.iter().filter(|(i, x)| pct(*i, x)).copied().collect();
for (g, members) in [(G::Line { stacked: false, markers: false }, sec_plain), (G::Line100 { markers: true }, sec_stacked)] {
if !members.is_empty() {
s.push_str(&group_xml(g, &members, ch, (3, 4), wb, si));
secondary.get_or_insert(g);
}
}
}
}
} else {
primary_g = match group_for(ch.kind) {
@@ -80,3 +80,23 @@ fn combo_line_series_keep_their_kinds_order_and_shared_percent_stack_when_saved(
let again = read_chart(&parse(saved.as_bytes()).unwrap()).unwrap();
assert_eq!(again.series.iter().map(|s| s.kind).collect::<Vec<_>>(), want, "{saved}");
}
#[test]
fn a_hundred_percent_stacked_line_on_the_secondary_axis_keeps_its_kind_when_saved() {
let ser =
|i: usize| format!("<c:ser><c:idx val=\"{i}\"/><c:order val=\"{i}\"/><c:val><c:numRef><c:f>Data!$A$1:$A$5</c:f></c:numRef></c:val></c:ser>");
let xml = format!(
"<c:chartSpace xmlns:c=\"http://schemas.openxmlformats.org/drawingml/2006/chart\"><c:chart><c:plotArea>\
<c:barChart><c:barDir val=\"col\"/><c:grouping val=\"clustered\"/>{}<c:axId val=\"1\"/><c:axId val=\"2\"/></c:barChart>\
<c:lineChart><c:grouping val=\"percentStacked\"/>{}<c:axId val=\"3\"/><c:axId val=\"4\"/></c:lineChart>\
</c:plotArea></c:chart></c:chartSpace>",
ser(0),
ser(1)
);
let ch = read_chart(&parse(xml.as_bytes()).unwrap()).unwrap();
assert!(ch.series.get(1).is_some_and(|s| s.secondary && s.kind == Some(ChartKind::LineStacked100Markers)));
let saved = write_chart(&Workbook::default(), 0, &ch, "Book1");
let again = read_chart(&parse(saved.as_bytes()).unwrap()).unwrap();
let got: Vec<(Option<ChartKind>, bool)> = again.series.iter().map(|s| (s.kind, s.secondary)).collect();
assert_eq!(got, [(Some(ChartKind::ColumnClustered), false), (Some(ChartKind::LineStacked100Markers), true)], "{saved}");
}