From 367bad82222e6962207f1c907f1746a7ccaeb2b9 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 26 Jan 2024 13:03:24 +0100 Subject: [PATCH] Clean up plot demo slightly --- crates/egui_demo_lib/src/demo/plot_demo.rs | 28 ++++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/crates/egui_demo_lib/src/demo/plot_demo.rs b/crates/egui_demo_lib/src/demo/plot_demo.rs index 05dc5921474..edd5215bfb5 100644 --- a/crates/egui_demo_lib/src/demo/plot_demo.rs +++ b/crates/egui_demo_lib/src/demo/plot_demo.rs @@ -530,25 +530,27 @@ impl CustomAxesDemo { 100.0 * y } - 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 { + let time_formatter = |mark: GridMark, _digits, _range: &RangeInclusive| { + let minutes = mark.value; + if minutes < 0.0 || 5.0 * MINS_PER_DAY <= minutes { // No labels outside value bounds String::new() - } else if is_approx_integer(x / MINS_PER_DAY) { + } else if is_approx_integer(minutes / MINS_PER_DAY) { // Days - format!("Day {}", day(x)) + format!("Day {}", day(minutes)) } else { // Hours and minutes - format!("{h}:{m:02}", h = hour(x), m = minute(x)) + format!("{h}:{m:02}", h = hour(minutes), m = minute(minutes)) } }; - 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)) + let percentage_formatter = |mark: GridMark, _digits, _range: &RangeInclusive| { + let percent = 100.0 * mark.value; + if is_approx_zero(percent) { + String::new() // skip zero + } else if is_approx_integer(percent) { + // Display only integer percentages + format!("{percent:.0}%") } else { String::new() } @@ -567,13 +569,13 @@ impl CustomAxesDemo { ui.label("Zoom in on the X-axis to see hours and minutes"); let x_axes = vec![ - AxisHints::new_x().label("Time").formatter(x_fmt), + AxisHints::new_x().label("Time").formatter(time_formatter), AxisHints::new_x().label("Value"), ]; let y_axes = vec![ AxisHints::new_y() .label("Percent") - .formatter(y_fmt) + .formatter(percentage_formatter) .max_digits(4), AxisHints::new_y() .label("Absolute")