Add ability to specify color and font of axis tick labels for each axis#155
Add ability to specify color and font of axis tick labels for each axis#155TommiKabelitz wants to merge 5 commits intoemilk:mainfrom
Conversation
egui_plot/src/axis.rs
Outdated
| pub(super) placement: Placement, | ||
| pub(super) label_spacing: Rangef, | ||
| pub(super) tick_label_color: Option<egui::Color32>, | ||
| pub(super) tick_label_font: Option<egui::FontId>, |
There was a problem hiding this comment.
import egui::FontId directly
egui_plot/src/axis.rs
Outdated
| pub(super) min_thickness: f32, | ||
| pub(super) placement: Placement, | ||
| pub(super) label_spacing: Rangef, | ||
| pub(super) tick_label_color: Option<egui::Color32>, |
There was a problem hiding this comment.
import egui::Color32 directly
| Axis::X => Rangef::new(60.0, 80.0), // labels can get pretty wide | ||
| Axis::Y => Rangef::new(20.0, 30.0), // text isn't very high | ||
| }, | ||
| tick_label_color: None, |
There was a problem hiding this comment.
whats the default?
maybe set it to a default which we already use
There was a problem hiding this comment.
else use Default::default()
There was a problem hiding this comment.
Currently the default is whatever is in ui.visuals().text_color() when the plot is actually rendered because this function
/// Determine a color from a 0-1 strength value.
pub fn color_from_strength(ui: &Ui, strength: f32) -> Color32 {
let base_color = ui.visuals().text_color();
base_color.gamma_multiply(strength.sqrt())
}is called to determine the color when the plot is shown.
I am still just calling this function when color is None so the behavior is not changing. I could potentially move the default to the instantiation of AxisHints, but that would change the behavior in cases where ui.visuals().text_color() is changed between creation of the axes and showing the plot.
egui_plot/src/axis.rs
Outdated
|
|
||
| /// Set the font of the axis tick labels. | ||
| /// | ||
| /// To change the color of the tick labels see [`Self::tick_label_color`]. |
There was a problem hiding this comment.
this comment is not necessary for this func
egui_plot/src/axis.rs
Outdated
| /// As labels get close, they will fade in color until they become invisible. See | ||
| /// [`Self::label_spacing`]. | ||
| /// | ||
| /// To change the font of the tick labels see [`Self::tick_label_font`]. |
There was a problem hiding this comment.
this comment is not necessary for this func
| let label_font_id = self | ||
| .hints | ||
| .tick_label_font | ||
| .clone() |
There was a problem hiding this comment.
2x clones?
Is there something else possible?
There was a problem hiding this comment.
I could be mistaken, but I don't think so. Cloning FontId is pretty cheap as it is just an f32 and the FontFamily enum which is an Arc.
I think you need to clone in both cases as layout_no_wrap expects an owned FontId. When the font has not been specified we use the global one so you need to clone. When it is specified, you have to clone as we only have a reference to self and so cannot move it out of the shared reference.
The implications of changing layout_no_wrap to accept an owned value look to be significant at a glance.
This allows specifying the color and font (through FontId) of the tick labels for each axis on a plot

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
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.