-
Notifications
You must be signed in to change notification settings - Fork 90
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 2 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 |
|---|---|---|
|
|
@@ -107,6 +107,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<egui::Color32>, | ||
| pub(super) tick_label_font: Option<egui::FontId>, | ||
|
||
| } | ||
|
|
||
| impl<'a> AxisHints<'a> { | ||
|
|
@@ -134,6 +136,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, | ||
|
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. whats the default?
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. else use Default::default()
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. Currently the default is whatever is in /// 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
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. works for me |
||
| tick_label_font: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -200,6 +204,27 @@ impl<'a> AxisHints<'a> { | |
| self.label_spacing = range.into(); | ||
| self | ||
| } | ||
|
|
||
| /// Set the color of the axis tick labels. | ||
| /// | ||
| /// 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`]. | ||
|
||
| #[inline] | ||
| pub fn tick_label_color(mut self, color: impl Into<egui::Color32>) -> Self { | ||
| self.tick_label_color = Some(color.into()); | ||
| self | ||
| } | ||
|
|
||
| /// Set the font of the axis tick labels. | ||
| /// | ||
| /// To change the color of the tick labels see [`Self::tick_label_color`]. | ||
|
||
| #[inline] | ||
| pub fn tick_label_font(mut self, font: egui::FontId) -> Self { | ||
| self.tick_label_font = Some(font); | ||
| self | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
|
|
@@ -319,8 +344,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 = super::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.
import egui::Color32 directly