use super::format::NumericFormat;
use super::prefixes::selection::SelectedScale;
use super::set::FormattedSet;
use crate::{
AxisScale, FacetCell, NumericFactor, NumericLocale, NumericRate, NumericUnit, PlotKind, Result,
Text,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CoordinateAxis {
X,
Y,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NumericRole {
XAxis,
YAxis,
ContinuousColor,
HeatmapValues,
CoefficientLabels,
WaterfallTotals,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NumericPurpose {
IntrinsicProbe,
ActualSet,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NumericRangeOrigin {
Automatic,
Explicit,
ParentResolved,
}
#[derive(Debug, Clone, Copy)]
pub enum NumericOperand<'a> {
Binary64(f64),
PowerOfTen {
exponent: f64,
},
Multiplicative {
anchor: f64,
ratio: f64,
index: &'a str,
},
}
#[derive(Debug, Clone)]
pub enum NumericIdentity<'a> {
Tick {
axis: CoordinateAxis,
source_index: usize,
},
GuideTick {
source_index: usize,
},
HeatmapCell {
row: &'a str,
column: &'a str,
},
EstimateRow {
category: &'a str,
},
WaterfallBase,
WaterfallTerminal,
Probe {
index: usize,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct OwnerInfo {
pub(crate) kind: Option<PlotKind>,
pub(crate) canvas: Option<(usize, usize)>,
pub(crate) facet: Option<FacetCell>,
pub(crate) shared: bool,
pub(crate) scope: Option<String>,
}
#[derive(Debug, Clone)]
pub struct NumericOwner<'a>(pub(crate) &'a OwnerInfo);
impl<'a> NumericOwner<'a> {
pub const fn plot_kind(&self) -> Option<PlotKind> {
self.0.kind
}
pub const fn canvas_cell(&self) -> Option<(usize, usize)> {
self.0.canvas
}
pub fn facet_cell(&self) -> Option<&'a FacetCell> {
self.0.facet.as_ref()
}
pub const fn is_shared(&self) -> bool {
self.0.shared
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Identity {
Tick(CoordinateAxis, usize),
GuideTick(usize),
Heatmap(String, String),
Estimate(String),
Base,
Terminal,
Probe(usize),
}
impl Identity {
fn view(&self) -> NumericIdentity<'_> {
match self {
Self::Tick(axis, index) => NumericIdentity::Tick {
axis: *axis,
source_index: *index,
},
Self::GuideTick(index) => NumericIdentity::GuideTick {
source_index: *index,
},
Self::Heatmap(row, column) => NumericIdentity::HeatmapCell { row, column },
Self::Estimate(category) => NumericIdentity::EstimateRow { category },
Self::Base => NumericIdentity::WaterfallBase,
Self::Terminal => NumericIdentity::WaterfallTerminal,
Self::Probe(index) => NumericIdentity::Probe { index: *index },
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ContextValue {
pub(crate) operand: super::operand::Operand,
pub(crate) identity: Identity,
}
impl ContextValue {
fn operand(&self) -> NumericOperand<'_> {
match &self.operand {
super::operand::Operand::Binary64(bits) => {
NumericOperand::Binary64(f64::from_bits(*bits))
}
super::operand::Operand::PowerOfTen(bits) => NumericOperand::PowerOfTen {
exponent: f64::from_bits(*bits),
},
super::operand::Operand::Multiplicative(value) => NumericOperand::Multiplicative {
anchor: f64::from_bits(value.anchor_bits),
ratio: f64::from_bits(value.ratio_bits),
index: &value.index_text,
},
}
}
}
#[derive(Debug, Clone)]
pub struct NumericValue<'a>(pub(crate) &'a ContextValue);
impl<'a> NumericValue<'a> {
pub fn operand(&self) -> NumericOperand<'a> {
self.0.operand()
}
pub fn identity(&self) -> NumericIdentity<'a> {
self.0.identity.view()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct DomainInfo {
pub(crate) limits: (u64, u64),
pub(crate) data: Option<(u64, u64)>,
pub(crate) scale: AxisScale,
pub(crate) origin: NumericRangeOrigin,
}
#[derive(Debug, Clone)]
pub struct NumericDomain<'a>(pub(crate) &'a DomainInfo);
impl<'a> NumericDomain<'a> {
pub fn lower(&self) -> NumericOperand<'a> {
endpoint(self.0.limits.0, self.0.scale)
}
pub fn upper(&self) -> NumericOperand<'a> {
endpoint(self.0.limits.1, self.0.scale)
}
pub fn data_limits(&self) -> Option<(f64, f64)> {
self.0
.data
.map(|(a, b)| (f64::from_bits(a), f64::from_bits(b)))
}
pub fn scale_limits(&self) -> (f64, f64) {
(
f64::from_bits(self.0.limits.0),
f64::from_bits(self.0.limits.1),
)
}
pub const fn scale(&self) -> AxisScale {
self.0.scale
}
pub const fn origin(&self) -> NumericRangeOrigin {
self.0.origin
}
}
fn endpoint<'a>(bits: u64, scale: AxisScale) -> NumericOperand<'a> {
if scale == AxisScale::Log10 {
NumericOperand::PowerOfTen {
exponent: f64::from_bits(bits),
}
} else {
NumericOperand::Binary64(f64::from_bits(bits))
}
}
#[derive(Debug, Clone)]
pub struct NumericFactors<'a> {
pub(crate) multiplier: f64,
pub(crate) decimal_scale: i16,
pub(crate) rate: NumericRate,
pub(crate) scale: &'a SelectedScale,
pub(crate) unit: Option<&'a NumericUnit>,
}
impl<'a> NumericFactors<'a> {
pub const fn multiplier(&self) -> f64 {
self.multiplier
}
pub const fn decimal_scale(&self) -> i16 {
self.decimal_scale
}
pub const fn rate(&self) -> NumericRate {
self.rate
}
pub const fn divisor(&self) -> &'a NumericFactor {
&self.scale.divisor
}
pub fn prefix(&self) -> &'a str {
&self.scale.prefix
}
pub fn suffix(&self) -> &'a str {
&self.scale.suffix
}
pub const fn unit(&self) -> Option<&'a NumericUnit> {
self.unit
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ContextInfo {
pub(crate) owner: OwnerInfo,
pub(crate) role: NumericRole,
pub(crate) purpose: NumericPurpose,
pub(crate) values: Vec<ContextValue>,
pub(crate) domain: Option<DomainInfo>,
pub(crate) generation: Option<crate::TickGeneration>,
pub(crate) window: Option<[(u64, u64); 2]>,
}
#[derive(Debug, Clone)]
pub struct NumericSetContext<'a> {
pub(crate) info: &'a ContextInfo,
pub(crate) values: &'a [NumericValue<'a>],
pub(crate) format: &'a NumericFormat,
pub(crate) metadata: &'a FormattedSet,
pub(crate) operands: &'a [super::operand::Operand],
pub(crate) required: bool,
pub(crate) helper: &'a std::cell::OnceCell<FormattedSet>,
}
impl<'a> NumericSetContext<'a> {
pub fn owner(&self) -> NumericOwner<'a> {
NumericOwner(&self.info.owner)
}
pub const fn role(&self) -> NumericRole {
self.info.role
}
pub const fn purpose(&self) -> NumericPurpose {
self.info.purpose
}
pub const fn values(&self) -> &'a [NumericValue<'a>] {
self.values
}
pub fn domain(&self) -> Option<NumericDomain<'a>> {
self.info.domain.as_ref().map(NumericDomain)
}
pub fn locale(&self) -> &'a NumericLocale {
self.format.locale_ref()
}
pub fn common_factors(&self) -> Option<NumericFactors<'a>> {
let first = self.metadata.scales.first()?;
self.metadata
.scales
.iter()
.all(|scale| scale == first)
.then(|| self.factors(0))
}
pub fn format_builtin(&self) -> Result<NumericLabels> {
let builtin = self.builtin()?;
Ok(NumericLabels {
labels: crate::data::source::collect_source(
builtin
.labels
.iter()
.map(|label| crate::data::source::copy_source_string(label)),
)?,
explanation: builtin
.explanation
.as_ref()
.map(Text::try_copy)
.transpose()?,
})
}
fn builtin(&self) -> Result<&FormattedSet> {
if self.helper.get().is_none() {
let result = self.format.function_helper(
self.operands,
&self.metadata.scales,
self.required,
self.info.purpose == NumericPurpose::IntrinsicProbe,
)?;
self.helper.set(result).map_err(|_| {
crate::Error::Render("numeric helper was initialized recursively".into())
})?;
}
self.helper
.get()
.ok_or_else(|| crate::Error::Render("numeric helper result is missing".into()))
}
fn factors(&self, index: usize) -> NumericFactors<'a> {
let (multiplier, decimal_scale, rate) = self.format.factor_inputs();
NumericFactors {
multiplier,
decimal_scale,
rate,
scale: &self.metadata.scales[index],
unit: self.metadata.unit.as_ref(),
}
}
}
#[derive(Debug, Clone)]
pub struct NumericValueContext<'a> {
pub(crate) set: &'a NumericSetContext<'a>,
pub(crate) index: usize,
}
impl<'a> NumericValueContext<'a> {
pub const fn set(&self) -> &NumericSetContext<'a> {
self.set
}
pub const fn index(&self) -> usize {
self.index
}
pub fn value(&self) -> &NumericValue<'a> {
&self.set.values[self.index]
}
pub fn factors(&self) -> NumericFactors<'a> {
self.set.factors(self.index)
}
pub fn format_builtin(&self) -> Result<String> {
crate::data::source::copy_source_string(&self.set.builtin()?.labels[self.index])
}
}
#[derive(Debug, Clone)]
pub struct NumericLabels {
labels: Vec<String>,
explanation: Option<Text>,
}
impl NumericLabels {
pub const fn new(labels: Vec<String>) -> Self {
Self {
labels,
explanation: None,
}
}
#[must_use]
pub fn explanation(mut self, value: impl Into<Text>) -> Self {
self.explanation = Some(value.into());
self
}
pub fn labels(&self) -> &[String] {
&self.labels
}
pub const fn get_explanation(&self) -> Option<&Text> {
self.explanation.as_ref()
}
pub fn into_parts(self) -> (Vec<String>, Option<Text>) {
(self.labels, self.explanation)
}
}