diff --git a/crates/xlsx/src/chart.rs b/crates/xlsx/src/chart.rs index dccbb60f5..17c945eac 100644 --- a/crates/xlsx/src/chart.rs +++ b/crates/xlsx/src/chart.rs @@ -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) { diff --git a/crates/xlsx/src/tests/chart_line_percent_stacked.rs b/crates/xlsx/src/tests/chart_line_percent_stacked.rs index 5ecba4049..b5c58faa5 100644 --- a/crates/xlsx/src/tests/chart_line_percent_stacked.rs +++ b/crates/xlsx/src/tests/chart_line_percent_stacked.rs @@ -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::>(), want, "{saved}"); } + +#[test] +fn a_hundred_percent_stacked_line_on_the_secondary_axis_keeps_its_kind_when_saved() { + let ser = + |i: usize| format!("Data!$A$1:$A$5"); + let xml = format!( + "\ + {}\ + {}\ + ", + 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, bool)> = again.series.iter().map(|s| (s.kind, s.secondary)).collect(); + assert_eq!(got, [(Some(ChartKind::ColumnClustered), false), (Some(ChartKind::LineStacked100Markers), true)], "{saved}"); +}