mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Ergonomic enhancement to plot hover label function
This commit is contained in:
@@ -7,7 +7,7 @@ use epaint::Mesh;
|
|||||||
use crate::util::float_ord::FloatOrd;
|
use crate::util::float_ord::FloatOrd;
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
use super::{CustomLabelFunc, PlotBounds, ScreenTransform};
|
use super::{CustomLabelFuncRef, PlotBounds, ScreenTransform};
|
||||||
use rect_elem::*;
|
use rect_elem::*;
|
||||||
use values::*;
|
use values::*;
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ pub(super) trait PlotItem {
|
|||||||
elem: ClosestElem,
|
elem: ClosestElem,
|
||||||
shapes: &mut Vec<Shape>,
|
shapes: &mut Vec<Shape>,
|
||||||
plot: &PlotConfig<'_>,
|
plot: &PlotConfig<'_>,
|
||||||
custom_label_func: &CustomLabelFunc,
|
custom_label_func: &CustomLabelFuncRef,
|
||||||
) {
|
) {
|
||||||
let points = match self.geometry() {
|
let points = match self.geometry() {
|
||||||
PlotGeometry::Points(points) => points,
|
PlotGeometry::Points(points) => points,
|
||||||
@@ -1376,7 +1376,7 @@ impl PlotItem for BarChart {
|
|||||||
elem: ClosestElem,
|
elem: ClosestElem,
|
||||||
shapes: &mut Vec<Shape>,
|
shapes: &mut Vec<Shape>,
|
||||||
plot: &PlotConfig<'_>,
|
plot: &PlotConfig<'_>,
|
||||||
_: &CustomLabelFunc,
|
_: &CustomLabelFuncRef,
|
||||||
) {
|
) {
|
||||||
let bar = &self.bars[elem.index];
|
let bar = &self.bars[elem.index];
|
||||||
|
|
||||||
@@ -1518,7 +1518,7 @@ impl PlotItem for BoxPlot {
|
|||||||
elem: ClosestElem,
|
elem: ClosestElem,
|
||||||
shapes: &mut Vec<Shape>,
|
shapes: &mut Vec<Shape>,
|
||||||
plot: &PlotConfig<'_>,
|
plot: &PlotConfig<'_>,
|
||||||
_: &CustomLabelFunc,
|
_: &CustomLabelFuncRef,
|
||||||
) {
|
) {
|
||||||
let box_plot = &self.boxes[elem.index];
|
let box_plot = &self.boxes[elem.index];
|
||||||
|
|
||||||
@@ -1637,7 +1637,7 @@ pub(super) fn rulers_at_value(
|
|||||||
name: &str,
|
name: &str,
|
||||||
plot: &PlotConfig<'_>,
|
plot: &PlotConfig<'_>,
|
||||||
shapes: &mut Vec<Shape>,
|
shapes: &mut Vec<Shape>,
|
||||||
custom_label_func: &CustomLabelFunc,
|
custom_label_func: &CustomLabelFuncRef,
|
||||||
) {
|
) {
|
||||||
let line_color = rulers_color(plot.ui);
|
let line_color = rulers_color(plot.ui);
|
||||||
if plot.show_x {
|
if plot.show_x {
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ mod items;
|
|||||||
mod legend;
|
mod legend;
|
||||||
mod transform;
|
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_x: bool,
|
||||||
show_y: bool,
|
show_y: bool,
|
||||||
custom_label_func: CustomLabelFunc,
|
custom_label_func: CustomLabelFuncRef,
|
||||||
legend_config: Option<Legend>,
|
legend_config: Option<Legend>,
|
||||||
show_background: bool,
|
show_background: bool,
|
||||||
show_axes: [bool; 2],
|
show_axes: [bool; 2],
|
||||||
@@ -197,18 +198,21 @@ impl Plot {
|
|||||||
/// });
|
/// });
|
||||||
/// let line = Line::new(Values::from_values_iter(sin));
|
/// let line = Line::new(Values::from_values_iter(sin));
|
||||||
/// Plot::new("my_plot").view_aspect(2.0)
|
/// 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() {
|
/// if !name.is_empty() {
|
||||||
/// format!("{}: {:.*}%", name, 1, value.y).to_string()
|
/// format!("{}: {:.*}%", name, 1, value.y).to_string()
|
||||||
/// } else {
|
/// } else {
|
||||||
/// "".to_string()
|
/// "".to_string()
|
||||||
/// }
|
/// }
|
||||||
/// })))
|
/// })
|
||||||
/// .show(ui, |plot_ui| plot_ui.line(line));
|
/// .show(ui, |plot_ui| plot_ui.line(line));
|
||||||
/// # });
|
/// # });
|
||||||
/// ```
|
/// ```
|
||||||
pub fn custom_label_func(mut self, custom_lebel_func: CustomLabelFunc) -> Self {
|
pub fn custom_label_func<F: 'static + Fn(&str, &Value) -> String>(
|
||||||
self.custom_label_func = custom_lebel_func;
|
mut self,
|
||||||
|
custom_lebel_func: F,
|
||||||
|
) -> Self {
|
||||||
|
self.custom_label_func = Some(Box::new(custom_lebel_func));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -645,7 +649,7 @@ struct PreparedPlot {
|
|||||||
items: Vec<Box<dyn PlotItem>>,
|
items: Vec<Box<dyn PlotItem>>,
|
||||||
show_x: bool,
|
show_x: bool,
|
||||||
show_y: bool,
|
show_y: bool,
|
||||||
custom_label_func: CustomLabelFunc,
|
custom_label_func: CustomLabelFuncRef,
|
||||||
show_axes: [bool; 2],
|
show_axes: [bool; 2],
|
||||||
transform: ScreenTransform,
|
transform: ScreenTransform,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user