use crate::data::{IntoSeries, Series};
use crate::render::Color;
#[derive(Clone)]
pub struct Area<'a> {
pub(crate) x: Option<Series<'a>>,
pub(crate) low: Option<Series<'a>>,
pub(crate) high: Series<'a>,
pub(crate) color: Option<Color>,
pub(crate) label: Option<String>,
}
impl<'a> Area<'a> {
pub fn y(values: impl IntoSeries<'a>) -> Area<'a> {
Area {
x: None,
low: None,
high: values.into_series(),
color: None,
label: None,
}
}
pub fn xy(x: impl IntoSeries<'a>, y: impl IntoSeries<'a>) -> Area<'a> {
let x = x.into_series();
let y = y.into_series();
assert_eq!(x.len(), y.len(), "Area::xy requires series of equal length");
Area {
x: Some(x),
low: None,
high: y,
color: None,
label: None,
}
}
pub fn between(
x: impl IntoSeries<'a>,
low: impl IntoSeries<'a>,
high: impl IntoSeries<'a>,
) -> Area<'a> {
let x = x.into_series();
let low = low.into_series();
let high = high.into_series();
assert!(
x.len() == low.len() && low.len() == high.len(),
"Area::between requires series of equal length"
);
Area {
x: Some(x),
low: Some(low),
high,
color: None,
label: None,
}
}
#[must_use]
pub fn color(mut self, color: Color) -> Area<'a> {
self.color = Some(color);
self
}
#[must_use]
pub fn label(mut self, label: impl Into<String>) -> Area<'a> {
self.label = Some(label.into());
self
}
pub fn into_owned(self) -> Area<'static> {
Area {
x: self.x.map(Series::into_owned),
low: self.low.map(Series::into_owned),
high: self.high.into_owned(),
color: self.color,
label: self.label,
}
}
}
impl std::fmt::Debug for Area<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Area")
.field("points", &self.high.len())
.field("banded", &self.low.is_some())
.field("color", &self.color)
.finish()
}
}
#[cfg(test)]
#[path = "tests/area_tests.rs"]
mod tests;