Skip to content

Commit

Permalink
Add barplot axis labels and increas plot width to 80 characters (#128)
Browse files Browse the repository at this point in the history
* add barplot axis labels

* cargo fmt
  • Loading branch information
code-inflation authored Aug 25, 2024
1 parent 95b7eb2 commit 6885281
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/boxplot.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
use log;
const PLOT_WIDTH: usize = 40;
use std::fmt::Write;

const PLOT_WIDTH: usize = 80;

fn generate_axis_labels(minima: f64, maxima: f64) -> String {
let mut labels = String::new();
write!(labels, "{:<10.2}", minima).unwrap();
write!(
labels,
"{:^width$.2}",
(minima + maxima) / 2.0,
width = PLOT_WIDTH - 20
)
.unwrap();
write!(labels, "{:>10.2}", maxima).unwrap();
labels
}

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

let axis_labels = generate_axis_labels(minima, maxima);
plot.push('\n');
plot.push_str(&axis_labels);

log::debug!("fn input: {minima}, {q1}, {median}, {q3}, {maxima}");
log::debug!("quartiles: {quartile_0}, {quartile_1}, {quartile_2}, {quartile_3}");
log::debug!("value range: {value_range}");
Expand Down

0 comments on commit 6885281

Please sign in to comment.