Skip to content

Commit

Permalink
egui_plots: Fix the same plot tick label being painted multiple times (
Browse files Browse the repository at this point in the history
…#4307)

Usually this isn't visible (the same label being painted on top of
itself), but it will be visible if the user has a custom formatter (e.g.
`y_axis_formatter`) that choses a different format based on
`GridMark:step_size` (e.g. using fewer decimals for thicker ticks).
  • Loading branch information
emilk committed Apr 2, 2024
1 parent cadaa7a commit 5c8df02
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod memory;
mod plot_ui;
mod transform;

use std::{ops::RangeInclusive, sync::Arc};
use std::{cmp::Ordering, ops::RangeInclusive, sync::Arc};

use egui::ahash::HashMap;
use egui::*;
Expand Down Expand Up @@ -1714,9 +1714,31 @@ fn generate_marks(step_sizes: [f64; 3], bounds: (f64, f64)) -> Vec<GridMark> {
fill_marks_between(&mut steps, step_sizes[0], bounds);
fill_marks_between(&mut steps, step_sizes[1], bounds);
fill_marks_between(&mut steps, step_sizes[2], bounds);

// Remove duplicates:
// This can happen because we have overlapping steps, e.g.:
// step_size[0] = 10 => [-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]
// step_size[1] = 100 => [ 0, 100 ]
// step_size[2] = 1000 => [ 0 ]

steps.sort_by(|a, b| match cmp_f64(a.value, b.value) {
// Keep the largest step size when we dedup later
Ordering::Equal => cmp_f64(b.step_size, a.step_size),

ord => ord,
});
steps.dedup_by(|a, b| a.value == b.value);

steps
}

fn cmp_f64(a: f64, b: f64) -> Ordering {
match a.partial_cmp(&b) {
Some(ord) => ord,
None => a.is_nan().cmp(&b.is_nan()),
}
}

/// Fill in all values between [min, max] which are a multiple of `step_size`
fn fill_marks_between(out: &mut Vec<GridMark>, step_size: f64, (min, max): (f64, f64)) {
debug_assert!(max > min);
Expand Down

0 comments on commit 5c8df02

Please sign in to comment.