Skip to main content

ggplot_rs/scale/
util.rs

1/// Round step size to a "nice" value (1, 2, 5, 10, 20, 50, ...).
2pub fn nice_step(raw: f64) -> f64 {
3    let magnitude = 10f64.powf(raw.abs().log10().floor());
4    let fraction = raw / magnitude;
5
6    let nice = if fraction <= 1.5 {
7        1.0
8    } else if fraction <= 3.5 {
9        2.0
10    } else if fraction <= 7.5 {
11        5.0
12    } else {
13        10.0
14    };
15
16    nice * magnitude
17}
18
19/// Format a number nicely, removing trailing zeros.
20pub fn format_number(v: f64) -> String {
21    if v == v.round() && v.abs() < 1e10 {
22        format!("{}", v as i64)
23    } else {
24        let s = format!("{:.2}", v);
25        s.trim_end_matches('0').trim_end_matches('.').to_string()
26    }
27}