Bound chart import expansion

This commit is contained in:
Neil Cawse
2026-10-22 06:11:32 +00:00
parent 8338a35da6
commit 38696b93a4
4 changed files with 39 additions and 5 deletions
+2 -2
View File
@@ -169,9 +169,9 @@ fn category_table(wb: &Workbook, sheet: usize, formula: &str, values: &str, inne
let column = |c: usize| (0..h).map(|row| at(row, c)).collect::<Vec<String>>();
let row = |rr: usize| (0..w).map(|c| at(rr, c)).collect::<Vec<String>>();
let value_count = eval_list(wb, sheet, values).len();
let by_columns = if value_count == h && w > 1 {
let by_columns = if value_count == h && w > 1 && h != w {
true
} else if value_count == w && h > 1 {
} else if value_count == w && h > 1 && h != w {
false
} else {
levels.by_columns
+16
View File
@@ -1230,6 +1230,22 @@ fn two_level_categories_resolve_and_draw() {
assert!(d.category_levels.is_empty());
}
#[test]
fn square_category_blocks_keep_their_declared_orientation_on_ties() {
let mut wb = Workbook::new();
let s = wb.sheet_mut(0).unwrap();
s.name = "Data".into();
for (i, value) in ["A1", "B1", "A2", "B2", "A3", "B3"].iter().enumerate() {
s.set_value(CellRef::parse(value).unwrap(), Value::from(*value));
s.set_value(CellRef::new(3, i as u32), Value::Number(i as f64 + 1.0));
}
let mut c = chart(ChartKind::Line);
c.series = vec![Series { categories: Some("Data!$A$1:$B$2".into()), values: "Data!$A$3:$B$3".into(), ..Series::default() }];
let d = resolve(&wb, 0, &c);
assert_eq!(d.categories, vec!["A2", "B2"]);
assert_eq!(d.category_levels, vec![vec!["A1", "B1"]]);
}
#[test]
fn a_huge_trendline_forecast_does_not_collapse_the_columns() {
let m = ApproxMeasure;
+8 -3
View File
@@ -117,6 +117,10 @@ fn data_ref(e: Option<&El>) -> Option<String> {
/// Most cached points a PivotChart series keeps (the chart renderer's own cap).
const MAX_CACHED_POINTS: usize = 100_000;
/// Trailing gaps after the last present point are retained only within this bound.
const MAX_TRAILING_GAPS: usize = 16;
/// Maximum series retained from one chart, matching the renderer's series cap.
const MAX_SERIES: usize = 255;
/// The points of a cache or literal (`c` holds `ptCount`, `pts` the `<c:pt>`s), by index; missing
/// points are `None`. Points without a valid `idx` (schema-invalid, but some writers omit it) go
@@ -125,8 +129,8 @@ fn read_points<'a>(c: &El, pts: &'a El) -> Option<Vec<Option<&'a str>>> {
let at = |i: usize, p: &El| p.attr("idx").and_then(|v| v.trim().parse::<usize>().ok()).unwrap_or(i);
let placed = || pts.kids("pt").enumerate().map(|(i, p)| (at(i, p), p)).filter(|(i, _)| *i < MAX_CACHED_POINTS);
let highest = placed().map(|(i, _)| i + 1).max().unwrap_or(0);
let count =
c.child_val("ptCount").and_then(|v| v.trim().parse::<usize>().ok()).filter(|n| (highest..=MAX_CACHED_POINTS).contains(n)).unwrap_or(highest);
let limit = highest.saturating_add(MAX_TRAILING_GAPS).min(MAX_CACHED_POINTS);
let count = c.child_val("ptCount").and_then(|v| v.trim().parse::<usize>().ok()).filter(|n| (highest..=limit).contains(n)).unwrap_or(highest);
if count == 0 {
return None;
}
@@ -431,7 +435,8 @@ pub(crate) fn read_chart(root: &El) -> Option<(Chart, Vec<PivotSeriesCache>)> {
let mut series = vec![];
let mut caches = vec![];
for (gi, (g, k)) in groups.iter().enumerate() {
for s in g.kids("ser") {
let remaining = MAX_SERIES.saturating_sub(series.len());
for s in g.kids("ser").take(remaining) {
let mut ser = read_series(s);
if groups.len() > 1 {
// Every series of a combo chart keeps its own group's kind, the first group's included.
+13
View File
@@ -237,6 +237,19 @@ fn literal_series_keep_their_points_without_a_valid_pt_count() {
assert_eq!(values(&format!(r#"<c:ptCount val="3"/>{pts}"#)), "{1,2,#N/A}");
}
#[test]
fn chart_cache_counts_and_series_are_bounded() {
let sparse = r#"<c:ser><c:idx val="0"/><c:order val="0"/><c:val><c:numLit><c:ptCount val="100000"/><c:pt idx="1"><c:v>2</c:v></c:pt></c:numLit></c:val></c:ser>"#;
let chart = read(&chart_xml(false, &format!("<c:pieChart>{sparse}</c:pieChart>"), ""));
assert_eq!(chart.series[0].values, "{#N/A,2}");
let series: String = (0..300)
.map(|i| format!(r#"<c:ser><c:idx val="{i}"/><c:order val="{i}"/><c:val><c:numLit><c:ptCount val="1"/><c:pt idx="0"><c:v>{i}</c:v></c:pt></c:numLit></c:val></c:ser>"#))
.collect();
let chart = read(&chart_xml(false, &format!("<c:pieChart>{series}</c:pieChart>"), ""));
assert_eq!(chart.series.len(), 255);
}
#[test]
fn a_deleted_automatic_title_stays_deleted() {
let one = ser("<c:v>Vehicles</c:v>", &str_cache("S!$A$1:$A$2", &["a", "b"]), &num_cache("S!$B$1:$B$2", 2, &[]));