use crate::bounds::PlotBounds;
use crate::bounds::PlotPoint;
type CoordinatesFormatterFn<'a> = dyn Fn(&PlotPoint, &PlotBounds) -> String + 'a;
pub struct CoordinatesFormatter<'a> {
function: Box<CoordinatesFormatterFn<'a>>,
}
impl<'a> CoordinatesFormatter<'a> {
pub fn new(function: impl Fn(&PlotPoint, &PlotBounds) -> String + 'a) -> Self {
Self {
function: Box::new(function),
}
}
pub fn with_decimals(num_decimals: usize) -> Self {
Self {
function: Box::new(move |value, _| format!("x: {:.d$}\ny: {:.d$}", value.x, value.y, d = num_decimals)),
}
}
pub(crate) fn format(&self, value: &PlotPoint, bounds: &PlotBounds) -> String {
(self.function)(value, bounds)
}
}
impl Default for CoordinatesFormatter<'_> {
fn default() -> Self {
Self::with_decimals(3)
}
}