Skip to content

Commit

Permalink
Pass GridMark to the axis formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 26, 2024
1 parent 6b0782c commit 1d47b40
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
6 changes: 4 additions & 2 deletions crates/egui_demo_lib/src/demo/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,8 @@ impl CustomAxesDemo {
100.0 * y
}

let x_fmt = |x, _digits, _range: &RangeInclusive<f64>| {
let x_fmt = |x: GridMark, _digits, _range: &RangeInclusive<f64>| {
let x = x.value;
if x < 0.0 * MINS_PER_DAY || x >= 5.0 * MINS_PER_DAY {
// No labels outside value bounds
String::new()
Expand All @@ -543,7 +544,8 @@ impl CustomAxesDemo {
}
};

let y_fmt = |y, _digits, _range: &RangeInclusive<f64>| {
let y_fmt = |y: GridMark, _digits, _range: &RangeInclusive<f64>| {
let y = y.value;
// Display only integer percentages
if !is_approx_zero(y) && is_approx_integer(100.0 * y) {
format!("{:.0}%", percent(y))
Expand Down
14 changes: 10 additions & 4 deletions crates/egui_plot/src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>) -> String;
pub(super) type AxisFormatterFn = dyn Fn(GridMark, usize, &RangeInclusive<f64>) -> String;

/// X or Y axis.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -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<f64>) -> String + 'static,
fmt: impl Fn(GridMark, usize, &RangeInclusive<f64>) -> String + 'static,
) -> Self {
self.formatter = Arc::new(fmt);
self
}

fn default_formatter(tick: f64, max_digits: usize, _range: &RangeInclusive<f64>) -> String {
fn default_formatter(
mark: GridMark,
max_digits: usize,
_range: &RangeInclusive<f64>,
) -> 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}");
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions crates/egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>) -> String + 'static,
fmt: impl Fn(GridMark, usize, &RangeInclusive<f64>) -> String + 'static,
) -> Self {
if let Some(main) = self.x_axes.first_mut() {
main.formatter = Arc::new(fmt);
Expand All @@ -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<f64>) -> String + 'static,
fmt: impl Fn(GridMark, usize, &RangeInclusive<f64>) -> String + 'static,
) -> Self {
if let Some(main) = self.y_axes.first_mut() {
main.formatter = Arc::new(fmt);
Expand Down

0 comments on commit 1d47b40

Please sign in to comment.