Skip to content

Commit 6885281

Browse files
Add barplot axis labels and increas plot width to 80 characters (#128)
* add barplot axis labels * cargo fmt
1 parent 95b7eb2 commit 6885281

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/boxplot.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
use log;
2-
const PLOT_WIDTH: usize = 40;
2+
use std::fmt::Write;
3+
4+
const PLOT_WIDTH: usize = 80;
5+
6+
fn generate_axis_labels(minima: f64, maxima: f64) -> String {
7+
let mut labels = String::new();
8+
write!(labels, "{:<10.2}", minima).unwrap();
9+
write!(
10+
labels,
11+
"{:^width$.2}",
12+
(minima + maxima) / 2.0,
13+
width = PLOT_WIDTH - 20
14+
)
15+
.unwrap();
16+
write!(labels, "{:>10.2}", maxima).unwrap();
17+
labels
18+
}
319

420
pub(crate) fn render_plot(minima: f64, q1: f64, median: f64, q3: f64, maxima: f64) -> String {
5-
// TODO print axis labels
621
let value_range = maxima - minima;
722
let quartile_0 = q1 - minima;
823
let quartile_1 = median - q1;
@@ -20,6 +35,10 @@ pub(crate) fn render_plot(minima: f64, q1: f64, median: f64, q3: f64, maxima: f6
2035
plot.push_str("-".repeat((quartile_3 * scale_factor) as usize).as_str());
2136
plot.push('|');
2237

38+
let axis_labels = generate_axis_labels(minima, maxima);
39+
plot.push('\n');
40+
plot.push_str(&axis_labels);
41+
2342
log::debug!("fn input: {minima}, {q1}, {median}, {q3}, {maxima}");
2443
log::debug!("quartiles: {quartile_0}, {quartile_1}, {quartile_2}, {quartile_3}");
2544
log::debug!("value range: {value_range}");

0 commit comments

Comments
 (0)