From 070164b6bacbcad1424a4f43a97e14a627a4b597 Mon Sep 17 00:00:00 2001 From: Everett Pompeii Date: Thu, 26 Dec 2024 13:26:10 -0600 Subject: [PATCH] trim_decimal --- lib/bencher_comment/src/lib.rs | 8 ++++---- lib/bencher_plot/src/line.rs | 21 ++++++++++++++++----- lib/bencher_valid/src/units.rs | 8 ++++---- 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/bencher_comment/src/lib.rs b/lib/bencher_comment/src/lib.rs index 571d1fc05..be2aba099 100644 --- a/lib/bencher_comment/src/lib.rs +++ b/lib/bencher_comment/src/lib.rs @@ -779,7 +779,7 @@ fn value_cell( baseline: Option>, factor: OrderedFloat, ) -> String { - let mut cell = Units::format_number((value / factor).into()); + let mut cell = Units::format_float((value / factor).into()); if let Some(baseline) = baseline { let percent = if value.is_normal() && baseline.is_normal() { @@ -788,7 +788,7 @@ fn value_cell( 0.0.into() }; let plus = if percent > 0.0.into() { "+" } else { "" }; - let percent = Units::format_number(percent.into()); + let percent = Units::format_float(percent.into()); cell.push_str(&format!("
({plus}{percent}%)")); } @@ -861,8 +861,8 @@ fn limit_cell( percent: OrderedFloat, factor: OrderedFloat, ) -> String { - let mut cell = Units::format_number((limit / factor).into()); - let percent = Units::format_number(percent.into()); + let mut cell = Units::format_float((limit / factor).into()); + let percent = Units::format_float(percent.into()); cell.push_str(&format!("
({percent}%)")); cell } diff --git a/lib/bencher_plot/src/line.rs b/lib/bencher_plot/src/line.rs index 0bcb4a6b6..a7aa1cac9 100644 --- a/lib/bencher_plot/src/line.rs +++ b/lib/bencher_plot/src/line.rs @@ -117,7 +117,7 @@ impl LinePlot { .y_desc(&perf_data.y_desc) .y_labels(Y_LABELS) .y_label_style((FontFamily::Monospace, 12)) - .y_label_formatter(&|&y| Units::trim_number(y)) + .y_label_formatter(&|&y| Units::format_number(y, perf_data.trim_key_point_decimal())) .max_light_lines(4) .draw()?; @@ -330,8 +330,19 @@ impl PerfData { 0.0..0.0 } + fn key_points(&self) -> Vec { + RangedCoordf64::from(self.y_range()).key_points(Y_LABELS) + } + + fn trim_key_point_decimal(&self) -> bool { + !self + .key_points() + .iter() + .any(|y| !format!("{y:.2}").ends_with(".00")) + } + fn y_label_area_size(&self) -> Result { - let y_range = RangedCoordf64::from(self.y_range()).key_points(Y_LABELS); + let y_range = self.key_points(); let min = y_range.first().copied().unwrap_or_default(); let max = y_range.last().copied().unwrap_or_default(); let buffer = if max < 1.0 { @@ -341,12 +352,12 @@ impl PerfData { } else { 32 }; - let y_len = buffer + 6 * std::cmp::max(Self::float_len(min), Self::float_len(max)); + let y_len = buffer + 6 * std::cmp::max(self.float_len(min), self.float_len(max)); u32::try_from(y_len).map_err(Into::into) } - fn float_len(y: f64) -> usize { - Units::trim_number(y).len() + fn float_len(&self, y: f64) -> usize { + Units::format_number(y, self.trim_key_point_decimal()).len() } fn plot_box(&self) -> Result { diff --git a/lib/bencher_valid/src/units.rs b/lib/bencher_valid/src/units.rs index 720b6f4e6..743ec99b7 100644 --- a/lib/bencher_valid/src/units.rs +++ b/lib/bencher_valid/src/units.rs @@ -29,12 +29,12 @@ impl Units { self.scale.units(self.units.as_ref()) } - pub fn format_number(number: f64) -> String { - format_number(number, false) + pub fn format_float(number: f64) -> String { + Self::format_number(number, false) } - pub fn trim_number(number: f64) -> String { - format_number(number, true) + pub fn format_number(number: f64, trim_decimal: bool) -> String { + format_number(number, trim_decimal) } }