mirror of
https://github.com/storytold/gridcraft.git
synced 2026-12-02 02:33:55 -06:00
XLSX: combo charts keep each area, marked line and column grouping in its own group when saved
Lines and secondary series are written as one chart group per kind, so areas, lines with markers and columns with different groupings open again as the same kinds instead of plain lines or the first column's grouping.
This commit is contained in:
+24
-27
@@ -767,6 +767,25 @@ fn strip_book(link: &str) -> &str {
|
||||
link.strip_prefix('[').and_then(|r| r.split_once(']')).map_or(link, |(_, rest)| rest)
|
||||
}
|
||||
|
||||
/// Splits a combo chart's series into one group per kind, in the order each kind first appears.
|
||||
/// Columns, bars, lines and areas keep their own grouping and markers; 100% stacked lines share one group
|
||||
/// whose series write their own markers; other kinds are written as plain lines.
|
||||
fn combo_groups<'a>(series: &[(usize, &'a Series)], kind: impl Fn(usize, &Series) -> Option<ChartKind>) -> Vec<(G, Vec<(usize, &'a Series)>)> {
|
||||
let mut groups: Vec<(G, Vec<(usize, &'a Series)>)> = vec![];
|
||||
for &(i, x) in series {
|
||||
let g = match kind(i, x).filter(|k| *k != ChartKind::Combo).map(group_for) {
|
||||
Some(g @ (G::Bar { .. } | G::Line { .. } | G::Area { .. })) => g,
|
||||
Some(G::Line100 { .. }) => G::Line100 { markers: true },
|
||||
_ => G::Line { stacked: false, markers: false },
|
||||
};
|
||||
match groups.iter_mut().find(|(k, _)| *k == g) {
|
||||
Some((_, members)) => members.push((i, x)),
|
||||
None => groups.push((g, vec![(i, x)])),
|
||||
}
|
||||
}
|
||||
groups
|
||||
}
|
||||
|
||||
/// Chart part XML for a model chart on sheet `si`.
|
||||
pub fn write_chart(wb: &Workbook, si: usize, ch: &Chart, book_name: &str) -> String {
|
||||
let mut s = String::from(
|
||||
@@ -806,34 +825,12 @@ pub fn write_chart(wb: &Workbook, si: usize, ch: &Chart, book_name: &str) -> Str
|
||||
if !bars.is_empty() || (lines.is_empty() && sec.is_empty()) {
|
||||
s.push_str(&group_xml(primary_g, &bars, ch, (1, 2), wb, si));
|
||||
}
|
||||
// 100% stacked line series share one percentStacked group, each with its own markers.
|
||||
let pct = |i: usize, x: &Series| matches!(kind(i, x).map(group_for), Some(G::Line100 { .. }));
|
||||
let plain: Vec<(usize, &Series)> = lines.iter().filter(|(i, x)| !pct(*i, x)).copied().collect();
|
||||
let stacked: Vec<(usize, &Series)> = lines.iter().filter(|(i, x)| pct(*i, x)).copied().collect();
|
||||
if !plain.is_empty() {
|
||||
s.push_str(&group_xml(G::Line { stacked: false, markers: false }, &plain, ch, (1, 2), wb, si));
|
||||
for (g, members) in combo_groups(&lines, |i, x| kind(i, x)) {
|
||||
s.push_str(&group_xml(g, &members, ch, (1, 2), wb, si));
|
||||
}
|
||||
if !stacked.is_empty() {
|
||||
s.push_str(&group_xml(G::Line100 { markers: true }, &stacked, ch, (1, 2), wb, si));
|
||||
}
|
||||
if !sec.is_empty() {
|
||||
// Secondary bars, lines and 100% stacked lines each get their own group.
|
||||
let bar_of = |x: &Series| match x.kind.map(group_for) {
|
||||
Some(b @ G::Bar { .. }) => Some(b),
|
||||
_ => None,
|
||||
};
|
||||
let sec_bars: Vec<(usize, &Series)> = sec.iter().filter(|(_, x)| bar_of(x).is_some()).copied().collect();
|
||||
let sec_plain: Vec<(usize, &Series)> = sec.iter().filter(|(i, x)| bar_of(x).is_none() && !pct(*i, x)).copied().collect();
|
||||
let sec_stacked: Vec<(usize, &Series)> = sec.iter().filter(|(i, x)| pct(*i, x)).copied().collect();
|
||||
let bar_g = sec_bars.first().and_then(|(_, x)| bar_of(x)).unwrap_or(G::Bar { bar: false, grouping: "clustered" });
|
||||
for (g, members) in
|
||||
[(bar_g, sec_bars), (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);
|
||||
}
|
||||
}
|
||||
for (g, members) in combo_groups(&sec, |_, x| x.kind) {
|
||||
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) {
|
||||
|
||||
@@ -130,3 +130,38 @@ fn secondary_columns_lines_and_hundred_percent_stacked_lines_keep_their_kinds_wh
|
||||
let again = read_chart(&parse(saved.as_bytes()).unwrap()).unwrap();
|
||||
assert_eq!(again.series.iter().map(|s| (s.kind, s.secondary)).collect::<Vec<_>>(), want, "{saved}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn combo_areas_marked_lines_and_mixed_column_groupings_keep_their_kinds_when_saved() {
|
||||
let ser = |i: usize, marker: &str| {
|
||||
format!("<c:ser><c:idx val=\"{i}\"/><c:order val=\"{i}\"/>{marker}<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:areaChart><c:grouping val=\"stacked\"/>{}<c:axId val=\"1\"/><c:axId val=\"2\"/></c:areaChart>\
|
||||
<c:lineChart><c:grouping val=\"standard\"/>{}<c:axId val=\"1\"/><c:axId val=\"2\"/></c:lineChart>\
|
||||
<c:barChart><c:barDir val=\"col\"/><c:grouping val=\"stacked\"/>{}<c:overlap val=\"100\"/><c:axId val=\"3\"/><c:axId val=\"4\"/></c:barChart>\
|
||||
<c:barChart><c:barDir val=\"col\"/><c:grouping val=\"clustered\"/>{}<c:axId val=\"3\"/><c:axId val=\"4\"/></c:barChart>\
|
||||
<c:areaChart><c:grouping val=\"percentStacked\"/>{}<c:axId val=\"3\"/><c:axId val=\"4\"/></c:areaChart>\
|
||||
<c:lineChart><c:grouping val=\"standard\"/>{}<c:axId val=\"3\"/><c:axId val=\"4\"/></c:lineChart>\
|
||||
</c:plotArea></c:chart></c:chartSpace>",
|
||||
ser(0, ""),
|
||||
ser(1, ""),
|
||||
ser(2, ""),
|
||||
ser(3, ""),
|
||||
ser(4, ""),
|
||||
ser(5, ""),
|
||||
ser(6, NO_MARKER)
|
||||
);
|
||||
let ch = read_chart(&parse(xml.as_bytes()).unwrap()).unwrap();
|
||||
let got: Vec<(Option<ChartKind>, bool)> = ch.series.iter().map(|s| (s.kind, s.secondary)).collect();
|
||||
assert_eq!(got[1], (Some(ChartKind::AreaStacked), false));
|
||||
assert_eq!(got[3], (Some(ChartKind::ColumnStacked), true));
|
||||
assert_eq!(got[4], (Some(ChartKind::ColumnClustered), true));
|
||||
assert_eq!(got[5], (Some(ChartKind::AreaStacked100), true));
|
||||
assert_eq!(got[6], (Some(ChartKind::Line), true));
|
||||
let saved = write_chart(&Workbook::default(), 0, &ch, "Book1");
|
||||
let again = read_chart(&parse(saved.as_bytes()).unwrap()).unwrap();
|
||||
assert_eq!(again.series.iter().map(|s| (s.kind, s.secondary)).collect::<Vec<_>>(), got, "{saved}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user