From ea959d15e7e72b918e247306aa469ecb2757ac60 Mon Sep 17 00:00:00 2001 From: Ivgeni Segal Date: Mon, 6 Dec 2021 22:17:40 -0800 Subject: [PATCH] Ergonomic enhancement to plot hover label function --- egui/src/widgets/plot/items/mod.rs | 10 +++++----- egui/src/widgets/plot/mod.rs | 18 +++++++++++------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/egui/src/widgets/plot/items/mod.rs b/egui/src/widgets/plot/items/mod.rs index a5b62e976f21..0e703788e533 100644 --- a/egui/src/widgets/plot/items/mod.rs +++ b/egui/src/widgets/plot/items/mod.rs @@ -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::*; @@ -66,7 +66,7 @@ pub(super) trait PlotItem { elem: ClosestElem, shapes: &mut Vec, plot: &PlotConfig<'_>, - custom_label_func: &CustomLabelFunc, + custom_label_func: &CustomLabelFuncRef, ) { let points = match self.geometry() { PlotGeometry::Points(points) => points, @@ -1376,7 +1376,7 @@ impl PlotItem for BarChart { elem: ClosestElem, shapes: &mut Vec, plot: &PlotConfig<'_>, - _: &CustomLabelFunc, + _: &CustomLabelFuncRef, ) { let bar = &self.bars[elem.index]; @@ -1518,7 +1518,7 @@ impl PlotItem for BoxPlot { elem: ClosestElem, shapes: &mut Vec, plot: &PlotConfig<'_>, - _: &CustomLabelFunc, + _: &CustomLabelFuncRef, ) { let box_plot = &self.boxes[elem.index]; @@ -1637,7 +1637,7 @@ pub(super) fn rulers_at_value( name: &str, plot: &PlotConfig<'_>, shapes: &mut Vec, - custom_label_func: &CustomLabelFunc, + custom_label_func: &CustomLabelFuncRef, ) { let line_color = rulers_color(plot.ui); if plot.show_x { diff --git a/egui/src/widgets/plot/mod.rs b/egui/src/widgets/plot/mod.rs index 47eca2ea1f65..9e69e496ea6f 100644 --- a/egui/src/widgets/plot/mod.rs +++ b/egui/src/widgets/plot/mod.rs @@ -18,7 +18,8 @@ mod items; mod legend; mod transform; -type CustomLabelFunc = Option String>>; +type CustomLabelFunc = dyn Fn(&str, &Value) -> String; +type CustomLabelFuncRef = Option>; // ---------------------------------------------------------------------------- @@ -78,7 +79,7 @@ pub struct Plot { show_x: bool, show_y: bool, - custom_label_func: CustomLabelFunc, + custom_label_func: CustomLabelFuncRef, legend_config: Option, show_background: bool, show_axes: [bool; 2], @@ -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 String>( + mut self, + custom_lebel_func: F, + ) -> Self { + self.custom_label_func = Some(Box::new(custom_lebel_func)); self } @@ -645,7 +649,7 @@ struct PreparedPlot { items: Vec>, show_x: bool, show_y: bool, - custom_label_func: CustomLabelFunc, + custom_label_func: CustomLabelFuncRef, show_axes: [bool; 2], transform: ScreenTransform, }