Skip to main content

lichtung_plot_ndarray/frontend/
forwarding.rs

1//! Local inventory of native configuration; scientific behavior stays in core.
2use super::PlotAdapter;
3use crate::{
4    coordinates::{AxisScale, AxisTick, CategoryLabelSelection, CategoryOrder, TickGeneration},
5    data::EstimateStatus,
6    elements::{
7        BarPointLayer, GuideSpec, PlotTextAnnotation, PointLayer, Significance,
8        SignificanceMarkStyle,
9    },
10    guides::{GuideTextRole, LegendComposition, LegendPolicy},
11    kind,
12    output::PngOptions,
13    plots::{
14        BarSpacing, HeatmapAnnotations, HeatmapLayout, HeatmapNullRowAnnotation,
15        HistogramComposite, InteractionStyle, LinePathStyle, QqLine, RawOverlay, WaterfallLabels,
16    },
17    scales::{CategoricalColorScale, ContinuousColorScale},
18    stats::{HistogramBins, HistogramStat, RegressionBand},
19    text::{TextAlignment, TextLayout},
20    NumericFormat, Output, Text, Theme,
21};
22use std::path::Path;
23
24macro_rules! forward {
25    ($name:ident($($arg:ident:$ty:ty),*) $(=>$bound:ident)?) => {
26        impl<K:kind::Marker> PlotAdapter<'_,K> {
27            #[doc=concat!("Forward native [`crate::Plot::",stringify!($name),"`] configuration.")]
28            #[must_use]
29            pub fn $name(mut self,$($arg:$ty),*)->Self $(where K:kind::$bound)? { self.plot=self.plot.$name($($arg),*); self }
30        }
31    };
32}
33forward!(title(text:impl Into<Text>));
34forward!(subtitle(text:impl Into<Text>));
35forward!(caption(text:impl Into<Text>));
36forward!(x_label(text:impl Into<Text>));
37forward!(y_label(text:impl Into<Text>));
38forward!(clear_title_style());
39forward!(clear_title_layout());
40forward!(clear_subtitle_style());
41forward!(clear_subtitle_layout());
42forward!(clear_caption_style());
43forward!(clear_caption_layout());
44forward!(clear_x_label_style());
45forward!(clear_x_label_layout());
46forward!(clear_y_label_style());
47forward!(clear_y_label_layout());
48forward!(clear_color_label_style()=>ContinuousColor);
49forward!(clear_color_label_layout()=>ContinuousColor);
50forward!(title_alignment(alignment:TextAlignment));
51forward!(subtitle_alignment(alignment:TextAlignment));
52forward!(theme(theme:impl Into<Option<Theme>>));
53forward!(default_theme(theme: impl Into<Option<Theme>>));
54forward!(width(width:u32));
55forward!(max_width(maximum:u32));
56forward!(size(width:u32,height:u32));
57forward!(color_label(label:impl Into<Text>)=>ContinuousColor);
58forward!(color_label_format(format:NumericFormat)=>ContinuousColor);
59forward!(heatmap_null_row_annotation(annotation:HeatmapNullRowAnnotation)=>HeatmapOptions);
60forward!(estimate_labels(format:NumericFormat)=>CoefficientOptions);
61forward!(significance_mark_style(style:SignificanceMarkStyle)=>SignificanceStyle);
62forward!(text_annotation(annotation:PlotTextAnnotation)=>TextAnnotation);
63forward!(x_category_labels(selection:CategoryLabelSelection)=>CategoricalX);
64forward!(y_category_labels(selection:CategoryLabelSelection)=>CategoricalY);
65forward!(category_order(order:impl Into<CategoryOrder>)=>CategoryOrder);
66forward!(x_category_order(order:impl Into<CategoryOrder>)=>HeatmapOptions);
67forward!(y_category_order(order:impl Into<CategoryOrder>)=>HeatmapOptions);
68forward!(x_limits(min:f64,max:f64)=>NumericX);
69forward!(y_limits(min:f64,max:f64)=>NumericY);
70forward!(x_ticks(ticks:Vec<AxisTick>)=>NumericX);
71forward!(y_ticks(ticks:Vec<AxisTick>)=>NumericY);
72forward!(x_tick_generation(generation:TickGeneration)=>NumericX);
73forward!(y_tick_generation(generation:TickGeneration)=>NumericY);
74forward!(x_tick_format(format:NumericFormat)=>NumericX);
75forward!(y_tick_format(format:NumericFormat)=>NumericY);
76forward!(guide(guide:GuideSpec)=>Guides);
77forward!(categorical_color_scale(scale:CategoricalColorScale)=>CategoricalScale);
78forward!(continuous_color_scale(scale:ContinuousColorScale)=>ContinuousColor);
79forward!(color_by_sign()=>SignedBars);
80forward!(zero_line()=>ZeroLine);
81forward!(hide_point_outline()=>PointOutline);
82forward!(show_markers(visible:bool)=>Markers);
83forward!(legend(policy:LegendPolicy)=>Legend);
84forward!(legend_composition(composition:LegendComposition)=>Legend);
85forward!(raw_overlay(overlay:RawOverlay)=>RawOverlay);
86forward!(estimate_status(statuses:impl IntoIterator<Item=EstimateStatus>)=>EstimateStatus);
87forward!(x_tick_text_layout(layout:TextLayout)=>XAxisText);
88forward!(y_tick_text_layout(layout:TextLayout)=>YAxisText);
89forward!(facet_row_text_layout(layout:TextLayout)=>Facet);
90forward!(facet_column_text_layout(layout:TextLayout)=>Facet);
91forward!(estimate_detail_text_layout(layout:TextLayout)=>EstimateDetailText);
92forward!(significance_text_layout(layout:TextLayout)=>SignificanceStyle);
93forward!(guide_text_layout(role:GuideTextRole,layout:TextLayout)=>Legend);
94macro_rules! family_forward {
95    ($kind:ident,$name:ident($($arg:ident:$ty:ty),*)) => {
96        impl PlotAdapter<'_,kind::$kind> {
97            #[doc=concat!("Forward native ",stringify!($kind)," `",stringify!($name),"` configuration.")]
98            #[must_use]
99            pub fn $name(mut self,$($arg:$ty),*)->Self { self.plot=self.plot.$name($($arg),*); self }
100        }
101    };
102}
103family_forward!(Heatmap,layout(layout:HeatmapLayout));
104family_forward!(Heatmap,annotations(annotations:HeatmapAnnotations));
105family_forward!(Heatmap,value_text_layout(layout:TextLayout));
106family_forward!(Heatmap,null_row_text_layout(layout:TextLayout));
107family_forward!(Forest,scale(scale:AxisScale));
108family_forward!(Bar,spacing(spacing:BarSpacing));
109family_forward!(HorizontalBar,spacing(spacing:BarSpacing));
110family_forward!(Histogram,bins(bins:HistogramBins));
111family_forward!(Histogram,stat(stat:HistogramStat));
112family_forward!(Histogram,composite(composite:HistogramComposite));
113family_forward!(Qq,reference_line(line:QqLine));
114family_forward!(Regression,band(band:RegressionBand));
115family_forward!(LinePath,style(style:LinePathStyle));
116family_forward!(Raincloud,hue_label(label:impl Into<Text>));
117family_forward!(Raincloud, clear_hue_label_style());
118family_forward!(Raincloud, clear_hue_label_layout());
119family_forward!(Coefficient,reference_line(value:f64));
120family_forward!(Interaction,connectors(visible:bool));
121family_forward!(Interaction,styles(styles:impl IntoIterator<Item=InteractionStyle>));
122family_forward!(Waterfall,total_format(format:NumericFormat));
123family_forward!(Waterfall,total_labels(labels:WaterfallLabels));
124family_forward!(Waterfall,total_text_layout(layout:TextLayout));
125family_forward!(Matrix,matrix_row_text_layout(layout:TextLayout));
126family_forward!(Matrix,matrix_column_text_layout(layout:TextLayout));
127macro_rules! layer_forward {
128    ($family:ident,$layer:ty) => {
129        impl PlotAdapter<'_, kind::$family> {
130            /// Append an independently bound native or adapter layer.
131            #[must_use]
132            pub fn point_layer(mut self, layer: impl Into<$layer>) -> Self {
133                self.plot = self.plot.point_layer(layer.into());
134                self
135            }
136        }
137    };
138}
139layer_forward!(Line, PointLayer);
140layer_forward!(Bar, BarPointLayer);
141layer_forward!(HorizontalBar, BarPointLayer);
142impl<K: kind::Facet> PlotAdapter<'_, K> {
143    /// Borrow the currently bound native facet.
144    #[must_use]
145    pub fn get_facet(&self) -> Option<&crate::layout::FacetSpec> {
146        self.plot.get_facet()
147    }
148}
149
150impl<K: kind::Marker> PlotAdapter<'_, K> {
151    /// Borrow the explicitly selected local theme.
152    pub fn get_theme(&self) -> Option<&Theme> {
153        self.plot.get_theme()
154    }
155
156    /// Borrow the retained construction default.
157    pub fn get_default_theme(&self) -> Option<&Theme> {
158        self.plot.get_default_theme()
159    }
160
161    /// Inspect the authored point alpha encoding without ending source lookup.
162    pub fn get_point_alpha(&self) -> &crate::scales::AlphaEncoding {
163        self.plot.get_point_alpha()
164    }
165
166    /// Inspect the authored line alpha encoding without ending source lookup.
167    pub fn get_line_alpha(&self) -> &crate::scales::AlphaEncoding {
168        self.plot.get_line_alpha()
169    }
170
171    /// Inspect the authored fill alpha encoding without ending source lookup.
172    pub fn get_fill_alpha(&self) -> &crate::scales::AlphaEncoding {
173        self.plot.get_fill_alpha()
174    }
175
176    /// Borrow the native authored label policy.
177    #[must_use]
178    pub fn get_labels(&self) -> &crate::formatting::Labels {
179        self.plot.get_labels()
180    }
181
182    /// Request an exact physical size, retaining the native source lookup.
183    pub fn size_with_unit(
184        mut self,
185        width: f64,
186        height: f64,
187        unit: crate::layout::SizeUnit,
188    ) -> crate::Result<Self> {
189        self.plot = self.plot.size_with_unit(width, height, unit)?;
190        Ok(self)
191    }
192
193    /// Validate the chart without publishing or releasing source lookup.
194    pub fn preflight(&self) -> crate::Result<()> {
195        self.plot.preflight()
196    }
197
198    /// Use the native per-call options without releasing source lookup.
199    pub fn preflight_with_operation(
200        &self,
201        options: &crate::operation::OperationOptions,
202    ) -> crate::Result<()> {
203        self.plot.preflight_with_operation(options)
204    }
205
206    /// Freeze one reusable output without publishing or releasing source lookup.
207    pub fn render(&self) -> crate::Result<Output> {
208        self.plot.render()
209    }
210
211    /// Use the native per-call options without releasing source lookup.
212    pub fn render_with_operation(
213        &self,
214        options: &crate::operation::OperationOptions,
215    ) -> crate::Result<Output> {
216        self.plot.render_with_operation(options)
217    }
218
219    /// Freeze one output and the scientific values from that same native preparation.
220    pub fn render_with_results(&self) -> crate::Result<crate::stats::RenderWithResults> {
221        self.plot.render_with_results()
222    }
223
224    /// Use explicit options for the same-preparation result operation.
225    pub fn render_with_results_with_operation(
226        &self,
227        options: &crate::operation::OperationOptions,
228    ) -> crate::Result<crate::stats::RenderWithResults> {
229        self.plot.render_with_results_with_operation(options)
230    }
231
232    /// Append caller-owned native significance annotations with their placement type.
233    #[must_use]
234    pub fn significance<P>(mut self, annotations: impl IntoIterator<Item = Significance<P>>) -> Self
235    where
236        K: kind::Significance<P>,
237    {
238        self.plot = self.plot.significance(annotations);
239        self
240    }
241    /// Render and publish SVG, returning the native reusable Output.
242    pub fn svg(&self, path: impl AsRef<Path>) -> crate::Result<Output> {
243        self.plot.svg(path)
244    }
245
246    /// Render and publish using the native operation options.
247    pub fn svg_with_operation(
248        &self,
249        path: impl AsRef<Path>,
250        options: &crate::operation::OperationOptions,
251    ) -> crate::Result<Output> {
252        self.plot.svg_with_operation(path, options)
253    }
254    /// Render and publish SVG retaining selectable text.
255    pub fn svg_preserve_text(&self, path: impl AsRef<Path>) -> crate::Result<Output> {
256        self.plot.svg_preserve_text(path)
257    }
258
259    /// Render and publish using the native operation options.
260    pub fn svg_preserve_text_with_operation(
261        &self,
262        path: impl AsRef<Path>,
263        options: &crate::operation::OperationOptions,
264    ) -> crate::Result<Output> {
265        self.plot.svg_preserve_text_with_operation(path, options)
266    }
267    /// Render and publish a 300-DPI PNG.
268    pub fn png(&self, path: impl AsRef<Path>) -> crate::Result<Output> {
269        self.plot.png(path)
270    }
271
272    /// Render and publish using the native operation options.
273    pub fn png_with_operation(
274        &self,
275        path: impl AsRef<Path>,
276        options: &crate::operation::OperationOptions,
277    ) -> crate::Result<Output> {
278        self.plot.png_with_operation(path, options)
279    }
280    /// Render and publish a source-sized PNG.
281    pub fn png_preview(&self, path: impl AsRef<Path>) -> crate::Result<Output> {
282        self.plot.png_preview(path)
283    }
284
285    /// Render and publish using the native operation options.
286    pub fn png_preview_with_operation(
287        &self,
288        path: impl AsRef<Path>,
289        options: &crate::operation::OperationOptions,
290    ) -> crate::Result<Output> {
291        self.plot.png_preview_with_operation(path, options)
292    }
293    /// Publish using native caller-owned PNG sizing and encoding options.
294    pub fn png_with_options(
295        &self,
296        path: impl AsRef<Path>,
297        options: PngOptions,
298    ) -> crate::Result<Output> {
299        self.plot.png_with_options(path, options)
300    }
301
302    /// Render and publish using native device and operation options.
303    pub fn png_with_options_with_operation(
304        &self,
305        path: impl AsRef<Path>,
306        options: PngOptions,
307        operation: &crate::operation::OperationOptions,
308    ) -> crate::Result<Output> {
309        self.plot
310            .png_with_options_with_operation(path, options, operation)
311    }
312}
313
314// Original role and declaration queries forward the same owning native values.
315impl<K: crate::kind::Marker> PlotAdapter<'_, K> {
316    /// Discover actual original inputs in the native plot.
317    pub fn data_roles(
318        &self,
319    ) -> std::result::Result<Vec<crate::data::DataRoleInfo>, crate::AdapterError> {
320        Ok(self.plot.data_roles()?)
321    }
322
323    /// Inspect original inputs of an actual declaration.
324    pub fn data_roles_at(
325        &self,
326        target: &crate::elements::ElementRef,
327    ) -> std::result::Result<Vec<crate::data::DataRoleInfo>, crate::AdapterError> {
328        Ok(self.plot.data_roles_at(target)?)
329    }
330
331    /// Inspect original inputs of actual declarations in a panel.
332    pub fn data_roles_in(
333        &self,
334        panel: &crate::layout::PanelPath,
335    ) -> std::result::Result<Vec<crate::data::DataRoleInfo>, crate::AdapterError> {
336        Ok(self.plot.data_roles_in(panel)?)
337    }
338
339    /// Read an actual original input, without source lookup.
340    pub fn data(
341        &self,
342        selector: impl Into<crate::data::DataSelector>,
343    ) -> std::result::Result<crate::data::DataValue, crate::AdapterError> {
344        Ok(self.plot.data(selector)?)
345    }
346
347    /// Discover actual native scientific layer declarations.
348    pub fn elements(
349        &self,
350        query: crate::elements::ElementQuery,
351    ) -> std::result::Result<Vec<crate::elements::ElementRef>, crate::AdapterError> {
352        Ok(self.plot.elements(query)?)
353    }
354
355    /// Find an actual native declaration.
356    pub fn element_ref(
357        &self,
358        query: impl Into<crate::elements::ElementQuery>,
359    ) -> std::result::Result<crate::elements::ElementRef, crate::AdapterError> {
360        Ok(self.plot.element_ref(query)?)
361    }
362
363    /// Read an owning native declaration snapshot.
364    pub fn read_element(
365        &self,
366        reference: &crate::elements::ElementRef,
367    ) -> std::result::Result<crate::elements::ElementSnapshot, crate::AdapterError> {
368        Ok(self.plot.read_element(reference)?)
369    }
370}
371
372impl<K: kind::Marker> PlotAdapter<'_, K> {
373    /// Read actual supplied significance annotations.
374    pub fn get_significance(&self) -> Option<&[crate::elements::SignificanceAnnotation]> {
375        self.plot.get_significance()
376    }
377}
378
379macro_rules! stat_result {
380    ($kind:ident, $result:ty) => {
381        impl PlotAdapter<'_, kind::$kind> {
382            /// Prepare actual scientific values without rendering.
383            pub fn stat_result(&self) -> crate::Result<$result> {
384                self.plot.stat_result()
385            }
386            /// Prepare with native operation options.
387            pub fn stat_result_with_operation(
388                &self,
389                options: &crate::operation::OperationOptions,
390            ) -> crate::Result<$result> {
391                self.plot.stat_result_with_operation(options)
392            }
393            /// Prepare actual values for the selected scientific panel.
394            pub fn stat_result_in(
395                &self,
396                panel: &crate::layout::PanelPath,
397            ) -> crate::Result<$result> {
398                self.plot.stat_result_in(panel)
399            }
400            /// Prepare the selected panel with explicit operation options.
401            pub fn stat_result_in_with_operation(
402                &self,
403                panel: &crate::layout::PanelPath,
404                options: &crate::operation::OperationOptions,
405            ) -> crate::Result<$result> {
406                self.plot.stat_result_in_with_operation(panel, options)
407            }
408        }
409    };
410}
411stat_result!(Histogram, crate::plots::HistogramResult);
412stat_result!(Density, crate::plots::DensityResult);
413stat_result!(Qq, crate::plots::ProbabilityPlotResult);
414stat_result!(Regression, crate::stats::FitResult);
415stat_result!(Bar, crate::stats::StatResult);
416stat_result!(HorizontalBar, crate::stats::StatResult);
417stat_result!(StackedBar, crate::stats::StatResult);
418stat_result!(StackedHorizontalBar, crate::stats::StatResult);
419stat_result!(Interaction, crate::stats::StatResult);
420stat_result!(Waterfall, crate::stats::StatResult);
421stat_result!(Boxplot, crate::plots::BoxSummary);
422stat_result!(Violin, crate::plots::DensityProfiles);
423stat_result!(Raincloud, crate::plots::RaincloudGeometry);
424stat_result!(PointInterval, crate::stats::StatResult);
425stat_result!(Forest, crate::stats::StatResult);
426stat_result!(Coefficient, crate::stats::StatResult);
427
428macro_rules! density_group_result {
429    ($kind:ident) => {
430        impl PlotAdapter<'_, kind::$kind> {
431            /// Prepare the actual selected original group of a density panel.
432            pub fn stat_result_group(
433                &self,
434                panel: &crate::layout::PanelPath,
435                group: &crate::data::GroupKey,
436            ) -> crate::Result<crate::plots::DensityResult> {
437                self.plot.stat_result_group(panel, group)
438            }
439
440            /// Prepare an original density group with per-call resource options.
441            pub fn stat_result_group_with_operation(
442                &self,
443                panel: &crate::layout::PanelPath,
444                group: &crate::data::GroupKey,
445                options: &crate::operation::OperationOptions,
446            ) -> crate::Result<crate::plots::DensityResult> {
447                self.plot
448                    .stat_result_group_with_operation(panel, group, options)
449            }
450        }
451    };
452}
453density_group_result!(Density);
454density_group_result!(Marginal);
455density_group_result!(Matrix);
456
457macro_rules! regional_density_result {
458    ($kind:ident) => {
459        impl PlotAdapter<'_, kind::$kind> {
460            /// Prepare actual density values for the selected scientific region.
461            pub fn stat_result_in(
462                &self,
463                panel: &crate::layout::PanelPath,
464            ) -> crate::Result<crate::plots::DensityResult> {
465                self.plot.stat_result_in(panel)
466            }
467
468            /// Prepare the selected region with operation options.
469            pub fn stat_result_in_with_operation(
470                &self,
471                panel: &crate::layout::PanelPath,
472                options: &crate::operation::OperationOptions,
473            ) -> crate::Result<crate::plots::DensityResult> {
474                self.plot.stat_result_in_with_operation(panel, options)
475            }
476        }
477    };
478}
479regional_density_result!(Marginal);
480regional_density_result!(Matrix);