use crate::core::data::AggregateOp;
use crate::scale::{Expansion, ResolvedScale, Scale, ScaleDomain};
#[derive(Clone, Debug, Default, PartialEq)]
pub enum StackMode {
#[default]
None,
Stacked,
Normalize,
Center,
}
impl From<&str> for StackMode {
fn from(s: &str) -> Self {
match s.to_lowercase().as_str() {
"stacked" => StackMode::Stacked,
"normalize" => StackMode::Normalize,
"center" => StackMode::Center,
_ => StackMode::None,
}
}
}
#[derive(Debug, Clone)]
pub struct Y {
pub(crate) field: String,
pub(crate) aggregate: AggregateOp,
pub(crate) scale_type: Option<Scale>,
pub(crate) domain: Option<ScaleDomain>,
pub(crate) expansion: Option<Expansion>,
pub(crate) zero: Option<bool>,
pub(crate) bins: Option<usize>,
pub(crate) normalize: bool,
pub(crate) stack: StackMode,
pub(crate) resolved_scale: ResolvedScale,
}
impl Y {
pub fn new(field: &str) -> Self {
Self {
field: field.to_string(),
aggregate: AggregateOp::default(), scale_type: None,
domain: None,
expansion: None,
zero: None,
bins: None,
normalize: false, stack: StackMode::None,
resolved_scale: ResolvedScale::none(),
}
}
pub fn with_aggregate<A: Into<AggregateOp>>(mut self, op: A) -> Self {
self.aggregate = op.into();
self
}
pub fn with_scale(mut self, scale_type: Scale) -> Self {
self.scale_type = Some(scale_type);
self
}
pub fn with_domain(mut self, domain: ScaleDomain) -> Self {
self.domain = Some(domain);
self
}
pub fn with_expansion(mut self, expansion: Expansion) -> Self {
self.expansion = Some(expansion);
self
}
pub fn with_zero(mut self, zero: bool) -> Self {
self.zero = Some(zero);
self
}
pub fn with_bins(mut self, bins: usize) -> Self {
self.bins = Some(bins);
self
}
pub fn with_normalize(mut self, normalize: bool) -> Self {
self.normalize = normalize;
self
}
pub fn with_stack(mut self, stack: impl Into<StackMode>) -> Self {
self.stack = stack.into();
self
}
}
pub fn y(field: &str) -> Y {
Y::new(field)
}