use emath::NumExt as _;
use crate::bounds::PlotPoint;
pub fn format_number(number: f64, num_decimals: usize) -> String {
let is_integral = number as i64 as f64 == number;
if is_integral {
format!("{number:.0}")
} else {
format!("{:.*}", num_decimals.at_least(1), number)
}
}
type LabelFormatterFn<'a> = dyn Fn(&HoverPosition<'_>) -> Option<String> + 'a;
pub type LabelFormatter<'a> = Box<LabelFormatterFn<'a>>;
#[derive(Copy, Clone, PartialEq)]
pub enum HoverPosition<'a> {
NearDataPoint {
plot_name: &'a str,
position: PlotPoint,
index: usize,
},
Elsewhere {
position: PlotPoint,
},
}
#[expect(clippy::unnecessary_wraps)]
pub fn default_label_formatter(pos: &HoverPosition<'_>) -> Option<String> {
Some(match pos {
HoverPosition::NearDataPoint {
plot_name,
position,
index: _,
} => format!("{}\nx = {:.3}\ny = {:.3}", plot_name, position.x, position.y),
HoverPosition::Elsewhere { position } => format!("x = {:.3}\ny = {:.3}", position.x, position.y),
})
}