Skip to content

Commit

Permalink
Increase precision in axis formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
YgorSouza committed Jun 1, 2024
1 parent 6233339 commit a128a7a
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions examples/plot_log_scale/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,16 @@ fn log_axis_spacer(input: GridInput) -> Vec<GridMark> {
}

fn log_axis_formatter(gm: GridMark, max_size: usize, _bounds: &RangeInclusive<f64>) -> String {
let precision = (-gm.value).clamp(1.0, 10.0) as usize;
let digits = (gm.value).clamp(0.0, 10.0) as usize;
let size = digits + precision + 1;
let min_precision = (-gm.value + 1.0).ceil().clamp(1.0, 10.0) as usize;
let digits = (gm.value).ceil().max(1.0) as usize;
let size = digits + min_precision + 1;
let value = 10.0f64.powf(gm.value);
if size < max_size {
let precision = max_size.saturating_sub(digits + 1);
format!("{value:.precision$}")
} else {
let precision = max_size.saturating_sub(size);
let exp_digits = (digits as f64).log10() as usize;
let precision = max_size.saturating_sub(exp_digits).saturating_sub(3);
format!("{value:.precision$e}")
}
}

0 comments on commit a128a7a

Please sign in to comment.