From 1d47b40074ec07d7d9d61bc0478fefee98362132 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 26 Jan 2024 11:56:35 +0100 Subject: [PATCH] Pass GridMark to the axis formatters --- crates/egui_demo_lib/src/demo/plot_demo.rs | 6 ++++-- crates/egui_plot/src/axis.rs | 14 ++++++++++---- crates/egui_plot/src/lib.rs | 8 ++++---- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/crates/egui_demo_lib/src/demo/plot_demo.rs b/crates/egui_demo_lib/src/demo/plot_demo.rs index fef3b4fc6dd..5b8d0b88c3f 100644 --- a/crates/egui_demo_lib/src/demo/plot_demo.rs +++ b/crates/egui_demo_lib/src/demo/plot_demo.rs @@ -530,7 +530,8 @@ impl CustomAxesDemo { 100.0 * y } - let x_fmt = |x, _digits, _range: &RangeInclusive| { + let x_fmt = |x: GridMark, _digits, _range: &RangeInclusive| { + let x = x.value; if x < 0.0 * MINS_PER_DAY || x >= 5.0 * MINS_PER_DAY { // No labels outside value bounds String::new() @@ -543,7 +544,8 @@ impl CustomAxesDemo { } }; - let y_fmt = |y, _digits, _range: &RangeInclusive| { + let y_fmt = |y: GridMark, _digits, _range: &RangeInclusive| { + let y = y.value; // Display only integer percentages if !is_approx_zero(y) && is_approx_integer(100.0 * y) { format!("{:.0}%", percent(y)) diff --git a/crates/egui_plot/src/axis.rs b/crates/egui_plot/src/axis.rs index cf704c7ae16..47e193e1d46 100644 --- a/crates/egui_plot/src/axis.rs +++ b/crates/egui_plot/src/axis.rs @@ -7,7 +7,7 @@ use crate::{Response, Sense, TextStyle, Ui, WidgetText}; use super::{transform::PlotTransform, GridMark}; -pub(super) type AxisFormatterFn = dyn Fn(f64, usize, &RangeInclusive) -> String; +pub(super) type AxisFormatterFn = dyn Fn(GridMark, usize, &RangeInclusive) -> String; /// X or Y axis. #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -111,13 +111,19 @@ impl AxisHints { /// The second parameter of `formatter` is the currently shown range on this axis. pub fn formatter( mut self, - fmt: impl Fn(f64, usize, &RangeInclusive) -> String + 'static, + fmt: impl Fn(GridMark, usize, &RangeInclusive) -> String + 'static, ) -> Self { self.formatter = Arc::new(fmt); self } - fn default_formatter(tick: f64, max_digits: usize, _range: &RangeInclusive) -> String { + fn default_formatter( + mark: GridMark, + max_digits: usize, + _range: &RangeInclusive, + ) -> String { + let tick = mark.value; + if tick.abs() > 10.0_f64.powf(max_digits as f64) { let tick_rounded = tick as isize; return format!("{tick_rounded:+e}"); @@ -258,7 +264,7 @@ impl AxisWidget { }; for step in self.steps.iter() { - let text = (self.hints.formatter)(step.value, self.hints.digits, &self.range); + let text = (self.hints.formatter)(*step, self.hints.digits, &self.range); if !text.is_empty() { const MIN_TEXT_SPACING: f32 = 20.0; const FULL_CONTRAST_SPACING: f32 = 40.0; diff --git a/crates/egui_plot/src/lib.rs b/crates/egui_plot/src/lib.rs index 5dc159f288f..9228a7f9605 100644 --- a/crates/egui_plot/src/lib.rs +++ b/crates/egui_plot/src/lib.rs @@ -624,12 +624,12 @@ impl Plot { /// Specify custom formatter for ticks on the main X-axis. /// /// Arguments of `fmt`: - /// * raw tick value as `f64`. + /// * the grid mark to format /// * maximum requested number of characters per tick label. /// * currently shown range on this axis. pub fn x_axis_formatter( mut self, - fmt: impl Fn(f64, usize, &RangeInclusive) -> String + 'static, + fmt: impl Fn(GridMark, usize, &RangeInclusive) -> String + 'static, ) -> Self { if let Some(main) = self.x_axes.first_mut() { main.formatter = Arc::new(fmt); @@ -640,12 +640,12 @@ impl Plot { /// Specify custom formatter for ticks on the main Y-axis. /// /// Arguments of `fmt`: - /// * raw tick value as `f64`. + /// * the grid mark to format /// * maximum requested number of characters per tick label. /// * currently shown range on this axis. pub fn y_axis_formatter( mut self, - fmt: impl Fn(f64, usize, &RangeInclusive) -> String + 'static, + fmt: impl Fn(GridMark, usize, &RangeInclusive) -> String + 'static, ) -> Self { if let Some(main) = self.y_axes.first_mut() { main.formatter = Arc::new(fmt);