Skip to content

Commit fdcebd5

Browse files
TommiKabelitzTommi Kabelitzemilk
authored
Add ability to specify color and font of axis tick labels for each axis (#155)
<!-- Please read the "Making a PR" section of [`CONTRIBUTING.md`](https://github.com/emilk/egui_plot/blob/master/CONTRIBUTING.md) before opening a Pull Request! * Keep your PR:s small and focused. * The PR title is what ends up in the changelog, so make it descriptive! * If applicable, add a screenshot or gif. * If it is a non-trivial addition, consider adding a demo f or a new example. * Do NOT open PR:s from your `master` or `main` branch, as that makes it hard for maintainers to test and add commits to your PR. * Remember to run `cargo fmt` and `cargo clippy`. * Open the PR as a draft until you have self-reviewed it and run `./scripts/check.sh`. * When you have addressed a PR comment, mark it as resolved. Please be patient! We will review your PR, but our time is limited! --> This allows specifying the color and font (through FontId) of the tick labels for each axis on a plot <img width="1329" height="867" alt="image" src="https://github.com/user-attachments/assets/a5e683e4-0163-40d4-8955-0ca106ec12d0" /> All feedback and criticism of the API and implementation are much appreciated. I will note, to ensure that the fading of the tick label color is respected, I did the following ``` let text_color = if let Some(color) = self.hints.tick_label_color { color.gamma_multiply(strength.sqrt()) } else { super::color_from_strength(ui, strength) }; ``` and I don't love the duplication of the gamma_multiply, I just didn't want to touch the color_from_strength function without the all clear. * Closes <#154> * [x] I have followed the instructions in the PR template --------- Co-authored-by: Tommi Kabelitz <tommi.kabelitz@priorianalytica.com> Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
1 parent e03309f commit fdcebd5

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

egui_plot/src/axis.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use std::fmt::Debug;
22
use std::ops::RangeInclusive;
33
use std::sync::Arc;
44

5+
use egui::Color32;
6+
use egui::FontId;
57
use egui::Pos2;
68
use egui::Rangef;
79
use egui::Rect;
@@ -21,7 +23,6 @@ use emath::remap;
2123

2224
use crate::bounds::PlotBounds;
2325
use crate::bounds::PlotPoint;
24-
use crate::colors;
2526
use crate::grid::GridMark;
2627
use crate::placement::HPlacement;
2728
use crate::placement::Placement;
@@ -62,6 +63,8 @@ pub struct AxisHints<'a> {
6263
pub(super) min_thickness: f32,
6364
pub(super) placement: Placement,
6465
pub(super) label_spacing: Rangef,
66+
pub(super) tick_label_color: Option<Color32>,
67+
pub(super) tick_label_font: Option<FontId>,
6568
}
6669

6770
impl<'a> AxisHints<'a> {
@@ -89,6 +92,8 @@ impl<'a> AxisHints<'a> {
8992
Axis::X => Rangef::new(60.0, 80.0), // labels can get pretty wide
9093
Axis::Y => Rangef::new(20.0, 30.0), // text isn't very high
9194
},
95+
tick_label_color: None,
96+
tick_label_font: None,
9297
}
9398
}
9499

@@ -157,6 +162,20 @@ impl<'a> AxisHints<'a> {
157162
self.label_spacing = range.into();
158163
self
159164
}
165+
166+
/// Set the color of the axis tick labels.
167+
#[inline]
168+
pub fn tick_label_color(mut self, color: impl Into<Color32>) -> Self {
169+
self.tick_label_color = Some(color.into());
170+
self
171+
}
172+
173+
/// Set the font of the axis tick labels.
174+
#[inline]
175+
pub fn tick_label_font(mut self, font: FontId) -> Self {
176+
self.tick_label_font = Some(font);
177+
self
178+
}
160179
}
161180

162181
#[derive(Clone)]
@@ -274,8 +293,15 @@ impl<'a> AxisWidget<'a> {
274293
// Fade in labels as they get further apart:
275294
let strength = remap_clamp(spacing_in_points, label_spacing, 0.0..=1.0);
276295

277-
let text_color = colors::color_from_strength(ui, strength);
278-
let galley = painter.layout_no_wrap(text, font_id.clone(), text_color);
296+
let text_color = if let Some(color) = self.hints.tick_label_color {
297+
color.gamma_multiply(strength.sqrt())
298+
} else {
299+
super::color_from_strength(ui, strength)
300+
};
301+
302+
let label_font_id = self.hints.tick_label_font.clone().unwrap_or_else(|| font_id.clone());
303+
304+
let galley = painter.layout_no_wrap(text, label_font_id, text_color);
279305
let galley_size = match axis {
280306
Axis::X => galley.size(),
281307
Axis::Y => galley.size() + 2.0 * SIDE_MARGIN * Vec2::X,

0 commit comments

Comments
 (0)