From 607423f9ebce1bf44093b903428a7030d56b1309 Mon Sep 17 00:00:00 2001 From: Johannes Schiffer Date: Fri, 11 Nov 2022 10:01:04 +0100 Subject: [PATCH] Always draw axis labels at plot borders --- crates/egui/src/widgets/plot/mod.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/egui/src/widgets/plot/mod.rs b/crates/egui/src/widgets/plot/mod.rs index c5db91992..79b3a126d 100644 --- a/crates/egui/src/widgets/plot/mod.rs +++ b/crates/egui/src/widgets/plot/mod.rs @@ -1421,6 +1421,7 @@ impl PreparedPlot { cursors } + // `axis`=0 means x-axis, `axis`=1 means y-axis. fn paint_axis( &self, ui: &Ui, @@ -1430,7 +1431,6 @@ impl PreparedPlot { sharp_grid_lines: bool, ) { #![allow(clippy::collapsible_else_if)] - let Self { transform, axis_formatters, @@ -1527,12 +1527,12 @@ impl PreparedPlot { )); } + // --- axis labels const MIN_TEXT_SPACING: f32 = 40.0; if spacing_in_points > MIN_TEXT_SPACING { let text_strength = remap_clamp(spacing_in_points, MIN_TEXT_SPACING..=150.0, 0.0..=1.0); let color = color_from_contrast(ui, text_strength); - let text: String = if let Some(formatter) = axis_formatters[axis].as_deref() { formatter(value_main, &axis_range) } else { @@ -1547,11 +1547,13 @@ impl PreparedPlot { let galley = ui.painter().layout_no_wrap(text, font_id.clone(), color); let mut text_pos = pos_in_gui + vec2(1.0, -galley.size().y); - - // Make sure we see the labels, even if the axis is off-screen: - text_pos[1 - axis] = text_pos[1 - axis] - .at_most(transform.frame().max[1 - axis] - galley.size()[1 - axis] - 2.0) - .at_least(transform.frame().min[1 - axis] + 1.0); + // move to border of plot frame + if axis == 0 { + text_pos.y = transform.frame().max[1] - galley.size()[1] - 2.0; + } + if axis == 1 { + text_pos.x = transform.frame().min[0] + 1.0; + } shapes.push((Shape::galley(text_pos, galley), text_strength)); }