Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

axis: fix label thickness #68

Merged
merged 3 commits into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 50 additions & 68 deletions egui_plot/src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>) -> String + 'a;

/// X or Y axis.
Expand Down Expand Up @@ -254,80 +257,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 tick_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.0 + AXIS_LABEL_GAP),
}
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 * AXIS_LABEL_GAP,
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 * AXIS_LABEL_GAP,
}
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 * (1.0 - AXIS_LABEL_GAP),
y: pos.y + galley.size().x * 0.5,
}
},
};

ui.painter()
.add(TextShape::new(text_pos, galley, text_color).with_angle(angle));

text_thickness
}
},
};
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,
};

(response, thickness + text_thickness)
ui.painter()
.add(TextShape::new(text_pos, galley, ui.visuals().text_color()).with_angle(angle));

(response, tick_labels_thickness + axis_label_thickness)
}

/// Add tick labels to the axis. Returns the thickness of the axis.
Expand Down
Loading