Graphs: CSV graph data keeps the series labels given, as Graph Data does

Graph data applied as CSV kept a blank series label for each column after
the last label given, because to_csv writes one for every series without a
label. A graph data file named in an Import Data Sets % column therefore
gave the graph ["A", ""] where Graph Data's Import Data and Apply give it
["A"]. The CSV branch of apply_data now drops trailing blank labels, as
graph_data_from_grid does; blank labels between given ones stay.
This commit is contained in:
dotCooCoo
2026-11-15 08:28:07 -08:00
parent 06e212e1bb
commit 3c0d32c1c3
2 changed files with 33 additions and 0 deletions
+5
View File
@@ -245,6 +245,11 @@ pub(super) fn apply_data(g: &mut GraphSpec, p: &Value, cmd: &str) -> Result<bool
if let Some(csv) = csv {
let (s, c, r) = parse_csv(csv);
g.series = s;
// Series labels up to the last one given, as Graph Data's sheet keeps them
// ([`graph_data_from_grid`]): `to_csv` writes a blank label for each series without one.
while g.series.last().is_some_and(String::is_empty) {
g.series.pop();
}
g.categories = c;
g.set_cells(r);
changed = true;
+28
View File
@@ -2178,6 +2178,34 @@ fn datasets_csv_reads_graph_data_files_in_percent_columns() {
assert!(matches!(&value, Some(vectorcraft_doc::DataValue::Graph(c)) if c.contains("North") && c.contains("Feb")), "{value:?}");
}
/// A graph data file whose rows hold more values than its first row has series labels gives the
/// bound graph the series labels that Graph Data's Import Data and Apply give it: up to the last one
/// given, with no blank label for the series after it.
#[test]
fn datasets_csv_graph_data_file_keeps_the_given_series_labels() {
let dir = Folder::new("vars-csv-graph-wide");
let wide: &[u8] = b"\tA\t\nX\t1\t2\n";
write(&dir.file("wide.txt"), wide);
let path = dir.file("data.csv");
write(&path, b"Name,%Chart\na,wide.txt\n");
let mut s = session();
let p = json!({"type": "column", "x": 10, "y": 10, "width": 100, "height": 80, "csv": ",B\nY,1"});
let g = NodeId(s.execute("graph.create", &p).unwrap()["id"].as_u64().unwrap());
s.execute("dataset.import", &json!({"path": path})).unwrap();
s.execute("variable.bind", &json!({"variable": "Chart", "ids": [g.0]})).unwrap();
s.execute("dataset.select", &json!({"name": "Data Set 1"})).unwrap();
let data = crate::cmd::graph_data_from_grid(&crate::cmd::graph_grid_from_text(&crate::cmd::graph_data_text(wide)));
let mut probe = session();
let id = NodeId(probe.execute("graph.create", &p).unwrap()["id"].as_u64().unwrap());
let mut set = data.clone();
set["id"] = json!(id.0);
probe.execute("graph.setData", &set).unwrap();
assert_eq!(graph_data(&mut s, g), graph_data(&mut probe, id));
assert_eq!(graph_data(&mut s, g), json!({"series": ["A"], "categories": ["X"], "rows": [[1.0, 2.0]]}));
}
/// A `%` column's file that is missing, a folder, larger than 1 MB, or gives a graph value longer
/// than 256 KB, a relative path with no
/// folder to take it from, a `base` that isn't a folder path, and a `%` header whose name is a