Repository navigation
fix(layout): keep a colorbar's end labels and thin the rest evenly #3507
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:fix/3501-colorbar-even-labels
Dec 1, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9a6c4fd
fix(layout): keep a colorbar's end labels and thin the rest evenly
jgregoriorincon b4dda7e
docs(layout): say a colorbar's thinned labels run between the first a…
jgregoriorincon 8f5ec33
fix(layout): say the end a colorbar leaves out where its ends would t…
jgregoriorincon 4601eba
test(layout): refuse repeated colorbar label texts in the label tests…
jgregoriorincon 3669ef8
test(layout): size the touching-ends colorbar from its labels, and ch…
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,120 @@ | ||
| //! Which labels a colorbar keeps when they don't all fit (#3501). | ||
|
|
||
| use super::*; | ||
|
|
||
| const PT: f64 = 16.0; | ||
|
|
||
| /// A colorbar of `scale` in a `w` by `h` element at one pixel per point of its own: the indices, | ||
| /// low to high, of the labels it draws among all of its labels, how many it has, and how many | ||
| /// spilled. Panics if two drawn labels stand closer than the plan's gap, or two of the bar's labels | ||
| /// have the same text, which would not tell them apart. | ||
| fn kept(scale: &ColorScale, ticks: usize, orientation: Orientation, reverse: bool, w: f32, h: f32) -> (Vec<usize>, usize, usize) { | ||
| let bar = Bar { title: "", orientation, ticks, side: TickSide::End, reverse, comma: false, ink: INK, text_pt: PT, decimals: Some(2) }; | ||
| let plan = |w, h| colorbar_plan(Rect::from_xywh(0.0, 0.0, w, h).unwrap(), scale, &bar, 1.0); | ||
| let mut all: Vec<String> = plan(3000.0, 3000.0).labels.into_iter().map(|l| l.0).collect(); | ||
| if reverse != (orientation == Orientation::Vertical) { | ||
| all.reverse(); | ||
| } | ||
| assert_eq!(all.iter().collect::<std::collections::BTreeSet<_>>().len(), all.len(), "a repeated label: {all:?}"); | ||
| let drawn = plan(w, h); | ||
| let px = label_px(PT, 1.0); | ||
| let spans: Vec<(f32, f32)> = drawn | ||
| .labels | ||
| .iter() | ||
| .map(|(s, _, at, _)| { | ||
| let (middle, extent) = | ||
| if orientation == Orientation::Vertical { (at.1, px) } else { (at.0, text::measure(s, px / 1.33, false)) }; | ||
| (middle - extent / 2.0, middle + extent / 2.0) | ||
| }) | ||
| .collect(); | ||
| assert!(spans.windows(2).all(|p| p[0].1 + GAP_PX <= p[1].0), "{w}x{h}: labels touch: {spans:?}"); | ||
| let mut indices: Vec<usize> = drawn.labels.iter().map(|l| all.iter().position(|s| *s == l.0).unwrap()).collect(); | ||
| indices.sort_unstable(); | ||
| (indices, all.len(), drawn.spilled) | ||
| } | ||
|
|
||
| /// Every set of labels kept over elements from narrow (or short) to roomy where nothing is said to | ||
| /// be cut off; each one holds both ends and is evenly spaced between them. | ||
| fn outcomes(scale: &ColorScale, ticks: usize, orientation: Orientation, reverse: bool) -> std::collections::BTreeSet<Vec<usize>> { | ||
| let mut seen = std::collections::BTreeSet::new(); | ||
| for size in (20..900).step_by(2) { | ||
| let (w, h) = if orientation == Orientation::Vertical { (300.0, size as f32) } else { (size as f32, 80.0) }; | ||
| let (indices, n, spilled) = kept(scale, ticks, orientation, reverse, w, h); | ||
| if spilled > 0 || indices.is_empty() { | ||
| continue; | ||
| } | ||
| assert_eq!((indices.first(), indices.last()), (Some(&0), Some(&(n - 1))), "{w}x{h}: the ends: {indices:?}"); | ||
| let step = indices[1] - indices[0]; | ||
| assert!(indices.windows(2).all(|p| p[1] - p[0] == step), "{w}x{h}: evenly spaced: {indices:?}"); | ||
| seen.insert(indices); | ||
| } | ||
| seen | ||
| } | ||
|
|
||
| fn ramp() -> ColorScale { | ||
| let grey = Rgba::rgb(200, 200, 200); | ||
| ColorScale::Ramp { lo: 415.82, hi: 569.18, colors: vec![grey, grey] } | ||
| } | ||
|
|
||
| fn sets(of: &[&[usize]]) -> std::collections::BTreeSet<Vec<usize>> { | ||
| of.iter().map(|s| s.to_vec()).collect() | ||
| } | ||
|
|
||
| /// The case of the issue and its family: all five where they fit, then every second one, then | ||
| /// only the ends, never the ends' neighbours without the maximum. | ||
| #[test] | ||
| fn a_colorbar_keeps_its_ends_and_thins_the_rest_evenly() { | ||
| let expected = sets(&[&[0, 1, 2, 3, 4], &[0, 2, 4], &[0, 4]]); | ||
| for orientation in [Orientation::Horizontal, Orientation::Vertical] { | ||
| for reverse in [false, true] { | ||
| assert_eq!(outcomes(&ramp(), 5, orientation, reverse), expected, "{orientation:?}, reverse {reverse}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Five gaps have no even step between all of them and the ends alone. | ||
| #[test] | ||
| fn a_colorbar_with_a_prime_number_of_gaps_keeps_all_or_its_ends() { | ||
| for orientation in [Orientation::Horizontal, Orientation::Vertical] { | ||
| assert_eq!(outcomes(&ramp(), 6, orientation, false), sets(&[&[0, 1, 2, 3, 4, 5], &[0, 5]]), "{orientation:?}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_classified_or_unique_colorbar_thins_its_labels_the_same_way() { | ||
| let grey = Rgba::rgb(200, 200, 200); | ||
| let steps = ColorScale::Steps { edges: vec![415.82, 420.0, 500.0, 501.0, 569.18], colors: vec![grey; 4] }; | ||
| let values = ColorScale::Values([415.82, 420.0, 500.0, 501.0, 569.18].map(|v| (v, grey)).to_vec()); | ||
| let expected = sets(&[&[0, 1, 2, 3, 4], &[0, 2, 4], &[0, 4]]); | ||
| for scale in [&steps, &values] { | ||
| for orientation in [Orientation::Horizontal, Orientation::Vertical] { | ||
| assert_eq!(outcomes(scale, 5, orientation, false), expected, "{orientation:?}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_colorbar_of_one_value_labels_it_once() { | ||
| let grey = Rgba::rgb(200, 200, 200); | ||
| let flat = ColorScale::Ramp { lo: 7.0, hi: 7.0, colors: vec![grey] }; | ||
| assert_eq!(kept(&flat, 5, Orientation::Horizontal, false, 300.0, 80.0), (vec![0], 1, 0)); | ||
| } | ||
|
|
||
| /// Where even the first and last label would touch, only the one at the bar's start (left, or | ||
| /// top) is drawn, and the other end is said to be cut off. | ||
| #[test] | ||
| fn a_colorbar_whose_ends_would_touch_keeps_one_and_says_so() { | ||
| // Twice the wider end label: both just fit, a label's width apart. Measured, as fonts differ. | ||
| let end = |s: &str| text::measure(s, label_px(PT, 1.0) / 1.33, false); | ||
| let narrow = 2.0 * end("415.82").max(end("569.18")); | ||
| assert_eq!(kept(&ramp(), 5, Orientation::Horizontal, false, narrow, 80.0), (vec![0], 5, 1)); | ||
| assert_eq!(kept(&ramp(), 5, Orientation::Vertical, false, 300.0, 43.0), (vec![4], 5, 1)); | ||
| } | ||
|
|
||
| /// A label that spills out of the element is counted though it would also touch its neighbour. | ||
| #[test] | ||
| fn a_colorbar_counts_a_label_that_spills_and_would_touch() { | ||
| let grey = Rgba::rgb(200, 200, 200); | ||
| let values = ColorScale::Values(vec![(1.0, grey), (1e12, grey)]); | ||
| assert_eq!(kept(&values, 5, Orientation::Vertical, true, 80.0, 43.0), (vec![0], 2, 1)); | ||
| } | ||
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
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.