Skip to content

Commit

Permalink
Ergonomic enhancement to plot hover label function
Browse files Browse the repository at this point in the history
  • Loading branch information
isegal committed Dec 7, 2021
1 parent 4f17c3f commit ea959d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
10 changes: 5 additions & 5 deletions egui/src/widgets/plot/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use epaint::Mesh;
use crate::util::float_ord::FloatOrd;
use crate::*;

use super::{CustomLabelFunc, PlotBounds, ScreenTransform};
use super::{CustomLabelFuncRef, PlotBounds, ScreenTransform};
use rect_elem::*;
use values::*;

Expand Down Expand Up @@ -66,7 +66,7 @@ pub(super) trait PlotItem {
elem: ClosestElem,
shapes: &mut Vec<Shape>,
plot: &PlotConfig<'_>,
custom_label_func: &CustomLabelFunc,
custom_label_func: &CustomLabelFuncRef,
) {
let points = match self.geometry() {
PlotGeometry::Points(points) => points,
Expand Down Expand Up @@ -1376,7 +1376,7 @@ impl PlotItem for BarChart {
elem: ClosestElem,
shapes: &mut Vec<Shape>,
plot: &PlotConfig<'_>,
_: &CustomLabelFunc,
_: &CustomLabelFuncRef,
) {
let bar = &self.bars[elem.index];

Expand Down Expand Up @@ -1518,7 +1518,7 @@ impl PlotItem for BoxPlot {
elem: ClosestElem,
shapes: &mut Vec<Shape>,
plot: &PlotConfig<'_>,
_: &CustomLabelFunc,
_: &CustomLabelFuncRef,
) {
let box_plot = &self.boxes[elem.index];

Expand Down Expand Up @@ -1637,7 +1637,7 @@ pub(super) fn rulers_at_value(
name: &str,
plot: &PlotConfig<'_>,
shapes: &mut Vec<Shape>,
custom_label_func: &CustomLabelFunc,
custom_label_func: &CustomLabelFuncRef,
) {
let line_color = rulers_color(plot.ui);
if plot.show_x {
Expand Down
18 changes: 11 additions & 7 deletions egui/src/widgets/plot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ mod items;
mod legend;
mod transform;

type CustomLabelFunc = Option<Box<dyn Fn(&str, &Value) -> String>>;
type CustomLabelFunc = dyn Fn(&str, &Value) -> String;
type CustomLabelFuncRef = Option<Box<CustomLabelFunc>>;

// ----------------------------------------------------------------------------

Expand Down Expand Up @@ -78,7 +79,7 @@ pub struct Plot {

show_x: bool,
show_y: bool,
custom_label_func: CustomLabelFunc,
custom_label_func: CustomLabelFuncRef,
legend_config: Option<Legend>,
show_background: bool,
show_axes: [bool; 2],
Expand Down Expand Up @@ -197,18 +198,21 @@ impl Plot {
/// });
/// let line = Line::new(Values::from_values_iter(sin));
/// Plot::new("my_plot").view_aspect(2.0)
/// .custom_label_func(Some(Box::new(|name, value| {
/// .custom_label_func(|name, value| {
/// if !name.is_empty() {
/// format!("{}: {:.*}%", name, 1, value.y).to_string()
/// } else {
/// "".to_string()
/// }
/// })))
/// })
/// .show(ui, |plot_ui| plot_ui.line(line));
/// # });
/// ```
pub fn custom_label_func(mut self, custom_lebel_func: CustomLabelFunc) -> Self {
self.custom_label_func = custom_lebel_func;
pub fn custom_label_func<F: 'static + Fn(&str, &Value) -> String>(
mut self,
custom_lebel_func: F,
) -> Self {
self.custom_label_func = Some(Box::new(custom_lebel_func));
self
}

Expand Down Expand Up @@ -645,7 +649,7 @@ struct PreparedPlot {
items: Vec<Box<dyn PlotItem>>,
show_x: bool,
show_y: bool,
custom_label_func: CustomLabelFunc,
custom_label_func: CustomLabelFuncRef,
show_axes: [bool; 2],
transform: ScreenTransform,
}
Expand Down

0 comments on commit ea959d1

Please sign in to comment.