Skip to main content

lichtung_plot_ndarray/
layers.rs

1//! Supplemental layers bind their own data and never inherit parent fields.
2use crate::error::{length, BindingResult};
3use crate::frontend::{context, Context};
4use crate::{
5    AdapterError, ArraySource, BarPointLayer, CategoryInput, CategoryValues, Color, NumericInput,
6    NumericValues, PointLayer, Text, TextLayout,
7};
8
9/// An independent native point layer with its own optional array label context.
10#[derive(Clone)]
11pub struct PointLayerAdapter<'source> {
12    layer: PointLayer,
13    source: Context<'source>,
14    len: usize,
15}
16impl<'source> PointLayerAdapter<'source> {
17    /// Snapshot coordinates independently of the parent line's record count.
18    pub fn new(
19        x: impl NumericInput,
20        y: impl NumericInput,
21    ) -> std::result::Result<Self, AdapterError> {
22        Self::bind(None, x, y)
23    }
24    /// Bind coordinates and retain only this layer's source for later labels.
25    pub fn from_data(
26        source: impl ArraySource + 'source,
27        x: impl NumericInput,
28        y: impl NumericInput,
29    ) -> std::result::Result<Self, AdapterError> {
30        Self::bind(context(source)?, x, y)
31    }
32    fn bind(
33        source: Context<'source>,
34        x: impl NumericInput,
35        y: impl NumericInput,
36    ) -> BindingResult<Self> {
37        let x = x.bind_numeric(source.as_deref(), "point layer x")?;
38        let y = y.bind_numeric(source.as_deref(), "point layer y")?;
39        let len = x.len();
40        length("point layer y", y.name(), len, y.len())?;
41        Ok(Self {
42            layer: PointLayer::new(x, y),
43            source,
44            len,
45        })
46    }
47    /// Bind labels against the layer's own coordinates, not the parent source.
48    pub fn labels(mut self, labels: impl CategoryInput) -> std::result::Result<Self, AdapterError> {
49        let labels = labels.bind_category(self.source.as_deref(), "point layer labels")?;
50        length("point layer labels", labels.name(), self.len, labels.len())?;
51        self.layer = self.layer.labels(labels);
52        Ok(self)
53    }
54    /// Replace the native exact-paint override.
55    #[must_use]
56    pub fn color(mut self, color: Color) -> Self {
57        self.layer = self.layer.color(color);
58        self
59    }
60    /// Replace the radius, retaining the native result/error type.
61    pub fn radius(mut self, radius: u32) -> crate::Result<Self> {
62        self.layer = self.layer.radius(radius)?;
63        Ok(self)
64    }
65    /// Override point/label alpha with an exact byte.
66    #[must_use]
67    pub fn alpha(mut self, alpha: u8) -> Self {
68        self.layer = self.layer.alpha(alpha);
69        self
70    }
71    /// Supply a complete-layer legend identity, independently of point labels.
72    #[must_use]
73    pub fn legend_label(mut self, label: impl Into<Text>) -> Self {
74        self.layer = self.layer.legend_label(label);
75        self
76    }
77    /// Remove the legend label's local style through the native slot operation.
78    #[must_use]
79    pub fn clear_legend_label_style(mut self) -> Self {
80        self.layer = self.layer.clear_legend_label_style();
81        self
82    }
83    /// Remove the legend label's local layout through the native slot operation.
84    #[must_use]
85    pub fn clear_legend_label_layout(mut self) -> Self {
86        self.layer = self.layer.clear_legend_label_layout();
87        self
88    }
89    /// Replace native label layout.
90    #[must_use]
91    pub fn label_text_layout(mut self, layout: TextLayout) -> Self {
92        self.layer = self.layer.label_text_layout(layout);
93        self
94    }
95    /// Inspect the native paint override.
96    pub fn get_color(&self) -> Option<Color> {
97        self.layer.get_color()
98    }
99    /// Inspect the native radius override.
100    pub fn get_radius(&self) -> Option<u32> {
101        self.layer.get_radius()
102    }
103    /// Inspect the native alpha override.
104    pub fn get_alpha(&self) -> Option<u8> {
105        self.layer.get_alpha()
106    }
107}
108impl From<PointLayerAdapter<'_>> for PointLayer {
109    fn from(adapter: PointLayerAdapter<'_>) -> Self {
110        adapter.layer
111    }
112}
113
114/// Independent observations positioned at prepared native ordinary-bar centers.
115#[derive(Clone)]
116pub struct BarPointLayerAdapter<'source> {
117    layer: BarPointLayer,
118    source: Context<'source>,
119}
120impl<'source> BarPointLayerAdapter<'source> {
121    /// Bind direct categories/values and native exact paint/radius.
122    pub fn new(
123        category: impl CategoryInput,
124        value: impl NumericInput,
125        color: Color,
126        radius: u32,
127    ) -> std::result::Result<Self, AdapterError> {
128        Self::bind(None, category, value, color, radius)
129    }
130    /// Bind this layer's own source, independently of parent bar observations.
131    pub fn from_data(
132        source: impl ArraySource + 'source,
133        category: impl CategoryInput,
134        value: impl NumericInput,
135        color: Color,
136        radius: u32,
137    ) -> std::result::Result<Self, AdapterError> {
138        Self::bind(context(source)?, category, value, color, radius)
139    }
140    fn bind(
141        source: Context<'source>,
142        category: impl CategoryInput,
143        value: impl NumericInput,
144        color: Color,
145        radius: u32,
146    ) -> BindingResult<Self> {
147        let category = category.bind_category(source.as_deref(), "bar point layer category")?;
148        let value = value.bind_numeric(source.as_deref(), "bar point layer value")?;
149        length(
150            "bar point layer value",
151            value.name(),
152            category.len(),
153            value.len(),
154        )?;
155        Ok(Self {
156            layer: BarPointLayer::new(category, None, value, color, radius)?,
157            source,
158        })
159    }
160    /// Bind positioned series in this layer's source and reconstruct natively.
161    pub fn series(mut self, series: impl CategoryInput) -> std::result::Result<Self, AdapterError> {
162        let series = series.bind_category(self.source.as_deref(), "bar point layer series")?;
163        length(
164            "bar point layer series",
165            series.name(),
166            self.layer.values().len(),
167            series.len(),
168        )?;
169        self.layer = BarPointLayer::new(
170            self.layer.category_values().clone(),
171            Some(series),
172            self.layer.values().clone(),
173            self.layer.color(),
174            self.layer.radius(),
175        )?;
176        Ok(self)
177    }
178    /// Inspect native categories.
179    pub fn category_values(&self) -> &CategoryValues {
180        self.layer.category_values()
181    }
182    /// Inspect optional native positioned-series values.
183    pub fn series_values(&self) -> Option<&CategoryValues> {
184        self.layer.series_values()
185    }
186    /// Inspect owned numeric values.
187    pub fn values(&self) -> &NumericValues {
188        self.layer.values()
189    }
190    /// Inspect native complete paint.
191    pub fn color(&self) -> Color {
192        self.layer.color()
193    }
194    /// Inspect the native radius.
195    pub fn radius(&self) -> u32 {
196        self.layer.radius()
197    }
198}
199impl From<BarPointLayerAdapter<'_>> for BarPointLayer {
200    fn from(adapter: BarPointLayerAdapter<'_>) -> Self {
201        adapter.layer
202    }
203}