Skip to content

Commit

Permalink
axis: fix label thickness
Browse files Browse the repository at this point in the history
  • Loading branch information
jordens committed Jan 15, 2025
1 parent 80e2199 commit 3674f1e
Showing 1 changed file with 47 additions and 68 deletions.
115 changes: 47 additions & 68 deletions egui_plot/src/axis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 3674f1e

Please sign in to comment.