use gilt::color::Color;
use gilt::console::Console;
use gilt::gradient::Gradient;
use gilt::histogram::Histogram;
use gilt::panel::Panel;
use gilt::style::Style;
use gilt::text::Text;
fn border() -> Style {
Style::parse("#44475a")
}
fn titled(title: &str, color: &str, hist: Histogram) -> Panel {
Panel::new(hist)
.with_title(Text::new(
&format!(" {title} "),
Style::parse(&format!("bold {color}")),
))
.with_border_style(border())
}
struct Lcg(u64);
impl Lcg {
fn next_unit(&mut self) -> f64 {
self.0 = self
.0
.wrapping_mul(6364136223846793005)
.wrapping_add(1442695040888963407);
((self.0 >> 33) as f64) / ((1u64 << 31) as f64)
}
}
fn gaussian(n: usize, mean: f64, std: f64, seed: u64) -> Vec<f64> {
let mut rng = Lcg(seed);
(0..n)
.map(|_| {
let sum: f64 = (0..12).map(|_| rng.next_unit()).sum();
mean + std * (sum - 6.0)
})
.collect()
}
fn skewed(n: usize, seed: u64) -> Vec<f64> {
gaussian(n, 0.0, 1.0, seed)
.into_iter()
.map(|z| z * z + 0.15 * z)
.collect()
}
fn main() {
let mut console = Console::builder()
.width(72)
.force_terminal(true)
.no_color(false)
.build();
console.print(&Gradient::new(
"gilt · Histogram — raw samples binned into a distribution",
vec![
Color::from_rgb(189, 147, 249),
Color::from_rgb(255, 121, 198),
Color::from_rgb(139, 233, 253),
],
));
console.line(1);
let bell = Histogram::new(&gaussian(4000, 0.0, 1.0, 0x1234_5678))
.with_bins(36)
.with_height(8)
.with_range(-3.5, 3.5)
.with_show_axis(true)
.with_style(Style::parse("#8be9fd"));
console.print(&titled("normal(0, 1) · 4000 samples", "#8be9fd", bell));
console.line(1);
let coarse = Histogram::new(&gaussian(4000, 0.0, 1.0, 0x1234_5678))
.with_bins(14)
.with_height(6)
.with_range(-3.5, 3.5)
.with_show_axis(true)
.with_style(Style::parse("#ff79c6"));
console.print(&titled("same data · 14 coarse bins", "#ff79c6", coarse));
console.line(1);
let tail = Histogram::new(&skewed(4000, 0xC0FF_EE42))
.with_bins(36)
.with_height(8)
.with_range(0.0, 9.0)
.with_show_axis(true)
.with_style(Style::parse("#50fa7b"));
console.print(&titled("right-skewed · long tail", "#50fa7b", tail));
console.line(1);
let scores = [
52.0, 61.0, 63.0, 67.0, 68.0, 71.0, 72.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0, 81.0, 82.0,
83.0, 84.0, 85.0, 86.0, 88.0, 90.0, 92.0, 95.0, 99.0, 70.0, 73.0, 80.0, 87.0, 66.0, 91.0,
];
let exam = Histogram::new(&scores)
.with_bins(10)
.with_height(6)
.with_range(0.0, 100.0)
.with_show_axis(true)
.with_style(Style::parse("#f1fa8c"));
console.print(&titled("exam scores · 10 bins of 0..100", "#f1fa8c", exam));
console.line(1);
console.print(&Text::new(
" every histogram is a Renderable — drop one in a Panel, Table cell, or Live display.",
Style::parse("italic #6272a4"),
));
}