Repository navigation
feat(layout): say a chart's notices on export, and when its box is too small for it #3168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jgregoriorincon
merged 5 commits into
opengeos:main
from
jgregoriorincon:feat/3156-chart-export-notices
Nov 28, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5b9cb86
feat(layout): say a chart's notices on export, and when its box is to…
jgregoriorincon df94b39
fix(layout): say an exported chart's notices from the rows its page drew
jgregoriorincon af16589
fix(layout): the layout view says its charts' notices too
jgregoriorincon 9246f5e
fix(layout): warn of columns left under a point high
jgregoriorincon 1911cd1
fix(layout): name charts with the same notice together
jgregoriorincon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| //! A layout chart's notices on export, and in a box too small to draw it (#3156). | ||
|
|
||
| use super::*; | ||
| use rustgis_data::{Dataset, Field, FieldType, Geometry, GeometryType, Value as Data}; | ||
|
|
||
| const COLUMNS: &str = "the chart's labels and values leave its columns no height: it draws as a placeholder until its box is taller"; | ||
| const THIN: &str = "the chart is too small for its bars: some draw under 1 pt until its box is larger"; | ||
|
|
||
| /// A layout "Charts" over a map with the points layer "Shops": `region` (E, N, S), `kind` and `n`. | ||
| fn shops() -> Session { | ||
| let mut s = Session::new(sample::world_project()); | ||
| let mut ds = Dataset::new( | ||
| "Shops", | ||
| GeometryType::Point, | ||
| vec![Field::new("region", FieldType::Text), Field::new("kind", FieldType::Text), Field::new("n", FieldType::Double)], | ||
| ); | ||
| for (region, kind) in [("E", "city"), ("E", "town"), ("N", "town"), ("S", "town")] { | ||
| ds.push( | ||
| Some(Geometry::Point(rustgis_data::Coord::new(0.0, 0.0))), | ||
| vec![Data::Text(region.into()), Data::Text(kind.into()), Data::Float(1.0)], | ||
| ); | ||
| } | ||
| s.map_mut().add_dataset(ds); | ||
| s.run("layout.new", &json!({"name": "Charts"})).unwrap(); | ||
| s | ||
| } | ||
|
|
||
| /// Adds a chart of Shops summed by region, with `extra` keys; its element id. | ||
| fn chart(s: &mut Session, extra: Value) -> u64 { | ||
| let mut p = json!({"kind": "chart", "layer": "Shops", "field": "n", "group": "region"}); | ||
| if let (Some(p), Some(e)) = (p.as_object_mut(), extra.as_object()) { | ||
| p.extend(e.clone()); | ||
| } | ||
| s.run("layout.addElement", &p).unwrap()["element"].as_u64().unwrap() | ||
| } | ||
|
|
||
| /// What running `command` with `p` says. | ||
| fn said(s: &mut Session, command: &str, p: Value) -> Vec<String> { | ||
| let mark = s.notice_mark(); | ||
| s.run(command, &p).unwrap(); | ||
| s.notices_since(mark).iter().map(|n| n.text.clone()).collect() | ||
| } | ||
|
|
||
| /// What exporting the layout to a file of `ext` says of its charts. | ||
| fn exported(s: &mut Session, ext: &str) -> Vec<String> { | ||
| let dir = std::env::temp_dir().join(format!("rustgis-3156-{}-{:?}", std::process::id(), std::thread::current().id())); | ||
| std::fs::create_dir_all(&dir).unwrap(); | ||
| let path = dir.join(format!("page.{ext}")); | ||
| let mut all = said(s, "layout.export", json!({"path": path, "dpi": 36})); | ||
| std::fs::remove_dir_all(&dir).unwrap(); | ||
| all.retain(|t| t.contains("chart")); | ||
| all | ||
| } | ||
|
|
||
| /// What the export says of the chart `id`'s `note`. | ||
| fn of(s: &Session, id: u64, note: &str) -> String { | ||
| let name = &s.project.layouts[0].element(id as u32).unwrap().name; | ||
| format!("Chart \"{name}\" ({id}): {note}") | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_export_says_what_each_chart_leaves_out() { | ||
| let mut s = shops(); | ||
| let id = chart(&mut s, json!({"bars": 2})); | ||
| let want = | ||
| of(&s, id, "the chart draws 2 of 3 bars: 1 left out (\"bars\" draws up to 100, \"other\": true gathers the rest in an Other bar)"); | ||
| for ext in ["png", "pdf", "svg"] { | ||
| assert_eq!(exported(&mut s, ext), std::slice::from_ref(&want), "{ext}"); | ||
| } | ||
| // A project opened again says it too: nothing is kept from adding the chart. | ||
| let mut again = Session::new(serde_json::from_value(serde_json::to_value(&s.project).unwrap()).unwrap()); | ||
| assert_eq!(exported(&mut again, "png"), [want]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_export_reads_the_layer_as_it_is_now() { | ||
| let mut s = shops(); | ||
| let id = chart(&mut s, json!({})); | ||
| assert!(exported(&mut s, "png").is_empty(), "nothing lost, nothing said"); | ||
| let layer = s.map_mut().layers.iter_mut().find(|l| l.name == "Shops").unwrap(); | ||
| layer.dataset.push( | ||
| Some(Geometry::Point(rustgis_data::Coord::new(0.0, 0.0))), | ||
| vec![Data::Text("N".into()), Data::Text("town".into()), Data::Null], | ||
| ); | ||
| let want = of(&s, id, "1 feature of Shops with no number in n left out of the chart"); | ||
| assert_eq!(exported(&mut s, "png"), [want]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_map_series_export_says_a_pages_losses_once() { | ||
| let mut s = shops(); | ||
| s.run("layout.series", &json!({"layer": "Shops", "nameField": "region"})).unwrap(); | ||
| // On page 2 alone, East's city and town take different colours. | ||
| s.map_mut().layers.iter_mut().find(|l| l.name == "Shops").unwrap().symbol_by.fill = | ||
| Some("CASE WHEN @page_number = 2 AND kind = 'city' THEN '#ff0000' ELSE '#0000ff' END".into()); | ||
| let id = chart(&mut s, json!({"classes": true})); | ||
| let want = of(&s, id, "1 bar keeps the chart's bar colour: its features have several colours on the map"); | ||
| assert!(exported(&mut s, "png").is_empty(), "page 1 has one colour"); | ||
| assert_eq!(exported(&mut s, "pdf"), [want]); | ||
| // The same loss on every page is said once. | ||
| s.run("layout.update", &json!({"element": id, "classes": false, "bars": 2})).unwrap(); | ||
| let said = exported(&mut s, "pdf"); | ||
| assert_eq!(said.len(), 1, "{said:?}"); | ||
| assert!(said[0].contains("draws 2 of 3 bars"), "{said:?}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_export_names_charts_with_the_same_notice_in_one() { | ||
| let mut s = shops(); | ||
| let ids: Vec<_> = (0..12).map(|_| chart(&mut s, json!({"bars": 2}))).collect(); | ||
| let other = chart(&mut s, json!({"bars": 1})); | ||
| let said = exported(&mut s, "png"); | ||
| assert_eq!(said.len(), 2, "{said:?}"); | ||
| let named = |id: u64| format!("\"{}\" ({id})", s.project.layouts[0].element(id as u32).unwrap().name); | ||
| let listed: Vec<_> = ids[..10].iter().map(|id| named(*id)).collect(); | ||
| let want = format!("12 charts ({} and 2 more): the chart draws 2 of 3 bars", listed.join(", ")); | ||
| assert!(said[0].starts_with(&want), "{said:?}"); | ||
| assert!(said[1].starts_with(&format!("Chart {}: the chart draws 1 of 3 bars", named(other))), "{said:?}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn an_export_says_nothing_of_a_chart_it_does_not_draw() { | ||
| let mut s = shops(); | ||
| let id = chart(&mut s, json!({"bars": 2})); | ||
| s.run("layout.update", &json!({"element": id, "visible": false})).unwrap(); | ||
| assert!(exported(&mut s, "png").is_empty(), "hidden"); | ||
| s.run("layout.update", &json!({"element": id, "visible": true})).unwrap(); | ||
| s.run("layer.remove", &json!({"layer": "Shops"})).unwrap(); | ||
| assert!(exported(&mut s, "png").is_empty(), "its layer is gone"); | ||
| } | ||
|
|
||
| /// The assistant's view of a layout says what exporting it says, of the charts in the region it | ||
| /// shows alone when it shows one. | ||
| #[test] | ||
| fn a_view_of_the_layout_says_the_notices_of_the_charts_it_shows() { | ||
| let mut s = shops(); | ||
| let id = chart(&mut s, json!({"bars": 2, "x": 10, "y": 10, "w": 60, "h": 40})); | ||
| let mut viewed = |region| { | ||
| let mark = s.notice_mark(); | ||
| crate::cmd::layout::layout_png(&mut s, 0, 36.0, region).unwrap(); | ||
| s.notices_since(mark).iter().map(|n| n.text.clone()).filter(|t| t.contains("chart")).collect::<Vec<_>>() | ||
| }; | ||
| for region in [None, Some([0.0, 0.0, 40.0, 30.0])] { | ||
| let said = viewed(region); | ||
| assert!(said.len() == 1 && said[0].contains(&format!("({id}): the chart draws 2 of 3 bars")), "{region:?}: {said:?}"); | ||
| } | ||
| assert!(viewed(Some([150.0, 100.0, 40.0, 30.0])).is_empty(), "a region without it"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_chart_of_columns_without_height_says_so() { | ||
| let mut s = shops(); | ||
| // 10 mm high: its title, a line of labels and one of values leave the columns nothing. | ||
| let small = 10.0; | ||
| let mark = s.notice_mark(); | ||
| let id = chart(&mut s, json!({"orientation": "vertical", "values": true, "h": small})); | ||
| let added: Vec<_> = s.notices_since(mark).iter().map(|n| n.text.clone()).collect(); | ||
| assert!(added.iter().any(|t| t == COLUMNS), "added: {added:?}"); | ||
| let want = of(&s, id, COLUMNS); | ||
| assert_eq!(exported(&mut s, "png"), [want]); | ||
| let taller = said(&mut s, "layout.update", json!({"element": id, "h": 60})); | ||
| assert!(!taller.iter().any(|t| t == COLUMNS), "{taller:?}"); | ||
| let resized = said(&mut s, "layout.update", json!({"element": id, "h": small})); | ||
| assert!(resized.iter().any(|t| t == COLUMNS), "resized: {resized:?}"); | ||
| let moved = said(&mut s, "layout.update", json!({"element": id, "x": 20})); | ||
| assert!(!moved.iter().any(|t| t == COLUMNS), "moved: {moved:?}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_chart_with_bars_under_a_point_says_so() { | ||
| let mut s = shops(); | ||
| let id = chart(&mut s, json!({"h": 60, "w": 80})); | ||
| assert!(exported(&mut s, "png").is_empty(), "room enough"); | ||
| for (key, value) in [("h", 5.0), ("w", 1.0)] { | ||
| s.run("layout.update", &json!({"element": id, "h": 60, "w": 80})).unwrap(); | ||
| let resized = said(&mut s, "layout.update", json!({"element": id, key: value})); | ||
| assert!(resized.iter().any(|t| t == THIN), "{key}: {resized:?}"); | ||
| let want = of(&s, id, THIN); | ||
| assert_eq!(exported(&mut s, "svg"), [want], "{key}"); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.