mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 23:00:04 -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
|
cursors
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// `axis`=0 means x-axis, `axis`=1 means y-axis.
|
||||||
fn paint_axis(
|
fn paint_axis(
|
||||||
&self,
|
&self,
|
||||||
ui: &Ui,
|
ui: &Ui,
|
||||||
@@ -1430,7 +1431,6 @@ impl PreparedPlot {
|
|||||||
sharp_grid_lines: bool,
|
sharp_grid_lines: bool,
|
||||||
) {
|
) {
|
||||||
#![allow(clippy::collapsible_else_if)]
|
#![allow(clippy::collapsible_else_if)]
|
||||||
|
|
||||||
let Self {
|
let Self {
|
||||||
transform,
|
transform,
|
||||||
axis_formatters,
|
axis_formatters,
|
||||||
@@ -1527,12 +1527,12 @@ impl PreparedPlot {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- axis labels
|
||||||
const MIN_TEXT_SPACING: f32 = 40.0;
|
const MIN_TEXT_SPACING: f32 = 40.0;
|
||||||
if spacing_in_points > MIN_TEXT_SPACING {
|
if spacing_in_points > MIN_TEXT_SPACING {
|
||||||
let text_strength =
|
let text_strength =
|
||||||
remap_clamp(spacing_in_points, MIN_TEXT_SPACING..=150.0, 0.0..=1.0);
|
remap_clamp(spacing_in_points, MIN_TEXT_SPACING..=150.0, 0.0..=1.0);
|
||||||
let color = color_from_contrast(ui, text_strength);
|
let color = color_from_contrast(ui, text_strength);
|
||||||
|
|
||||||
let text: String = if let Some(formatter) = axis_formatters[axis].as_deref() {
|
let text: String = if let Some(formatter) = axis_formatters[axis].as_deref() {
|
||||||
formatter(value_main, &axis_range)
|
formatter(value_main, &axis_range)
|
||||||
} else {
|
} else {
|
||||||
@@ -1547,11 +1547,13 @@ impl PreparedPlot {
|
|||||||
let galley = ui.painter().layout_no_wrap(text, font_id.clone(), color);
|
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);
|
let mut text_pos = pos_in_gui + vec2(1.0, -galley.size().y);
|
||||||
|
// move to border of plot frame
|
||||||
// Make sure we see the labels, even if the axis is off-screen:
|
if axis == 0 {
|
||||||
text_pos[1 - axis] = text_pos[1 - axis]
|
text_pos.y = transform.frame().max[1] - galley.size()[1] - 2.0;
|
||||||
.at_most(transform.frame().max[1 - axis] - galley.size()[1 - axis] - 2.0)
|
}
|
||||||
.at_least(transform.frame().min[1 - axis] + 1.0);
|
if axis == 1 {
|
||||||
|
text_pos.x = transform.frame().min[0] + 1.0;
|
||||||
|
}
|
||||||
|
|
||||||
shapes.push((Shape::galley(text_pos, galley), text_strength));
|
shapes.push((Shape::galley(text_pos, galley), text_strength));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user