Browse Source

Ergonomic enhancement to plot hover label function

pull/934/head
Ivgeni Segal 3 years ago
parent
commit
ea959d15e7
  1. 10
      egui/src/widgets/plot/items/mod.rs
  2. 18
      egui/src/widgets/plot/mod.rs

10
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<Shape>,
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<Shape>,
plot: &PlotConfig<'_>,
_: &CustomLabelFunc,
_: &CustomLabelFuncRef,
) {
let bar = &self.bars[elem.index];
@ -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];
@ -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 {

18
egui/src/widgets/plot/mod.rs

@ -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>>;
// ----------------------------------------------------------------------------
@ -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],
@ -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
}
@ -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,
}

Loading…
Cancel
Save