mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-08 08:20:13 +00:00
Graph data: a dropped empty row adds no blank series
Dropping rows with neither a label nor a value read the data padded to the widest row, so a dropped row wider than the rest (`,,`) left every kept row one blank cell wider. The graph then had an extra blank series, and a right-axis assignment to that index was kept. Each kept row now keeps its own width. This applies to graph.setData and to a graph variable's data set.
This commit is contained in:
@@ -136,9 +136,17 @@ pub(super) fn apply_data(g: &mut GraphSpec, p: &Value) -> bool {
|
||||
changed
|
||||
}
|
||||
|
||||
/// Drop rows with neither a value nor a label; a label-only row stays, its values blank.
|
||||
/// Drop rows with neither a value nor a label; a label-only row stays, its values blank. Each kept row keeps its own
|
||||
/// width, so a dropped row wider than the rest adds no blank series.
|
||||
fn drop_empty_rows(g: &mut GraphSpec) {
|
||||
let mut cells = g.cells();
|
||||
let blank: std::collections::HashSet<[usize; 2]> = g.blanks.iter().copied().collect();
|
||||
let mut cells: Vec<Vec<Option<f64>>> = g
|
||||
.rows
|
||||
.iter()
|
||||
.take(vectorcraft_doc::MAX_GRAPH_CATEGORIES)
|
||||
.enumerate()
|
||||
.map(|(c, r)| r.iter().take(vectorcraft_doc::MAX_GRAPH_SERIES).enumerate().map(|(s, v)| (!blank.contains(&[c, s])).then_some(*v)).collect())
|
||||
.collect();
|
||||
let keep: Vec<bool> = (0..cells.len())
|
||||
.map(|c| cells.get(c).is_some_and(|r| r.iter().any(Option::is_some)) || g.categories.get(c).is_some_and(|l| !l.is_empty()))
|
||||
.collect();
|
||||
|
||||
@@ -1647,7 +1647,8 @@ fn graph_variable_applies_csv_as_set_data() {
|
||||
s.execute("variable.makeGraphDynamic", &json!({})).unwrap();
|
||||
let name = s.execute("variable.list", &json!({})).unwrap()["variables"][0]["name"].as_str().unwrap().to_string();
|
||||
let spec = |s: &Session, g: u64| s.doc().unwrap().doc.node(NodeId(g)).unwrap().extras().graph.as_deref().unwrap().clone();
|
||||
for (row, csv) in [("One", ",a\nQ1,10\nQ2,5"), ("Blank", ",a\nQ1,10\n,\nQ2,5")] {
|
||||
// A dropped empty row wider than the others (`,,`) adds no blank series, so the right axis keeps nothing.
|
||||
for (row, csv) in [("Wide", ",a\nQ1,10\n,,\nQ2,5"), ("One", ",a\nQ1,10\nQ2,5"), ("Blank", ",a\nQ1,10\n,\nQ2,5")] {
|
||||
s.execute("dataset.new", &json!({"name": row, "values": {name.clone(): csv}})).unwrap();
|
||||
s.execute("dataset.select", &json!({"name": row})).unwrap();
|
||||
s.execute("graph.setData", &json!({"id": other, "csv": csv})).unwrap();
|
||||
@@ -1655,6 +1656,8 @@ fn graph_variable_applies_csv_as_set_data() {
|
||||
assert_eq!(merged.right_series, set.right_series, "{row}");
|
||||
assert_eq!(merged.categories, set.categories, "{row}");
|
||||
assert_eq!(merged.cells(), set.cells(), "{row}");
|
||||
assert!(merged.right_series.is_empty(), "{row}");
|
||||
assert_eq!(merged.cells(), [[Some(10.0)], [Some(5.0)]], "{row}");
|
||||
}
|
||||
assert!(spec(&s, id).right_series.is_empty());
|
||||
assert_eq!(spec(&s, id).categories, ["Q1", "Q2"]);
|
||||
|
||||
Reference in New Issue
Block a user