mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
Always draw axis labels at plot borders
This commit is contained in:
committed by
JohannesProgrammiert
parent
307565efa5
commit
607423f9eb
@@ -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));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user