Skip to content

Commit

Permalink
Clean up plot demo slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jan 26, 2024
1 parent 7d5f080 commit 367bad8
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions crates/egui_demo_lib/src/demo/plot_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,25 +530,27 @@ impl CustomAxesDemo {
100.0 * y
}

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 {
let time_formatter = |mark: GridMark, _digits, _range: &RangeInclusive<f64>| {
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<f64>| {
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<f64>| {
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()
}
Expand All @@ -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")
Expand Down

0 comments on commit 367bad8

Please sign in to comment.