@@ -45,7 +45,7 @@ use legend::LegendWidget;
4545type LabelFormatterFn < ' a > = dyn Fn ( & str , & PlotPoint ) -> String + ' a ;
4646pub type LabelFormatter < ' a > = Option < Box < LabelFormatterFn < ' a > > > ;
4747
48- type NewLabelFormatterFn < ' a > = dyn Fn ( Option < ( & str , usize ) > , & PlotPoint ) -> Option < String > + ' a ;
48+ type NewLabelFormatterFn < ' a > = dyn Fn ( & HoverPosition < ' _ > ) -> Option < String > + ' a ;
4949pub type NewLabelFormatter < ' a > = Option < Box < NewLabelFormatterFn < ' a > > > ;
5050
5151type GridSpacerFn < ' a > = dyn Fn ( GridInput ) -> Vec < GridMark > + ' a ;
@@ -95,6 +95,23 @@ pub enum Cursor {
9595 Vertical { x : f64 } ,
9696}
9797
98+ /// Indicates the position of the cursor in a plot for hover purposes.
99+ #[ derive( Copy , Clone , PartialEq ) ]
100+ pub enum HoverPosition < ' a > {
101+ NearDataPoint {
102+ /// The name of the plot whose data point is nearest to the cursor
103+ plot_name : & ' a str ,
104+ /// The position of the nearest data point
105+ position : PlotPoint ,
106+ /// The index of the nearest data point in its plot
107+ index : usize ,
108+ } ,
109+ Elsewhere {
110+ /// The position in the plot over which the cursor hovers
111+ position : PlotPoint ,
112+ } ,
113+ }
114+
98115/// Contains the cursors drawn for a plot widget in a single frame.
99116#[ derive( PartialEq , Clone ) ]
100117struct PlotFrameCursors {
@@ -430,8 +447,15 @@ impl<'a> Plot<'a> {
430447 label_formatter : impl Fn ( & str , & PlotPoint ) -> String + ' a ,
431448 ) -> Self {
432449 let inner_box = Box :: new ( label_formatter) ;
433- self . label_formatter = Some ( Box :: new ( move |option, point| {
434- Some ( inner_box ( option. map_or ( "" , |( name, _) | name) , point) )
450+ self . label_formatter = Some ( Box :: new ( move |position| {
451+ Some ( match position {
452+ HoverPosition :: NearDataPoint {
453+ plot_name,
454+ position,
455+ index : _,
456+ } => inner_box ( plot_name, & position) ,
457+ HoverPosition :: Elsewhere { position : _ } => "" . to_owned ( ) ,
458+ } )
435459 } ) ) ;
436460 self
437461 }
@@ -449,15 +473,18 @@ impl<'a> Plot<'a> {
449473 /// }).collect();
450474 /// let line = Line::new("sin", sin);
451475 /// Plot::new("my_plot").view_aspect(2.0)
452- /// .enumerated_label_formatter(|nearest_point, value| {
453- /// nearest_point.map(|(name, _index)| format!("{}: {:.*}%", name, 1, value.y))
476+ /// .enumerated_label_formatter(|context| {
477+ /// match context {
478+ /// Some((name, index)) => format!("{}: {:.*}%", name, 1, index),
479+ /// Elsewhere { ... } => None,
480+ /// }
454481 /// })
455482 /// .show(ui, |plot_ui| plot_ui.line(line));
456483 /// # });
457484 /// ```
458485 pub fn enumerated_label_formatter (
459486 mut self ,
460- label_formatter : impl Fn ( Option < ( & str , usize ) > , & PlotPoint ) -> Option < String > + ' a ,
487+ label_formatter : impl Fn ( & HoverPosition < ' _ > ) -> Option < String > + ' a ,
461488 ) -> Self {
462489 self . label_formatter = Some ( Box :: new ( label_formatter) ) ;
463490 self
0 commit comments