Skip to content

Commit

Permalink
trim_decimal
Browse files Browse the repository at this point in the history
  • Loading branch information
epompeii committed Dec 26, 2024
1 parent beca0d0 commit 070164b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
8 changes: 4 additions & 4 deletions lib/bencher_comment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ fn value_cell(
baseline: Option<OrderedFloat<f64>>,
factor: OrderedFloat<f64>,
) -> 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() {
Expand All @@ -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!("<br />({plus}{percent}%)"));
}

Expand Down Expand Up @@ -861,8 +861,8 @@ fn limit_cell(
percent: OrderedFloat<f64>,
factor: OrderedFloat<f64>,
) -> 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!("<br />({percent}%)"));
cell
}
Expand Down
21 changes: 16 additions & 5 deletions lib/bencher_plot/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand Down Expand Up @@ -330,8 +330,19 @@ impl PerfData {
0.0..0.0
}

fn key_points(&self) -> Vec<f64> {
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<u32, PlotError> {
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 {
Expand All @@ -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<PlotBox, PlotError> {
Expand Down
8 changes: 4 additions & 4 deletions lib/bencher_valid/src/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 070164b

Please sign in to comment.