Fix plot formatter not taking closures (#3260)

and fix their their comments
This commit is contained in:
Andreas Reich 2023-08-15 19:46:04 +02:00 committed by GitHub
parent b896d641c5
commit b0735775f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 15 deletions

View File

@ -9,7 +9,7 @@ use crate::{Response, Sense, TextStyle, Ui, WidgetText};
use super::{transform::PlotTransform, GridMark}; use super::{transform::PlotTransform, GridMark};
pub(super) type AxisFormatterFn = fn(f64, usize, &RangeInclusive<f64>) -> String; pub(super) type AxisFormatterFn = dyn Fn(f64, usize, &RangeInclusive<f64>) -> String;
/// X or Y axis. /// X or Y axis.
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -81,7 +81,7 @@ impl From<VPlacement> for Placement {
#[derive(Clone)] #[derive(Clone)]
pub struct AxisHints { pub struct AxisHints {
pub(super) label: WidgetText, pub(super) label: WidgetText,
pub(super) formatter: AxisFormatterFn, pub(super) formatter: Arc<AxisFormatterFn>,
pub(super) digits: usize, pub(super) digits: usize,
pub(super) placement: Placement, pub(super) placement: Placement,
} }
@ -98,7 +98,7 @@ impl Default for AxisHints {
fn default() -> Self { fn default() -> Self {
Self { Self {
label: Default::default(), label: Default::default(),
formatter: Self::default_formatter, formatter: Arc::new(Self::default_formatter),
digits: 5, digits: 5,
placement: Placement::LeftBottom, placement: Placement::LeftBottom,
} }
@ -111,8 +111,11 @@ impl AxisHints {
/// The first parameter of `formatter` is the raw tick value as `f64`. /// The first parameter of `formatter` is the raw tick value as `f64`.
/// The second parameter is the maximum number of characters that fit into y-labels. /// The second parameter is the maximum number of characters that fit into y-labels.
/// The second parameter of `formatter` is the currently shown range on this axis. /// The second parameter of `formatter` is the currently shown range on this axis.
pub fn formatter(mut self, fmt: fn(f64, usize, &RangeInclusive<f64>) -> String) -> Self { pub fn formatter(
self.formatter = fmt; mut self,
fmt: impl Fn(f64, usize, &RangeInclusive<f64>) -> String + 'static,
) -> Self {
self.formatter = Arc::new(fmt);
self self
} }

View File

@ -613,24 +613,32 @@ impl Plot {
/// Specify custom formatter for ticks on the main X-axis. /// Specify custom formatter for ticks on the main X-axis.
/// ///
/// The first parameter of `fmt` is the raw tick value as `f64`. /// Arguments of `fmt`:
/// The second parameter is the maximum requested number of characters per tick label. /// * raw tick value as `f64`.
/// The second parameter of `fmt` is the currently shown range on this axis. /// * maximum requested number of characters per tick label.
pub fn x_axis_formatter(mut self, fmt: fn(f64, usize, &RangeInclusive<f64>) -> String) -> Self { /// * currently shown range on this axis.
pub fn x_axis_formatter(
mut self,
fmt: impl Fn(f64, usize, &RangeInclusive<f64>) -> String + 'static,
) -> Self {
if let Some(main) = self.x_axes.first_mut() { if let Some(main) = self.x_axes.first_mut() {
main.formatter = fmt; main.formatter = Arc::new(fmt);
} }
self self
} }
/// Specify custom formatter for ticks on the main Y-axis. /// Specify custom formatter for ticks on the main Y-axis.
/// ///
/// The first parameter of `formatter` is the raw tick value as `f64`. /// Arguments of `fmt`:
/// The second parameter is the maximum requested number of characters per tick label. /// * raw tick value as `f64`.
/// The second parameter of `formatter` is the currently shown range on this axis. /// * maximum requested number of characters per tick label.
pub fn y_axis_formatter(mut self, fmt: fn(f64, usize, &RangeInclusive<f64>) -> String) -> Self { /// * currently shown range on this axis.
pub fn y_axis_formatter(
mut self,
fmt: impl Fn(f64, usize, &RangeInclusive<f64>) -> String + 'static,
) -> Self {
if let Some(main) = self.y_axes.first_mut() { if let Some(main) = self.y_axes.first_mut() {
main.formatter = fmt; main.formatter = Arc::new(fmt);
} }
self self
} }