From 3674f1e38cab0b942f105bb17cd8927b85a263a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 15 Jan 2025 17:14:51 +0100 Subject: [PATCH 1/3] axis: fix label thickness --- egui_plot/src/axis.rs | 115 +++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 68 deletions(-) diff --git a/egui_plot/src/axis.rs b/egui_plot/src/axis.rs index c1e4392..b095f66 100644 --- a/egui_plot/src/axis.rs +++ b/egui_plot/src/axis.rs @@ -254,80 +254,59 @@ impl<'a> AxisWidget<'a> { let Some(transform) = self.transform else { return (response, 0.0); }; - let thickness = self.add_tick_labels(ui, transform, axis); - - let visuals = ui.style().visuals.clone(); - - let text_thickness = { - let text = self.hints.label; - let galley = text.into_galley( - ui, - Some(TextWrapMode::Extend), - f32::INFINITY, - TextStyle::Body, - ); - let text_color = visuals - .override_text_color - .unwrap_or_else(|| ui.visuals().text_color()); - let angle: f32 = match axis { - Axis::X => 0.0, - Axis::Y => -std::f32::consts::TAU * 0.25, - }; - // select text_pos and angle depending on placement and orientation of widget - let (text_pos, text_thickness) = match self.hints.placement { - Placement::LeftBottom => match axis { - Axis::X => { - let pos = response.rect.center_bottom(); - ( - Pos2 { - x: pos.x - galley.size().x / 2.0, - y: pos.y - galley.size().y * 1.25, - }, - 0.0, - ) + let labels_thickness = self.add_tick_labels(ui, transform, axis); + + let galley = self.hints.label.into_galley( + ui, + Some(TextWrapMode::Extend), + f32::INFINITY, + TextStyle::Body, + ); + + let text_pos = match self.hints.placement { + Placement::LeftBottom => match axis { + Axis::X => { + let pos = response.rect.center_bottom(); + Pos2 { + x: pos.x - galley.size().x * 0.5, + y: pos.y - galley.size().y * 1.25, } - Axis::Y => { - let pos = response.rect.left_center(); - ( - Pos2 { - x: pos.x, - y: pos.y + galley.size().x / 2.0, - }, - galley.size().x * 0.5, - ) + } + Axis::Y => { + let pos = response.rect.left_center(); + Pos2 { + x: pos.x - galley.size().y * 0.25, + y: pos.y + galley.size().x * 0.5, } - }, - Placement::RightTop => match axis { - Axis::X => { - let pos = response.rect.center_top(); - ( - Pos2 { - x: pos.x - galley.size().x / 2.0, - y: pos.y + galley.size().y * 0.25, - }, - 0.0, - ) + } + }, + Placement::RightTop => match axis { + Axis::X => { + let pos = response.rect.center_top(); + Pos2 { + x: pos.x - galley.size().x * 0.5, + y: pos.y + galley.size().y * 0.25, } - Axis::Y => { - let pos = response.rect.right_center(); - ( - Pos2 { - x: pos.x - galley.size().y * 1.5, - y: pos.y + galley.size().x / 2.0, - }, - galley.size().x * 0.75, - ) + } + Axis::Y => { + let pos = response.rect.right_center(); + Pos2 { + x: pos.x - galley.size().y * 0.75, + y: pos.y + galley.size().x * 0.5, } - }, - }; - - ui.painter() - .add(TextShape::new(text_pos, galley, text_color).with_angle(angle)); - - text_thickness + } + }, + }; + let text_thickness = galley.size().y; + let angle = match axis { + Axis::X => 0.0, + Axis::Y => -std::f32::consts::FRAC_PI_2, }; - (response, thickness + text_thickness) + ui.painter() + .add(TextShape::new(text_pos, galley, ui.visuals().text_color()).with_angle(angle)); + + (response, labels_thickness + text_thickness) } /// Add tick labels to the axis. Returns the thickness of the axis. From f7b5a97fa23d3bbea65c2dcab91f079f619985c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Wed, 15 Jan 2025 22:10:45 +0100 Subject: [PATCH 2/3] axis: explicit gap between axis label and tick labels --- egui_plot/src/axis.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/egui_plot/src/axis.rs b/egui_plot/src/axis.rs index b095f66..737a909 100644 --- a/egui_plot/src/axis.rs +++ b/egui_plot/src/axis.rs @@ -254,7 +254,7 @@ impl<'a> AxisWidget<'a> { let Some(transform) = self.transform else { return (response, 0.0); }; - let labels_thickness = self.add_tick_labels(ui, transform, axis); + let tick_labels_thickness = self.add_tick_labels(ui, transform, axis); let galley = self.hints.label.into_galley( ui, @@ -263,19 +263,22 @@ impl<'a> AxisWidget<'a> { TextStyle::Body, ); + // Gap between tick labels and axis label in units of the axis label height + const GAP: f32 = 0.25; + let text_pos = match self.hints.placement { Placement::LeftBottom => match axis { Axis::X => { let pos = response.rect.center_bottom(); Pos2 { x: pos.x - galley.size().x * 0.5, - y: pos.y - galley.size().y * 1.25, + y: pos.y - galley.size().y * (1.0 + GAP), } } Axis::Y => { let pos = response.rect.left_center(); Pos2 { - x: pos.x - galley.size().y * 0.25, + x: pos.x - galley.size().y * GAP, y: pos.y + galley.size().x * 0.5, } } @@ -285,19 +288,19 @@ impl<'a> AxisWidget<'a> { let pos = response.rect.center_top(); Pos2 { x: pos.x - galley.size().x * 0.5, - y: pos.y + galley.size().y * 0.25, + y: pos.y + galley.size().y * GAP, } } Axis::Y => { let pos = response.rect.right_center(); Pos2 { - x: pos.x - galley.size().y * 0.75, + x: pos.x - galley.size().y * (1.0 - GAP), y: pos.y + galley.size().x * 0.5, } } }, }; - let text_thickness = galley.size().y; + let axis_label_thickness = galley.size().y * (1.0 + GAP); let angle = match axis { Axis::X => 0.0, Axis::Y => -std::f32::consts::FRAC_PI_2, @@ -306,7 +309,7 @@ impl<'a> AxisWidget<'a> { ui.painter() .add(TextShape::new(text_pos, galley, ui.visuals().text_color()).with_angle(angle)); - (response, labels_thickness + text_thickness) + (response, tick_labels_thickness + axis_label_thickness) } /// Add tick labels to the axis. Returns the thickness of the axis. From 7d53ab17e391e7790331396c20c2233f49786be9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Fri, 24 Jan 2025 17:59:30 +0100 Subject: [PATCH 3/3] axis: rename/move AXIS_LABEL_GAP --- egui_plot/src/axis.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/egui_plot/src/axis.rs b/egui_plot/src/axis.rs index 737a909..d1a47f9 100644 --- a/egui_plot/src/axis.rs +++ b/egui_plot/src/axis.rs @@ -8,6 +8,9 @@ use egui::{ use super::{transform::PlotTransform, GridMark}; +// Gap between tick labels and axis label in units of the axis label height +const AXIS_LABEL_GAP: f32 = 0.25; + pub(super) type AxisFormatterFn<'a> = dyn Fn(GridMark, &RangeInclusive) -> String + 'a; /// X or Y axis. @@ -263,22 +266,19 @@ impl<'a> AxisWidget<'a> { TextStyle::Body, ); - // Gap between tick labels and axis label in units of the axis label height - const GAP: f32 = 0.25; - let text_pos = match self.hints.placement { Placement::LeftBottom => match axis { Axis::X => { let pos = response.rect.center_bottom(); Pos2 { x: pos.x - galley.size().x * 0.5, - y: pos.y - galley.size().y * (1.0 + GAP), + y: pos.y - galley.size().y * (1.0 + AXIS_LABEL_GAP), } } Axis::Y => { let pos = response.rect.left_center(); Pos2 { - x: pos.x - galley.size().y * GAP, + x: pos.x - galley.size().y * AXIS_LABEL_GAP, y: pos.y + galley.size().x * 0.5, } } @@ -288,19 +288,19 @@ impl<'a> AxisWidget<'a> { let pos = response.rect.center_top(); Pos2 { x: pos.x - galley.size().x * 0.5, - y: pos.y + galley.size().y * GAP, + y: pos.y + galley.size().y * AXIS_LABEL_GAP, } } Axis::Y => { let pos = response.rect.right_center(); Pos2 { - x: pos.x - galley.size().y * (1.0 - GAP), + x: pos.x - galley.size().y * (1.0 - AXIS_LABEL_GAP), y: pos.y + galley.size().x * 0.5, } } }, }; - let axis_label_thickness = galley.size().y * (1.0 + GAP); + let axis_label_thickness = galley.size().y * (1.0 + AXIS_LABEL_GAP); let angle = match axis { Axis::X => 0.0, Axis::Y => -std::f32::consts::FRAC_PI_2,