Skip to content

Commit

Permalink
Implement custom ruler color for Plot (#47)
Browse files Browse the repository at this point in the history
Implement custom ruler color for Plot which is exposed through the new
`custom_ruler_color` method. The entire thing is only a few lines.

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
gweisert and emilk authored Dec 17, 2024
1 parent 64acd9d commit 7b9a4df
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion egui_plot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub struct Plot<'a> {
x_axes: Vec<AxisHints<'a>>, // default x axes
y_axes: Vec<AxisHints<'a>>, // default y axes
legend_config: Option<Legend>,
cursor_color: Option<Color32>,
show_background: bool,
show_axes: Vec2b,

Expand Down Expand Up @@ -227,6 +228,7 @@ impl<'a> Plot<'a> {
x_axes: vec![AxisHints::new(Axis::X)],
y_axes: vec![AxisHints::new(Axis::Y)],
legend_config: None,
cursor_color: None,
show_background: true,
show_axes: true.into(),

Expand Down Expand Up @@ -713,6 +715,15 @@ impl<'a> Plot<'a> {
self
}

/// Set custom cursor color.
///
/// You may set the color to [`Color32::TRANSPARENT`] to hide the cursors.
#[inline]
pub fn cursor_color(mut self, color: Color32) -> Self {
self.cursor_color = Some(color);
self
}

/// Interact with and add items to the plot and finally draw it.
pub fn show<R>(
self,
Expand Down Expand Up @@ -754,6 +765,7 @@ impl<'a> Plot<'a> {
x_axes,
y_axes,
legend_config,
cursor_color,
reset,
show_background,
show_axes,
Expand Down Expand Up @@ -1182,6 +1194,7 @@ impl<'a> Plot<'a> {
draw_cursor_x: linked_cursors.as_ref().map_or(false, |group| group.1.x),
draw_cursor_y: linked_cursors.as_ref().map_or(false, |group| group.1.y),
draw_cursors,
cursor_color,
grid_spacers,
sharp_grid_lines,
clamp_grid,
Expand Down Expand Up @@ -1472,6 +1485,7 @@ struct PreparedPlot<'a> {
draw_cursor_x: bool,
draw_cursor_y: bool,
draw_cursors: Vec<Cursor>,
cursor_color: Option<Color32>,

sharp_grid_lines: bool,
clamp_grid: bool,
Expand Down Expand Up @@ -1513,7 +1527,7 @@ impl<'a> PreparedPlot<'a> {
};

// Draw cursors
let line_color = rulers_color(ui);
let line_color = self.cursor_color.unwrap_or_else(|| rulers_color(ui));

let mut draw_cursor = |cursors: &Vec<Cursor>, always| {
for &cursor in cursors {
Expand Down

0 comments on commit 7b9a4df

Please sign in to comment.