1use super::PlotAdapter;
2use crate::error::{binding, length, BindingResult};
3use crate::{
4 kind, AdapterError, AlphaEncoding, AlphaInput, AlphaValues, AlphaValuesInput, ArraySource,
5 Bounds, CategoryInput, CategoryValues, NumericInput, NumericValues, SourceRowsInput,
6};
7
8fn mapped_numeric(
9 source: Option<&dyn ArraySource>,
10 input: impl NumericInput,
11 mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
12 copies: usize,
13 expected: Option<usize>,
14) -> BindingResult<AlphaValues> {
15 let copies = if input.native_slots() { 1 } else { copies };
16 let raw = input.bind_numeric(source, "alpha map")?;
17 let len = raw.len();
18 let values = mapper(raw)?;
19 length("alpha mapper output", values.name(), len, values.len())?;
20 crate::alpha::expand(values, expected, copies)
21}
22fn mapped_categories(
23 source: Option<&dyn ArraySource>,
24 input: impl CategoryInput,
25 mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
26 copies: usize,
27 expected: Option<usize>,
28) -> BindingResult<AlphaValues> {
29 let copies = if input.native_slots() { 1 } else { copies };
30 let raw = input.bind_category(source, "alpha map")?;
31 let len = raw.len();
32 let values = mapper(raw)?;
33 length("alpha mapper output", values.name(), len, values.len())?;
34 crate::alpha::expand(values, expected, copies)
35}
36
37macro_rules! alpha_target {
38 ($cap:ident,$base:ident,$cells:ident,$linear:ident,$map:ident,$categories:ident,$cell_map:ident,$cell_categories:ident,
39 $single:ident,$single_map:ident,$single_categories:ident,$keyed:ident,$keyed_data:ident,$keyed_map:ident,$keyed_map_data:ident,
40 $keyed_categories:ident,$keyed_categories_data:ident,$columns:ident,$columns_data:ident,$columns_map:ident,$columns_map_data:ident,$columns_categories:ident,$columns_categories_data:ident) => {
41 impl<K: kind::$cap> PlotAdapter<'_, K> {
42 pub fn $base(
44 mut self,
45 input: impl AlphaInput,
46 ) -> std::result::Result<Self, AdapterError> {
47 let encoding = input.bind_alpha(
48 self.source.as_deref(),
49 self.alpha_len(),
50 self.row_copies(),
51 )?;
52 self.plot = self.plot.$base(encoding);
53 Ok(self)
54 }
55 pub fn $cells(
57 mut self,
58 input: impl AlphaInput,
59 ) -> std::result::Result<Self, AdapterError> {
60 let encoding = input.bind_alpha(self.source.as_deref(), self.alpha_len(), 1)?;
61 self.plot = self.plot.$base(encoding);
62 Ok(self)
63 }
64 pub fn $linear(
66 self,
67 input: impl NumericInput,
68 domain: (f64, f64),
69 range: (u8, u8),
70 ) -> std::result::Result<Self, AdapterError> {
71 self.$map(input, |values| AlphaValues::linear(values, domain, range))
72 }
73 pub fn $map(
75 self,
76 input: impl NumericInput,
77 mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
78 ) -> std::result::Result<Self, AdapterError> {
79 let values = mapped_numeric(
80 self.source.as_deref(),
81 input,
82 mapper,
83 self.row_copies(),
84 self.alpha_len(),
85 )?;
86 self.$base(AlphaEncoding::observations(values))
87 }
88 pub fn $categories(
90 self,
91 input: impl CategoryInput,
92 mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
93 ) -> std::result::Result<Self, AdapterError> {
94 let values = mapped_categories(
95 self.source.as_deref(),
96 input,
97 mapper,
98 self.row_copies(),
99 self.alpha_len(),
100 )?;
101 self.$base(AlphaEncoding::observations(values))
102 }
103 pub fn $cell_map(
105 self,
106 input: impl NumericInput,
107 mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
108 ) -> std::result::Result<Self, AdapterError> {
109 let values =
110 mapped_numeric(self.source.as_deref(), input, mapper, 1, self.alpha_len())?;
111 self.$base(AlphaEncoding::observations(values))
112 }
113 pub fn $cell_categories(
115 self,
116 input: impl CategoryInput,
117 mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
118 ) -> std::result::Result<Self, AdapterError> {
119 let values =
120 mapped_categories(self.source.as_deref(), input, mapper, 1, self.alpha_len())?;
121 self.$base(AlphaEncoding::observations(values))
122 }
123 pub fn $single(
125 self,
126 input: impl AlphaValuesInput,
127 ) -> std::result::Result<Self, AdapterError> {
128 let values = input.bind_alpha_values(self.source.as_deref())?;
129 self.$base(AlphaEncoding::single(values)?)
130 }
131 pub fn $single_map(
133 self,
134 input: impl NumericInput,
135 mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
136 ) -> std::result::Result<Self, AdapterError> {
137 let values = mapped_numeric(self.source.as_deref(), input, mapper, 1, Some(1))?;
138 self.$base(AlphaEncoding::single(values)?)
139 }
140 pub fn $single_categories(
142 self,
143 input: impl CategoryInput,
144 mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
145 ) -> std::result::Result<Self, AdapterError> {
146 let values = mapped_categories(self.source.as_deref(), input, mapper, 1, Some(1))?;
147 self.$base(AlphaEncoding::single(values)?)
148 }
149 pub fn $keyed<I, S, B>(
151 self,
152 keys: I,
153 input: impl AlphaValuesInput,
154 ) -> std::result::Result<Self, AdapterError>
155 where
156 I: IntoIterator<Item = (S, B)>,
157 S: AsRef<str>,
158 B: CategoryInput,
159 {
160 let encoding = AlphaEncoding::keyed(
161 crate::alpha::keys(self.source.as_deref(), keys)?,
162 input.bind_alpha_values(self.source.as_deref())?,
163 )?;
164 self.$base(encoding)
165 }
166 pub fn $keyed_data<I, S, B>(
168 self,
169 source: impl ArraySource,
170 keys: I,
171 input: impl AlphaValuesInput,
172 ) -> std::result::Result<Self, AdapterError>
173 where
174 I: IntoIterator<Item = (S, B)>,
175 S: AsRef<str>,
176 B: CategoryInput,
177 {
178 source.validate()?;
179 let encoding = AlphaEncoding::keyed(
180 crate::alpha::keys(Some(&source), keys)?,
181 input.bind_alpha_values(Some(&source))?,
182 )?;
183 self.$base(encoding)
184 }
185 pub fn $keyed_map<I, S, B>(
187 self,
188 keys: I,
189 input: impl NumericInput,
190 mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
191 ) -> std::result::Result<Self, AdapterError>
192 where
193 I: IntoIterator<Item = (S, B)>,
194 S: AsRef<str>,
195 B: CategoryInput,
196 {
197 let values = mapped_numeric(self.source.as_deref(), input, mapper, 1, None)?;
198 self.$keyed(keys, values)
199 }
200 pub fn $keyed_map_data<I, S, B>(
202 self,
203 source: impl ArraySource,
204 keys: I,
205 input: impl NumericInput,
206 mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
207 ) -> std::result::Result<Self, AdapterError>
208 where
209 I: IntoIterator<Item = (S, B)>,
210 S: AsRef<str>,
211 B: CategoryInput,
212 {
213 source.validate()?;
214 let values = mapped_numeric(Some(&source), input, mapper, 1, None)?;
215 self.$keyed_data(source, keys, values)
216 }
217 pub fn $keyed_categories<I, S, B>(
219 self,
220 keys: I,
221 input: impl CategoryInput,
222 mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
223 ) -> std::result::Result<Self, AdapterError>
224 where
225 I: IntoIterator<Item = (S, B)>,
226 S: AsRef<str>,
227 B: CategoryInput,
228 {
229 let values = mapped_categories(self.source.as_deref(), input, mapper, 1, None)?;
230 self.$keyed(keys, values)
231 }
232 pub fn $keyed_categories_data<I, S, B>(
234 self,
235 source: impl ArraySource,
236 keys: I,
237 input: impl CategoryInput,
238 mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
239 ) -> std::result::Result<Self, AdapterError>
240 where
241 I: IntoIterator<Item = (S, B)>,
242 S: AsRef<str>,
243 B: CategoryInput,
244 {
245 source.validate()?;
246 let values = mapped_categories(Some(&source), input, mapper, 1, None)?;
247 self.$keyed_data(source, keys, values)
248 }
249 pub fn $columns<I, S, B>(
251 self,
252 column_key: impl SourceRowsInput,
253 other_keys: I,
254 values: impl AlphaValuesInput,
255 ) -> std::result::Result<Self, AdapterError>
256 where
257 I: IntoIterator<Item = (S, B)>,
258 S: AsRef<str>,
259 B: CategoryInput,
260 {
261 let expansion =
262 self.bindings.expansion.as_ref().ok_or_else(|| {
263 binding("column alpha", "no generated variable dimension")
264 })?;
265 let mut keys = crate::alpha::keys(self.source.as_deref(), other_keys)?;
266 keys.push(expansion.column_key(self.source.as_deref(), column_key)?);
267 let encoding =
268 AlphaEncoding::keyed(keys, values.bind_alpha_values(self.source.as_deref())?)?;
269 self.$base(encoding)
270 }
271 pub fn $columns_data<I, S, B>(
273 self,
274 source: impl ArraySource,
275 column_key: impl SourceRowsInput,
276 other_keys: I,
277 values: impl AlphaValuesInput,
278 ) -> std::result::Result<Self, AdapterError>
279 where
280 I: IntoIterator<Item = (S, B)>,
281 S: AsRef<str>,
282 B: CategoryInput,
283 {
284 source.validate()?;
285 let expansion =
286 self.bindings.expansion.as_ref().ok_or_else(|| {
287 binding("column alpha", "no generated variable dimension")
288 })?;
289 let mut keys = crate::alpha::keys(Some(&source), other_keys)?;
290 keys.push(expansion.column_key(Some(&source), column_key)?);
291 let encoding =
292 AlphaEncoding::keyed(keys, values.bind_alpha_values(Some(&source))?)?;
293 self.$base(encoding)
294 }
295 pub fn $columns_map<I, S, B>(
297 self,
298 column_key: impl SourceRowsInput,
299 other_keys: I,
300 input: impl NumericInput,
301 mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
302 ) -> std::result::Result<Self, AdapterError>
303 where
304 I: IntoIterator<Item = (S, B)>,
305 S: AsRef<str>,
306 B: CategoryInput,
307 {
308 let values = mapped_numeric(self.source.as_deref(), input, mapper, 1, None)?;
309 self.$columns(column_key, other_keys, values)
310 }
311 pub fn $columns_map_data<I, S, B>(
313 self,
314 source: impl ArraySource,
315 column_key: impl SourceRowsInput,
316 other_keys: I,
317 input: impl NumericInput,
318 mapper: impl FnOnce(NumericValues) -> crate::Result<AlphaValues>,
319 ) -> std::result::Result<Self, AdapterError>
320 where
321 I: IntoIterator<Item = (S, B)>,
322 S: AsRef<str>,
323 B: CategoryInput,
324 {
325 source.validate()?;
326 let values = mapped_numeric(Some(&source), input, mapper, 1, None)?;
327 self.$columns_data(source, column_key, other_keys, values)
328 }
329 pub fn $columns_categories<I, S, B>(
331 self,
332 column_key: impl SourceRowsInput,
333 other_keys: I,
334 input: impl CategoryInput,
335 mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
336 ) -> std::result::Result<Self, AdapterError>
337 where
338 I: IntoIterator<Item = (S, B)>,
339 S: AsRef<str>,
340 B: CategoryInput,
341 {
342 let values = mapped_categories(self.source.as_deref(), input, mapper, 1, None)?;
343 self.$columns(column_key, other_keys, values)
344 }
345 pub fn $columns_categories_data<I, S, B>(
347 self,
348 source: impl ArraySource,
349 column_key: impl SourceRowsInput,
350 other_keys: I,
351 input: impl CategoryInput,
352 mapper: impl FnOnce(CategoryValues) -> crate::Result<AlphaValues>,
353 ) -> std::result::Result<Self, AdapterError>
354 where
355 I: IntoIterator<Item = (S, B)>,
356 S: AsRef<str>,
357 B: CategoryInput,
358 {
359 source.validate()?;
360 let values = mapped_categories(Some(&source), input, mapper, 1, None)?;
361 self.$columns_data(source, column_key, other_keys, values)
362 }
363 }
364 };
365}
366alpha_target!(
367 PointAlpha,
368 point_alpha,
369 point_alpha_cells,
370 point_alpha_linear,
371 point_alpha_map,
372 point_alpha_map_categories,
373 point_alpha_cells_map,
374 point_alpha_cells_map_categories,
375 point_alpha_single,
376 point_alpha_single_map,
377 point_alpha_single_map_categories,
378 point_alpha_keyed,
379 point_alpha_keyed_from_data,
380 point_alpha_keyed_map,
381 point_alpha_keyed_map_from_data,
382 point_alpha_keyed_map_categories,
383 point_alpha_keyed_map_categories_from_data,
384 point_alpha_columns,
385 point_alpha_columns_from_data,
386 point_alpha_columns_map,
387 point_alpha_columns_map_from_data,
388 point_alpha_columns_map_categories,
389 point_alpha_columns_map_categories_from_data
390);
391alpha_target!(
392 LineAlpha,
393 line_alpha,
394 line_alpha_cells,
395 line_alpha_linear,
396 line_alpha_map,
397 line_alpha_map_categories,
398 line_alpha_cells_map,
399 line_alpha_cells_map_categories,
400 line_alpha_single,
401 line_alpha_single_map,
402 line_alpha_single_map_categories,
403 line_alpha_keyed,
404 line_alpha_keyed_from_data,
405 line_alpha_keyed_map,
406 line_alpha_keyed_map_from_data,
407 line_alpha_keyed_map_categories,
408 line_alpha_keyed_map_categories_from_data,
409 line_alpha_columns,
410 line_alpha_columns_from_data,
411 line_alpha_columns_map,
412 line_alpha_columns_map_from_data,
413 line_alpha_columns_map_categories,
414 line_alpha_columns_map_categories_from_data
415);
416alpha_target!(
417 FillAlpha,
418 fill_alpha,
419 fill_alpha_cells,
420 fill_alpha_linear,
421 fill_alpha_map,
422 fill_alpha_map_categories,
423 fill_alpha_cells_map,
424 fill_alpha_cells_map_categories,
425 fill_alpha_single,
426 fill_alpha_single_map,
427 fill_alpha_single_map_categories,
428 fill_alpha_keyed,
429 fill_alpha_keyed_from_data,
430 fill_alpha_keyed_map,
431 fill_alpha_keyed_map_from_data,
432 fill_alpha_keyed_map_categories,
433 fill_alpha_keyed_map_categories_from_data,
434 fill_alpha_columns,
435 fill_alpha_columns_from_data,
436 fill_alpha_columns_map,
437 fill_alpha_columns_map_from_data,
438 fill_alpha_columns_map_categories,
439 fill_alpha_columns_map_categories_from_data
440);
441
442impl PlotAdapter<'_, kind::Histogram> {
443 pub fn fill_alpha_bins<I, S, B>(
445 self,
446 lower: impl NumericInput,
447 upper: impl NumericInput,
448 facets: I,
449 values: impl AlphaValuesInput,
450 ) -> std::result::Result<Self, AdapterError>
451 where
452 I: IntoIterator<Item = (S, B)>,
453 S: AsRef<str>,
454 B: CategoryInput,
455 {
456 let bounds = Bounds::new(
457 lower.bind_numeric(self.source.as_deref(), "bin lower")?,
458 upper.bind_numeric(self.source.as_deref(), "bin upper")?,
459 );
460 let encoding = AlphaEncoding::bins(
461 bounds,
462 crate::alpha::keys(self.source.as_deref(), facets)?,
463 values.bind_alpha_values(self.source.as_deref())?,
464 )?;
465 self.fill_alpha(encoding)
466 }
467 pub fn fill_alpha_bins_from_data<I, S, B>(
469 self,
470 source: impl ArraySource,
471 lower: impl NumericInput,
472 upper: impl NumericInput,
473 facets: I,
474 values: impl AlphaValuesInput,
475 ) -> std::result::Result<Self, AdapterError>
476 where
477 I: IntoIterator<Item = (S, B)>,
478 S: AsRef<str>,
479 B: CategoryInput,
480 {
481 source.validate()?;
482 let bounds = Bounds::new(
483 lower.bind_numeric(Some(&source), "bin lower")?,
484 upper.bind_numeric(Some(&source), "bin upper")?,
485 );
486 let encoding = AlphaEncoding::bins(
487 bounds,
488 crate::alpha::keys(Some(&source), facets)?,
489 values.bind_alpha_values(Some(&source))?,
490 )?;
491 self.fill_alpha(encoding)
492 }
493}
494macro_rules! bin_mapping {
495 ($name:ident,$from:ident,$input:ident,$value:ty,$map:ident) => {
496 impl PlotAdapter<'_, kind::Histogram> {
497 pub fn $name<I, S, B>(
499 self,
500 lower: impl NumericInput,
501 upper: impl NumericInput,
502 facets: I,
503 input: impl $input,
504 mapper: impl FnOnce($value) -> crate::Result<AlphaValues>,
505 ) -> std::result::Result<Self, AdapterError>
506 where
507 I: IntoIterator<Item = (S, B)>,
508 S: AsRef<str>,
509 B: CategoryInput,
510 {
511 let values = $map(self.source.as_deref(), input, mapper, 1, None)?;
512 self.fill_alpha_bins(lower, upper, facets, values)
513 }
514 pub fn $from<I, S, B>(
516 self,
517 source: impl ArraySource,
518 lower: impl NumericInput,
519 upper: impl NumericInput,
520 facets: I,
521 input: impl $input,
522 mapper: impl FnOnce($value) -> crate::Result<AlphaValues>,
523 ) -> std::result::Result<Self, AdapterError>
524 where
525 I: IntoIterator<Item = (S, B)>,
526 S: AsRef<str>,
527 B: CategoryInput,
528 {
529 source.validate()?;
530 let values = $map(Some(&source), input, mapper, 1, None)?;
531 self.fill_alpha_bins_from_data(source, lower, upper, facets, values)
532 }
533 }
534 };
535}
536bin_mapping!(
537 fill_alpha_bins_map,
538 fill_alpha_bins_map_from_data,
539 NumericInput,
540 NumericValues,
541 mapped_numeric
542);
543bin_mapping!(
544 fill_alpha_bins_map_categories,
545 fill_alpha_bins_map_categories_from_data,
546 CategoryInput,
547 CategoryValues,
548 mapped_categories
549);