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