#[path = "common/mod.rs"]
mod common;
use common::{Rng, help, run};
use crossterm::event::KeyCode;
use ratatui::{
layout::{Constraint, Layout},
widgets::Block,
};
use std::time::Duration;
use tui_chart::{BoxOrientation, BoxPlot, Theme};
struct State {
theme: Theme,
rng: Rng,
data: Vec<Vec<f64>>,
orientation: BoxOrientation,
outliers: bool,
mean: bool,
notched: bool,
jitter: bool,
}
fn sample(rng: &mut Rng) -> Vec<Vec<f64>> {
(0..4)
.map(|g| {
(0..80)
.map(|_| 40.0 + g as f64 * 8.0 + rng.gauss() * (6.0 + g as f64 * 2.0))
.collect()
})
.collect()
}
fn main() -> anyhow::Result<()> {
let mut rng = Rng::new(11);
let data = sample(&mut rng);
run(
State {
theme: Theme::default(),
rng,
data,
orientation: BoxOrientation::Vertical,
outliers: true,
mean: true,
notched: false,
jitter: false,
},
Duration::from_millis(500),
|f, s| {
let [main, bar] =
Layout::vertical([Constraint::Min(5), Constraint::Length(2)]).areas(f.area());
let mut p = BoxPlot::new()
.orientation(s.orientation)
.show_outliers(s.outliers)
.show_mean(s.mean)
.notched(s.notched)
.jitter(s.jitter)
.theme(s.theme.clone())
.block(Block::bordered().title(" BoxPlot "));
for (i, d) in s.data.iter().enumerate() {
p = p.group(format!("G{}", i + 1), d.iter().copied());
}
f.render_widget(&p, main);
help(
f,
bar,
" o orient · r resample · w outliers · m mean · n notches · j jitter · t/T theme · q quit",
);
},
|s, key| {
match key {
KeyCode::Char('t') => s.theme = s.theme.next(),
KeyCode::Char('T') => s.theme = s.theme.prev(),
KeyCode::Char('q') | KeyCode::Esc => return true,
KeyCode::Char('o') => {
s.orientation = match s.orientation {
BoxOrientation::Vertical => BoxOrientation::Horizontal,
BoxOrientation::Horizontal => BoxOrientation::Vertical,
}
}
KeyCode::Char('w') => s.outliers = !s.outliers,
KeyCode::Char('m') => s.mean = !s.mean,
KeyCode::Char('n') => s.notched = !s.notched,
KeyCode::Char('j') => s.jitter = !s.jitter,
KeyCode::Char('r') => s.data = sample(&mut s.rng),
_ => {}
}
false
},
|_| {},
)
}