diff --git a/lib/bencher_plot/src/line.rs b/lib/bencher_plot/src/line.rs index 2d1042492..0bcb4a6b6 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| PerfData::y_label_fmt(y)) + .y_label_formatter(&|&y| Units::trim_number(y)) .max_light_lines(4) .draw()?; @@ -345,59 +345,8 @@ impl PerfData { u32::try_from(y_len).map_err(Into::into) } - #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] - fn y_label_fmt(y: f64) -> String { - if y < 1.0 { - Self::decimal_format(y) - } else { - Self::comma_format(y as u64) - } - } - - #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] fn float_len(y: f64) -> usize { - if y < 1.0 { - Self::decimal_format(y).len() - } else { - Self::comma_format(y as u64).len() - } - } - - fn decimal_format(y: f64) -> String { - const ZERO: char = '0'; - - let y_str = y.to_string(); - let mut y_chars = String::with_capacity(y_str.len()); - let mut zero_count = 0; - for (index, c) in y_str.chars().enumerate() { - if index < 2 { - y_chars.push(c); - } else if zero_count == 4 { - if index == 6 { - y_chars.push(ZERO); - } - break; - } else if c == ZERO { - zero_count += 1; - } else { - for _ in 0..zero_count { - y_chars.push(ZERO); - } - zero_count = 0; - y_chars.push(c); - } - } - y_chars - } - - fn comma_format(y: u64) -> String { - y.to_string() - .as_bytes() - .rchunks(3) - .rev() - .filter_map(|thousand| std::str::from_utf8(thousand).ok()) - .collect::>() - .join(",") + Units::trim_number(y).len() } fn plot_box(&self) -> Result { diff --git a/lib/bencher_valid/src/units.rs b/lib/bencher_valid/src/units.rs index 016c65133..720b6f4e6 100644 --- a/lib/bencher_valid/src/units.rs +++ b/lib/bencher_valid/src/units.rs @@ -30,7 +30,11 @@ impl Units { } pub fn format_number(number: f64) -> String { - format_number(number) + format_number(number, false) + } + + pub fn trim_number(number: f64) -> String { + format_number(number, true) } } @@ -236,7 +240,7 @@ enum Position { Decimal, } -fn format_number(number: f64) -> String { +fn format_number(number: f64, trim_decimal: bool) -> String { let mut number_str = String::new(); let mut position = Position::Decimal; for c in format!("{:.2}", number.abs()).chars().rev() { @@ -261,5 +265,15 @@ fn format_number(number: f64) -> String { if number < 0.0 { number_str.push('-'); } - number_str.chars().rev().collect() + if trim_decimal && number_str.starts_with("00.") { + number_str + .chars() + .collect::>() + .into_iter() + .skip(3) + .rev() + .collect() + } else { + number_str.chars().rev().collect() + } } diff --git a/services/console/src/chunks/docs-reference/changelog/en/changelog.mdx b/services/console/src/chunks/docs-reference/changelog/en/changelog.mdx index 96b9e7a7b..04280ffd2 100644 --- a/services/console/src/chunks/docs-reference/changelog/en/changelog.mdx +++ b/services/console/src/chunks/docs-reference/changelog/en/changelog.mdx @@ -1,3 +1,6 @@ +## Pending `v0.4.33` +- Fix plot image y-axis labels + ## `v0.4.32` - Fix benchmark result units in PR comments (Thank you [@dklassic](https://github.com/dklassic))