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