use crate::data::IntoSeries;
use crate::mark::{Bars, Cells, Line, Points};
use crate::plot::Plot;
pub fn line<'a>(values: impl IntoSeries<'a>) -> Plot<'a> {
Plot::new().layer(Line::y(values))
}
pub fn scatter<'a>(x: impl IntoSeries<'a>, y: impl IntoSeries<'a>) -> Plot<'a> {
Plot::new().layer(Points::xy(x, y))
}
pub fn bar<'a>(
categories: impl IntoIterator<Item = impl Into<String>>,
values: impl IntoSeries<'a>,
) -> Plot<'a> {
Plot::new().layer(Bars::new(categories, values))
}
pub fn hist<'a>(values: impl IntoSeries<'a>) -> Plot<'a> {
let series = values.into_series();
match crate::stat::Bins::auto(series.as_slice(), 60) {
Some(bins) => {
let counts: Vec<f64> = bins.counts().iter().map(|&count| count as f64).collect();
Plot::new().layer(Bars::spans(bins.start(), bins.width(), counts))
}
None => Plot::new(),
}
}
pub fn stairs<'a>(values: impl IntoSeries<'a>) -> Plot<'a> {
let series = values.into_series();
let mut x = Vec::with_capacity(series.len() * 2);
let mut y = Vec::with_capacity(series.len() * 2);
for (index, value) in series.iter().enumerate() {
if index > 0 {
x.push(index as f64);
y.push(y.last().copied().unwrap_or(value));
}
x.push(index as f64);
y.push(value);
}
Plot::new().layer(Line::xy(x, y))
}
pub fn ecdf<'a>(values: impl IntoSeries<'a>) -> Plot<'a> {
let series = values.into_series();
let (sorted, fractions) = crate::stat::ecdf(series.as_slice());
let mut x = Vec::with_capacity(sorted.len() * 2);
let mut y = Vec::with_capacity(sorted.len() * 2);
let mut previous = 0.0f64;
for (value, fraction) in sorted.into_iter().zip(fractions) {
x.push(value);
y.push(previous);
x.push(value);
y.push(fraction);
previous = fraction;
}
Plot::new().layer(Line::xy(x, y))
}
pub fn heatmap<'a>(columns: usize, values: impl IntoSeries<'a>) -> Plot<'a> {
Plot::new().layer(Cells::matrix(columns, values))
}
pub fn hist2d<'a>(x: impl IntoSeries<'a>, y: impl IntoSeries<'a>) -> Plot<'a> {
let xs = x.into_series();
let ys = y.into_series();
match crate::stat::bins2(xs.as_slice(), ys.as_slice(), 48, 32) {
Some(grid) => {
let counts: Vec<f64> = grid
.counts
.into_iter()
.map(|count| if count == 0.0 { f64::NAN } else { count })
.collect();
Plot::new().layer(Cells::matrix(grid.columns, counts).extents(grid.x, grid.y))
}
None => Plot::new(),
}
}