1use super::{context, Context, PlotAdapter};
4use crate::error::{length, BindingResult};
5use crate::{
6 kind, AdapterError, ArraySource, CategoryInput, HistogramComposite, HistogramGeometry,
7 HistogramInput, HistogramKde, NumericInput, Plot, RaincloudGeometry, RaincloudInput,
8};
9
10macro_rules! columnar_geometry {
13 ($family:ident, $direct:ident, $from:ident, $bind:ident,
14 ($first:ident: $input:ident => $method:ident $(, $arg:ident: $arg_input:ident => $arg_method:ident)*),
15 ($(($aux:ident => $head:ident: $head_input:ident => $head_method:ident $(, $tail:ident: $tail_input:ident => $tail_method:ident)*)),+),
16 ($($dimension:ident),+), $points:expr, $plot:expr) => {
17 impl<'source> PlotAdapter<'source, kind::$family> {
18 #[expect(clippy::too_many_arguments, reason = "complete columnar geometry keeps every scientific role explicit")]
21 pub fn $direct(
22 $first: impl $input, $($arg: impl $arg_input,)*
23 $($head: impl $head_input, $($tail: impl $tail_input,)*)+
24 $($dimension: impl Into<String>),+
25 ) -> std::result::Result<Self, AdapterError> {
26 $(let $aux = None;)+
27 Self::$bind(None, $($aux,)+ $first, $($arg,)* $($head, $($tail,)*)+ $($dimension),+)
28 }
29
30 #[expect(clippy::too_many_arguments, reason = "independent sources precede explicit complete geometry roles")]
34 pub fn $from(
35 source: impl ArraySource + 'source, $($aux: Option<&dyn ArraySource>,)+
36 $first: impl $input, $($arg: impl $arg_input,)*
37 $($head: impl $head_input, $($tail: impl $tail_input,)*)+
38 $($dimension: impl Into<String>),+
39 ) -> std::result::Result<Self, AdapterError> {
40 Self::$bind(context(source)?, $($aux,)+ $first, $($arg,)* $($head, $($tail,)*)+ $($dimension),+)
41 }
42
43 #[expect(clippy::too_many_arguments, reason = "private extraction mirrors the public complete geometry roles")]
44 fn $bind(
45 source: Context<'source>, $($aux: Option<&dyn ArraySource>,)+
46 $first: impl $input, $($arg: impl $arg_input,)*
47 $($head: impl $head_input, $($tail: impl $tail_input,)*)+
48 $($dimension: impl Into<String>),+
49 ) -> BindingResult<Self> {
50 $(if let Some(aux) = $aux { aux.validate()?; })+
51 let $first = $first.$method(source.as_deref(), stringify!($first))?;
52 let len = $first.len();
53 $(let $arg = $arg.$arg_method(source.as_deref(), stringify!($arg))?;
54 length(stringify!($arg), $arg.name(), len, $arg.len())?;)*
55 $(let $head = $head.$head_method($aux, stringify!($head))?;
56 let len = $head.len();
57 $(let $tail = $tail.$tail_method($aux, stringify!($tail))?;
58 length(stringify!($tail), $tail.name(), len, $tail.len())?;)*)+
59 let points = $points;
60 let mut adapter = Self::bound($plot, source, None);
61 adapter.bindings.geometry_points = points;
62 Ok(adapter)
63 }
64 }
65 };
66}
67
68columnar_geometry!(Histogram, geometry, geometry_from_data, histogram_geometry,
69(bin_lower: NumericInput => bind_numeric, bin_upper: NumericInput => bind_numeric,
70 bin_density: NumericInput => bind_numeric),
71((kde_source => kde_x: NumericInput => bind_numeric, kde_y: NumericInput => bind_numeric),
72 (rug_source => rug_x: NumericInput => bind_numeric, rug_baseline: NumericInput => bind_numeric,
73 rug_top: NumericInput => bind_numeric)),
74(value_dimension, density_dimension), None,
75Plot::histogram(HistogramInput::geometry(HistogramGeometry {
76 value_dimension: value_dimension.into(), density_dimension: density_dimension.into(),
77 bin_lower, bin_upper, bin_density, kde_x, kde_y, rug_x, rug_baseline, rug_top,
78})));
79
80columnar_geometry!(Raincloud, geometry, geometry_from_data, raincloud_geometry,
81(category: CategoryInput => bind_category, q1: NumericInput => bind_numeric,
82 median: NumericInput => bind_numeric, q3: NumericInput => bind_numeric),
83((density_source => density_category: CategoryInput => bind_category,
84 density_y: NumericInput => bind_numeric, normalized_width: NumericInput => bind_numeric),
85 (points_source => point_category: CategoryInput => bind_category,
86 point_y: NumericInput => bind_numeric, normalized_strip: NumericInput => bind_numeric)),
87(category_dimension, value_dimension), Some(point_y.len()),
88Plot::raincloud(RaincloudInput::geometry(RaincloudGeometry {
89 category_dimension: category_dimension.into(), value_dimension: value_dimension.into(),
90 hue_dimension: None, category, hue: None, q1, median, q3,
91 density_category, density_hue: None, density_y, normalized_width,
92 point_category, point_hue: None, point_y, normalized_strip,
93})));
94
95columnar_geometry!(Raincloud, geometry_with_hue, geometry_with_hue_from_data, hued_raincloud_geometry,
96(category: CategoryInput => bind_category, hue: CategoryInput => bind_category,
97 q1: NumericInput => bind_numeric, median: NumericInput => bind_numeric, q3: NumericInput => bind_numeric),
98((density_source => density_category: CategoryInput => bind_category, density_hue: CategoryInput => bind_category,
99 density_y: NumericInput => bind_numeric, normalized_width: NumericInput => bind_numeric),
100 (points_source => point_category: CategoryInput => bind_category, point_hue: CategoryInput => bind_category,
101 point_y: NumericInput => bind_numeric, normalized_strip: NumericInput => bind_numeric)),
102(category_dimension, value_dimension, hue_dimension), Some(point_y.len()),
103Plot::raincloud(RaincloudInput::geometry(RaincloudGeometry {
104 category_dimension: category_dimension.into(), value_dimension: value_dimension.into(),
105 hue_dimension: Some(hue_dimension.into()), category, hue: Some(hue), q1, median, q3,
106 density_category, density_hue: Some(density_hue), density_y, normalized_width,
107 point_category, point_hue: Some(point_hue), point_y, normalized_strip,
108})));
109
110impl PlotAdapter<'_, kind::Histogram> {
111 pub fn composite_precomputed_kde(
114 self,
115 x: impl NumericInput,
116 y: impl NumericInput,
117 ) -> std::result::Result<Self, AdapterError> {
118 self.bind_kde(None, x, y)
119 }
120
121 pub fn composite_precomputed_kde_from_data(
124 self,
125 source: impl ArraySource,
126 x: impl NumericInput,
127 y: impl NumericInput,
128 ) -> std::result::Result<Self, AdapterError> {
129 source.validate()?;
130 self.bind_kde(Some(&source), x, y)
131 }
132
133 fn bind_kde(
134 mut self,
135 source: Option<&dyn ArraySource>,
136 x: impl NumericInput,
137 y: impl NumericInput,
138 ) -> BindingResult<Self> {
139 let x = x.bind_numeric(source, "KDE x")?;
140 let y = y.bind_numeric(source, "KDE y")?;
141 length("KDE y", y.name(), x.len(), y.len())?;
142 self.plot = self
143 .plot
144 .composite(HistogramComposite::KdeAndRug(HistogramKde::Precomputed {
145 x,
146 y,
147 }));
148 Ok(self)
149 }
150}
151
152#[cfg(test)]
153mod tests;