-
Notifications
You must be signed in to change notification settings - Fork 89
Add ability to specify color and font of axis tick labels for each axis #155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7b09ce3
c290139
7d0ff18
79eb72d
dd6f178
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ use std::fmt::Debug; | |
| use std::ops::RangeInclusive; | ||
| use std::sync::Arc; | ||
|
|
||
| use egui::Color32; | ||
| use egui::FontId; | ||
| use egui::Pos2; | ||
| use egui::Rangef; | ||
| use egui::Rect; | ||
|
|
@@ -62,6 +64,8 @@ pub struct AxisHints<'a> { | |
| pub(super) min_thickness: f32, | ||
| pub(super) placement: Placement, | ||
| pub(super) label_spacing: Rangef, | ||
| pub(super) tick_label_color: Option<Color32>, | ||
| pub(super) tick_label_font: Option<FontId>, | ||
| } | ||
|
|
||
| impl<'a> AxisHints<'a> { | ||
|
|
@@ -89,6 +93,8 @@ impl<'a> AxisHints<'a> { | |
| 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, | ||
| tick_label_font: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -157,6 +163,20 @@ impl<'a> AxisHints<'a> { | |
| self.label_spacing = range.into(); | ||
| self | ||
| } | ||
|
|
||
| /// Set the color of the axis tick labels. | ||
| #[inline] | ||
| pub fn tick_label_color(mut self, color: impl Into<Color32>) -> Self { | ||
| self.tick_label_color = Some(color.into()); | ||
| self | ||
| } | ||
|
|
||
| /// Set the font of the axis tick labels. | ||
| #[inline] | ||
| pub fn tick_label_font(mut self, font: FontId) -> Self { | ||
| self.tick_label_font = Some(font); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
|
|
@@ -274,8 +294,19 @@ impl<'a> AxisWidget<'a> { | |
| // Fade in labels as they get further apart: | ||
| let strength = remap_clamp(spacing_in_points, label_spacing, 0.0..=1.0); | ||
|
|
||
| let text_color = colors::color_from_strength(ui, strength); | ||
| let galley = painter.layout_no_wrap(text, font_id.clone(), text_color); | ||
| let text_color = if let Some(color) = self.hints.tick_label_color { | ||
| color.gamma_multiply(strength.sqrt()) | ||
| } else { | ||
| super::color_from_strength(ui, strength) | ||
| }; | ||
|
|
||
| let label_font_id = self | ||
| .hints | ||
| .tick_label_font | ||
| .clone() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2x clones?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could be mistaken, but I don't think so. Cloning I think you need to clone in both cases as The implications of changing |
||
| .unwrap_or(font_id.clone()); | ||
|
|
||
| let galley = painter.layout_no_wrap(text, label_font_id, text_color); | ||
| let galley_size = match axis { | ||
| Axis::X => galley.size(), | ||
| Axis::Y => galley.size() + 2.0 * SIDE_MARGIN * Vec2::X, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whats the default?
maybe set it to a default which we already use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else use Default::default()
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently the default is whatever is in
ui.visuals().text_color()when the plot is actually rendered because this functionis called to determine the color when the plot is shown.
I am still just calling this function when color is
Noneso the behavior is not changing. I could potentially move the default to the instantiation ofAxisHints, but that would change the behavior in cases whereui.visuals().text_color()is changed between creation of the axes and showing the plot.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
works for me