1use super::format::NumericFormat;
4use super::prefixes::selection::SelectedScale;
5use super::set::FormattedSet;
6use crate::{
7 coordinates::AxisScale,
8 formatting::{NumericFactor, NumericLocale, NumericRate, NumericUnit},
9 layout::FacetCell,
10 plots::PlotKind,
11 Result, Text,
12};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum CoordinateAxis {
18 X,
20 Y,
22}
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25#[non_exhaustive]
26pub enum NumericRole {
27 XAxis,
29 YAxis,
31 ContinuousColor,
33 HeatmapValues,
35 CoefficientLabels,
37 WaterfallTotals,
39}
40#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum NumericPurpose {
43 IntrinsicProbe,
45 ActualSet,
47}
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum NumericRangeOrigin {
51 Automatic,
53 Explicit,
55 ParentResolved,
57}
58
59#[derive(Debug, Clone, Copy)]
61#[non_exhaustive]
62pub enum NumericOperand<'a> {
63 Binary64(f64),
65 PowerOfTen {
67 exponent: f64,
69 },
70 Multiplicative {
72 anchor: f64,
74 ratio: f64,
76 index: &'a str,
78 },
79}
80
81#[derive(Debug, Clone)]
83#[non_exhaustive]
84pub enum NumericIdentity<'a> {
85 Tick {
87 axis: CoordinateAxis,
89 source_index: usize,
91 },
92 GuideTick {
94 source_index: usize,
96 },
97 HeatmapCell {
99 row: &'a str,
101 column: &'a str,
103 },
104 EstimateRow {
106 category: &'a str,
108 },
109 WaterfallBase,
111 WaterfallTerminal,
113 Probe {
115 index: usize,
117 },
118}
119
120#[derive(Debug, Clone, PartialEq, Eq)]
121pub(crate) struct OwnerInfo {
122 pub(crate) kind: Option<PlotKind>,
123 pub(crate) canvas: Option<(usize, usize)>,
124 pub(crate) facet: Option<FacetCell>,
127 pub(crate) original_facet: Option<FacetCell>,
128 pub(crate) shared: bool,
129 pub(crate) scope: Option<String>,
130}
131
132#[derive(Debug, Clone)]
134pub struct NumericOwner<'a>(pub(crate) &'a OwnerInfo);
135impl<'a> NumericOwner<'a> {
136 pub const fn plot_kind(&self) -> Option<PlotKind> {
138 self.0.kind
139 }
140 pub const fn canvas_cell(&self) -> Option<(usize, usize)> {
142 self.0.canvas
143 }
144 pub fn facet_cell(&self) -> Option<&'a FacetCell> {
146 self.0.original_facet.as_ref()
147 }
148 pub const fn is_shared(&self) -> bool {
150 self.0.shared
151 }
152}
153
154#[derive(Debug, Clone, PartialEq, Eq)]
155pub(crate) enum Identity {
156 Tick(CoordinateAxis, usize),
157 GuideTick(usize),
158 Heatmap(String, String),
159 Estimate(String),
160 Base,
161 Terminal,
162 Probe(usize),
163}
164impl Identity {
165 fn view(&self) -> NumericIdentity<'_> {
166 match self {
167 Self::Tick(axis, index) => NumericIdentity::Tick {
168 axis: *axis,
169 source_index: *index,
170 },
171 Self::GuideTick(index) => NumericIdentity::GuideTick {
172 source_index: *index,
173 },
174 Self::Heatmap(row, column) => NumericIdentity::HeatmapCell { row, column },
175 Self::Estimate(category) => NumericIdentity::EstimateRow { category },
176 Self::Base => NumericIdentity::WaterfallBase,
177 Self::Terminal => NumericIdentity::WaterfallTerminal,
178 Self::Probe(index) => NumericIdentity::Probe { index: *index },
179 }
180 }
181}
182
183#[derive(Debug, Clone, PartialEq, Eq)]
184pub(crate) struct ContextValue {
185 pub(crate) operand: super::operand::Operand,
186 pub(crate) identity: Identity,
187}
188impl ContextValue {
189 fn operand(&self) -> NumericOperand<'_> {
190 match &self.operand {
191 super::operand::Operand::Binary64(bits) => {
192 NumericOperand::Binary64(f64::from_bits(*bits))
193 }
194 super::operand::Operand::PowerOfTen(bits) => NumericOperand::PowerOfTen {
195 exponent: f64::from_bits(*bits),
196 },
197 super::operand::Operand::Multiplicative(value) => NumericOperand::Multiplicative {
198 anchor: f64::from_bits(value.anchor_bits),
199 ratio: f64::from_bits(value.ratio_bits),
200 index: &value.index_text,
201 },
202 }
203 }
204}
205
206#[derive(Debug, Clone)]
208pub struct NumericValue<'a>(pub(crate) &'a ContextValue);
209impl<'a> NumericValue<'a> {
210 pub fn operand(&self) -> NumericOperand<'a> {
212 self.0.operand()
213 }
214 pub fn identity(&self) -> NumericIdentity<'a> {
216 self.0.identity.view()
217 }
218}
219
220#[derive(Debug, Clone, PartialEq, Eq)]
221pub(crate) struct DomainInfo {
222 pub(crate) limits: (u64, u64),
223 pub(crate) data: Option<(u64, u64)>,
224 pub(crate) scale: AxisScale,
225 pub(crate) origin: NumericRangeOrigin,
226}
227
228#[derive(Debug, Clone)]
230pub struct NumericDomain<'a>(pub(crate) &'a DomainInfo);
231impl<'a> NumericDomain<'a> {
232 pub fn lower(&self) -> NumericOperand<'a> {
234 endpoint(self.0.limits.0, self.0.scale)
235 }
236 pub fn upper(&self) -> NumericOperand<'a> {
238 endpoint(self.0.limits.1, self.0.scale)
239 }
240 pub fn data_limits(&self) -> Option<(f64, f64)> {
242 self.0
243 .data
244 .map(|(a, b)| (f64::from_bits(a), f64::from_bits(b)))
245 }
246 pub fn scale_limits(&self) -> (f64, f64) {
248 (
249 f64::from_bits(self.0.limits.0),
250 f64::from_bits(self.0.limits.1),
251 )
252 }
253 pub const fn scale(&self) -> AxisScale {
255 self.0.scale
256 }
257 pub const fn origin(&self) -> NumericRangeOrigin {
259 self.0.origin
260 }
261}
262fn endpoint<'a>(bits: u64, scale: AxisScale) -> NumericOperand<'a> {
263 if scale == AxisScale::Log10 {
264 NumericOperand::PowerOfTen {
265 exponent: f64::from_bits(bits),
266 }
267 } else {
268 NumericOperand::Binary64(f64::from_bits(bits))
269 }
270}
271
272#[derive(Debug, Clone)]
274pub struct NumericFactors<'a> {
275 pub(crate) multiplier: f64,
276 pub(crate) decimal_scale: i16,
277 pub(crate) rate: NumericRate,
278 pub(crate) scale: &'a SelectedScale,
279 pub(crate) unit: Option<&'a NumericUnit>,
280}
281impl<'a> NumericFactors<'a> {
282 pub const fn multiplier(&self) -> f64 {
284 self.multiplier
285 }
286 pub const fn decimal_scale(&self) -> i16 {
288 self.decimal_scale
289 }
290 pub const fn rate(&self) -> NumericRate {
292 self.rate
293 }
294 pub const fn divisor(&self) -> &'a NumericFactor {
296 &self.scale.divisor
297 }
298 pub fn prefix(&self) -> &'a str {
300 &self.scale.prefix
301 }
302 pub fn suffix(&self) -> &'a str {
304 &self.scale.suffix
305 }
306 pub const fn unit(&self) -> Option<&'a NumericUnit> {
308 self.unit
309 }
310}
311
312#[derive(Debug, Clone, PartialEq, Eq)]
313pub(crate) struct ContextInfo {
314 pub(crate) owner: OwnerInfo,
315 pub(crate) role: NumericRole,
316 pub(crate) purpose: NumericPurpose,
317 pub(crate) values: Vec<ContextValue>,
318 pub(crate) domain: Option<DomainInfo>,
319 pub(crate) generation: Option<crate::coordinates::TickGeneration>,
320 pub(crate) window: Option<[(u64, u64); 2]>,
321}
322
323#[derive(Debug, Clone)]
325pub struct NumericSetContext<'a> {
326 pub(crate) info: &'a ContextInfo,
327 pub(crate) values: &'a [NumericValue<'a>],
328 pub(crate) format: &'a NumericFormat,
329 pub(crate) metadata: &'a FormattedSet,
330 pub(crate) operands: &'a [super::operand::Operand],
331 pub(crate) required: bool,
332 pub(crate) helper: &'a std::cell::OnceCell<FormattedSet>,
333}
334impl<'a> NumericSetContext<'a> {
335 pub fn owner(&self) -> NumericOwner<'a> {
337 NumericOwner(&self.info.owner)
338 }
339 pub const fn role(&self) -> NumericRole {
341 self.info.role
342 }
343 pub const fn purpose(&self) -> NumericPurpose {
345 self.info.purpose
346 }
347 pub const fn values(&self) -> &'a [NumericValue<'a>] {
349 self.values
350 }
351 pub fn domain(&self) -> Option<NumericDomain<'a>> {
353 self.info.domain.as_ref().map(NumericDomain)
354 }
355 pub fn locale(&self) -> &'a NumericLocale {
357 self.format.locale_ref()
358 }
359 pub fn common_factors(&self) -> Option<NumericFactors<'a>> {
361 let first = self.metadata.scales.first()?;
362 self.metadata
363 .scales
364 .iter()
365 .all(|scale| scale == first)
366 .then(|| self.factors(0))
367 }
368 pub fn format_builtin(&self) -> Result<NumericLabels> {
370 let builtin = self.builtin()?;
371 Ok(NumericLabels {
372 labels: crate::data::source::collect_source(
373 builtin
374 .labels
375 .iter()
376 .map(|label| crate::data::source::copy_source_string(label)),
377 )?,
378 explanation: builtin
379 .explanation
380 .as_ref()
381 .map(Text::try_copy)
382 .transpose()?,
383 })
384 }
385 fn builtin(&self) -> Result<&FormattedSet> {
386 if self.helper.get().is_none() {
387 let result = self.format.function_helper(
388 self.operands,
389 &self.metadata.scales,
390 self.required,
391 self.info.purpose == NumericPurpose::IntrinsicProbe,
392 )?;
393 self.helper.set(result).map_err(|_| {
394 crate::Error::Render("numeric helper was initialized recursively".into())
395 })?;
396 }
397 self.helper
398 .get()
399 .ok_or_else(|| crate::Error::Render("numeric helper result is missing".into()))
400 }
401 fn factors(&self, index: usize) -> NumericFactors<'a> {
402 let (multiplier, decimal_scale, rate) = self.format.factor_inputs();
403 NumericFactors {
404 multiplier,
405 decimal_scale,
406 rate,
407 scale: &self.metadata.scales[index],
408 unit: self.metadata.unit.as_ref(),
409 }
410 }
411}
412
413#[derive(Debug, Clone)]
415pub struct NumericValueContext<'a> {
416 pub(crate) set: &'a NumericSetContext<'a>,
417 pub(crate) index: usize,
418}
419impl<'a> NumericValueContext<'a> {
420 pub const fn set(&self) -> &NumericSetContext<'a> {
422 self.set
423 }
424 pub const fn index(&self) -> usize {
426 self.index
427 }
428 pub fn value(&self) -> &NumericValue<'a> {
430 &self.set.values[self.index]
431 }
432 pub fn factors(&self) -> NumericFactors<'a> {
434 self.set.factors(self.index)
435 }
436 pub fn format_builtin(&self) -> Result<String> {
438 crate::data::source::copy_source_string(&self.set.builtin()?.labels[self.index])
439 }
440}
441
442#[derive(Debug, Clone)]
444pub struct NumericLabels {
445 labels: Vec<String>,
446 explanation: Option<Text>,
447}
448impl NumericLabels {
449 pub const fn new(labels: Vec<String>) -> Self {
451 Self {
452 labels,
453 explanation: None,
454 }
455 }
456 #[must_use]
458 pub fn explanation(mut self, value: impl Into<Text>) -> Self {
459 self.explanation = Some(value.into());
460 self
461 }
462 pub fn labels(&self) -> &[String] {
464 &self.labels
465 }
466 pub const fn get_explanation(&self) -> Option<&Text> {
468 self.explanation.as_ref()
469 }
470 pub fn into_parts(self) -> (Vec<String>, Option<Text>) {
472 (self.labels, self.explanation)
473 }
474}