1use crate::capture::Capture;
3use crate::error::{length, BindingResult};
4use crate::frontend::{context, Context};
5use crate::{
6 binding::{ArraySource, CategoryInput, NumericInput},
7 elements::{BarPointLayer, PointLayer},
8 text::TextLayout,
9 AdapterError, CategoryValues, Color, NumericValues, Text,
10};
11
12#[derive(Clone)]
14pub struct PointLayerAdapter<'source> {
15 layer: PointLayer,
16 source: Context<'source>,
17 source_ref: Option<crate::data::DataSourceRef>,
18 len: usize,
19}
20impl<'source> PointLayerAdapter<'source> {
21 pub fn new(
23 x: impl NumericInput,
24 y: impl NumericInput,
25 ) -> std::result::Result<Self, AdapterError> {
26 Self::bind(None, x, y)
27 }
28 pub fn from_data(
30 source: impl ArraySource + 'source,
31 x: impl NumericInput,
32 y: impl NumericInput,
33 ) -> std::result::Result<Self, AdapterError> {
34 Self::bind(context(source)?, x, y)
35 }
36 fn bind(
37 source: Context<'source>,
38 x: impl NumericInput,
39 y: impl NumericInput,
40 ) -> BindingResult<Self> {
41 let capture = Capture::new(source.as_deref());
42 let x = capture.bind(x, 1, |input, source| {
43 input.bind_numeric(source, "point layer x")
44 })?;
45 let y = capture.bind(y, 1, |input, source| {
46 input.bind_numeric(source, "point layer y")
47 })?;
48 let len = x.len();
49 length("point layer y", y.name(), len, y.len())?;
50 let source_ref = capture.reference();
51 Ok(Self {
52 layer: PointLayer::new(x, y),
53 source,
54 source_ref,
55 len,
56 })
57 }
58 pub fn labels(mut self, labels: impl CategoryInput) -> std::result::Result<Self, AdapterError> {
60 let capture = Capture::recapture(self.source.as_deref(), self.source_ref.as_ref());
61 let labels = capture.bind(labels, 1, |input, source| {
62 input.bind_category(source, "point layer labels")
63 })?;
64 length("point layer labels", labels.name(), self.len, labels.len())?;
65 self.layer = self.layer.labels(labels);
66 Ok(self)
67 }
68 #[must_use]
70 pub fn color(mut self, color: Color) -> Self {
71 self.layer = self.layer.color(color);
72 self
73 }
74 pub fn radius(mut self, radius: u32) -> crate::Result<Self> {
76 self.layer = self.layer.radius(radius)?;
77 Ok(self)
78 }
79 pub fn alpha(mut self, alpha: f64) -> std::result::Result<Self, AdapterError> {
81 self.layer = self.layer.alpha(alpha)?;
82 Ok(self)
83 }
84 #[must_use]
86 pub fn legend_label(mut self, label: impl Into<Text>) -> Self {
87 self.layer = self.layer.legend_label(label);
88 self
89 }
90 #[must_use]
92 pub fn clear_legend_label_style(mut self) -> Self {
93 self.layer = self.layer.clear_legend_label_style();
94 self
95 }
96 #[must_use]
98 pub fn clear_legend_label_layout(mut self) -> Self {
99 self.layer = self.layer.clear_legend_label_layout();
100 self
101 }
102 #[must_use]
104 pub fn label_text_layout(mut self, layout: TextLayout) -> Self {
105 self.layer = self.layer.label_text_layout(layout);
106 self
107 }
108 pub fn get_color(&self) -> Option<Color> {
110 self.layer.get_color()
111 }
112 pub fn get_radius(&self) -> Option<u32> {
114 self.layer.get_radius()
115 }
116 pub fn get_alpha(&self) -> Option<f64> {
118 self.layer.get_alpha()
119 }
120}
121impl From<PointLayerAdapter<'_>> for PointLayer {
122 fn from(adapter: PointLayerAdapter<'_>) -> Self {
123 adapter.layer
124 }
125}
126
127#[derive(Clone)]
129pub struct BarPointLayerAdapter<'source> {
130 layer: BarPointLayer,
131 source: Context<'source>,
132 source_ref: Option<crate::data::DataSourceRef>,
133}
134impl<'source> BarPointLayerAdapter<'source> {
135 pub fn new(
137 category: impl CategoryInput,
138 value: impl NumericInput,
139 ) -> std::result::Result<Self, AdapterError> {
140 Self::bind(None, category, value)
141 }
142 pub fn from_data(
144 source: impl ArraySource + 'source,
145 category: impl CategoryInput,
146 value: impl NumericInput,
147 ) -> std::result::Result<Self, AdapterError> {
148 Self::bind(context(source)?, category, value)
149 }
150 fn bind(
151 source: Context<'source>,
152 category: impl CategoryInput,
153 value: impl NumericInput,
154 ) -> BindingResult<Self> {
155 let capture = Capture::new(source.as_deref());
156 let category = capture.bind(category, 1, |input, source| {
157 input.bind_category(source, "bar point layer category")
158 })?;
159 let value = capture.bind(value, 1, |input, source| {
160 input.bind_numeric(source, "bar point layer value")
161 })?;
162 length(
163 "bar point layer value",
164 value.name(),
165 category.len(),
166 value.len(),
167 )?;
168 let source_ref = capture.reference();
169 Ok(Self {
170 layer: BarPointLayer::new(category, value),
171 source,
172 source_ref,
173 })
174 }
175 pub fn series(mut self, series: impl CategoryInput) -> std::result::Result<Self, AdapterError> {
177 let capture = Capture::recapture(self.source.as_deref(), self.source_ref.as_ref());
178 let series = capture.bind(series, 1, |input, source| {
179 input.bind_category(source, "bar point layer series")
180 })?;
181 length(
182 "bar point layer series",
183 series.name(),
184 self.layer.values().len(),
185 series.len(),
186 )?;
187 self.layer = self.layer.series(series);
188 Ok(self)
189 }
190 pub fn category_values(&self) -> &CategoryValues {
192 self.layer.category_values()
193 }
194 pub fn series_values(&self) -> Option<&CategoryValues> {
196 self.layer.series_values()
197 }
198 pub fn values(&self) -> &NumericValues {
200 self.layer.values()
201 }
202 #[must_use]
204 pub fn color(mut self, color: Color) -> Self {
205 self.layer = self.layer.color(color);
206 self
207 }
208 pub fn radius(mut self, radius: u32) -> std::result::Result<Self, AdapterError> {
210 self.layer = self.layer.radius(radius)?;
211 Ok(self)
212 }
213 pub fn get_color(&self) -> Option<Color> {
215 self.layer.get_color()
216 }
217 pub fn get_radius(&self) -> Option<u32> {
219 self.layer.get_radius()
220 }
221}
222impl From<BarPointLayerAdapter<'_>> for BarPointLayer {
223 fn from(adapter: BarPointLayerAdapter<'_>) -> Self {
224 adapter.layer
225 }
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231 use ndarray::array;
232
233 #[test]
234 fn bar_point_layer_keeps_optional_style_and_independent_lookup() {
235 let values = array![[1.25], [2.25]];
236 let category = array!["A", "B"];
237 let series = array!["S2", "S1"];
238 let layer =
239 BarPointLayerAdapter::from_data(values.view(), category.view(), 0usize).unwrap();
240 assert!(layer.get_color().is_none() && layer.get_radius().is_none());
241 let fixed = Color::rgba(23, 34, 45, 0.).unwrap();
242 let layer = layer
243 .color(fixed)
244 .radius(6)
245 .unwrap()
246 .series(series.view())
247 .unwrap();
248 assert_eq!(layer.get_color(), Some(fixed));
249 assert_eq!(layer.get_radius(), Some(6));
250 let native: BarPointLayer = layer.into();
251 assert_eq!(native.series_values().unwrap().get(0), Some(Some("S2")));
252 assert_eq!(native.values().get(1), Some(Some(2.25)));
253 }
254}