lichtung_plot/builder.rs
1//! `lichtung-plot::builder` — the native `Plot` builder API.
2//!
3//! Semantic constructors accept immutable Rust-native values. Configuration is
4//! chained before `.svg(path)` or `.png(path)` borrows the configured plot.
5//!
6//! Significance annotations are caller-owned display inputs. The renderer never
7//! performs hypothesis tests or adjusts p-values.
8
9use std::marker::PhantomData;
10use std::sync::Arc;
11
12mod accessors;
13mod alpha;
14mod configuration;
15mod facets;
16mod layout;
17mod text_clear;
18mod text_layout;
19
20pub(crate) use layout::{measure_root_frame, RootFrameDomainInput};
21
22use crate::formatting::Labels;
23use crate::input::{AxisValues, PlotInput};
24use crate::layers::{BarPointLayer, GuideSpec, PointLayer};
25use crate::layout::AuthoredAxisSpec;
26use crate::spec::{
27 AxisScale, BarSpacing, CategoryOrder, DumbbellEndpoints, FacetSpec, HeatmapAnnotations,
28 HeatmapLayout, HeatmapNullRowAnnotation, HistogramBins, HistogramComposite, HistogramStat,
29 LegendPolicy, LinePathStyle, PlotKind, PlotTextAnnotation, QqLine, RawOverlay, RegressionBand,
30 WaterfallLabels,
31};
32use crate::theme::Theme;
33use crate::{
34 data::{Bounds, SourceRows},
35 plots::{BarInput, CoefficientInput, HistogramInput, RaincloudInput},
36 BooleanValues, CategoryValues, NumericFormat, NumericValues,
37};
38
39/// One configured semantic plot backed by immutable Rust-native values.
40///
41/// `K` is a zero-sized plot-family marker from [`crate::kind`]. Omitting it
42/// means [`crate::kind::Any`], the erased form used by heterogeneous storage.
43/// Family-specific methods are available only through exact-marker inherent
44/// implementations or a corresponding sealed capability.
45/// Without a sizing call, dimensions derive from content. [`Self::width`] fixes
46/// width with content-derived height; [`Self::size`] fixes both dimensions.
47/// The latest sizing call replaces the complete request. Final dimensions are
48/// inspected on [`crate::Output`], after the scene has been frozen.
49///
50/// ```compile_fail
51/// use lichtung_plot::{kind, Plot};
52/// fn removed_width_getter(plot: &Plot<kind::Scatter>) {
53/// let _ = plot.width();
54/// }
55/// ```
56///
57/// ```compile_fail
58/// use lichtung_plot::{kind, Plot};
59/// fn removed_height_getter(plot: &Plot<kind::Scatter>) {
60/// let _ = plot.height();
61/// }
62/// ```
63///
64/// ```compile_fail
65/// use lichtung_plot::{kind, Plot};
66/// fn removed_identity_alias(plot: &Plot<kind::Scatter>) {
67/// let _ = plot.plot_kind();
68/// }
69/// ```
70///
71/// ```compile_fail
72/// use lichtung_plot::{kind, Plot};
73/// fn internal_family_options(plot: &Plot<kind::Histogram>) {
74/// let _ = plot.histogram_bins_spec();
75/// }
76/// ```
77///
78/// ```compile_fail
79/// use lichtung_plot::{kind, Plot};
80/// fn internal_renderer_configuration(plot: &Plot<kind::Scatter>) {
81/// let _ = plot.x_axis_spec();
82/// }
83/// ```
84///
85/// ```compile_fail
86/// use lichtung_plot::{kind, Plot};
87/// fn internal_palette(plot: &Plot<kind::Scatter>) {
88/// let _ = plot.palette();
89/// }
90/// ```
91///
92/// ```compile_fail
93/// use lichtung_plot::{kind, Plot};
94/// fn internal_layout(plot: &Plot<kind::Scatter>) {
95/// let _ = plot.canvas_layout();
96/// }
97/// ```
98///
99/// ```compile_fail
100/// use lichtung_plot::{NumericValues, Plot};
101/// let plot = Plot::scatter(
102/// NumericValues::new("x", [1.0]),
103/// NumericValues::new("y", [2.0]),
104/// );
105/// let _ = plot.x_tick_count(3);
106/// ```
107///
108/// ```compile_fail
109/// use lichtung_plot::{NumericValues, Plot};
110/// let plot = Plot::scatter(
111/// NumericValues::new("x", [1.0]),
112/// NumericValues::new("y", [2.0]),
113/// );
114/// let _ = plot.y_tick_count(3);
115/// ```
116///
117/// ```compile_fail
118/// use lichtung_plot::{NumericValues, Plot, stats::HistogramBins};
119/// let plot = Plot::scatter(
120/// NumericValues::new("x", [1.0]),
121/// NumericValues::new("y", [2.0]),
122/// );
123/// let _ = plot.bins(HistogramBins::Count(4));
124/// ```
125///
126/// ```compile_fail
127/// use lichtung_plot::{NumericValues, Plot, plots::HistogramInput, stats::HistogramBins};
128/// let plot = Plot::histogram(HistogramInput::samples(
129/// NumericValues::new("value", [1.0]),
130/// )).erase();
131/// let _ = plot.bins(HistogramBins::Count(4));
132/// ```
133///
134/// ```compile_fail
135/// use lichtung_plot::{CategoryValues, NumericValues, Plot, plots::BarInput, scales::ContinuousColorScale};
136/// let plot = Plot::bar(
137/// CategoryValues::new("category", ["a"]).unwrap(),
138/// BarInput::estimates(NumericValues::new("value", [1.0])),
139/// );
140/// let _ = plot.continuous_color_scale(ContinuousColorScale::sequential(None, None).unwrap());
141/// ```
142///
143/// ```compile_fail
144/// use lichtung_plot::{NumericValues, Plot, plots::{HistogramInput, RawOverlay}};
145/// let plot = Plot::histogram(HistogramInput::samples(NumericValues::new("value", [1.0])));
146/// let _ = plot.raw_overlay(RawOverlay::Points);
147/// ```
148///
149/// ```compile_fail
150/// use lichtung_plot::{NumericValues, Plot, data::Bounds};
151/// let plot = Plot::regression(
152/// NumericValues::new("x", [1.0]),
153/// NumericValues::new("y", [2.0]),
154/// );
155/// let bounds = Bounds::new(
156/// NumericValues::new("lower", [0.0]),
157/// NumericValues::new("upper", [3.0]),
158/// );
159/// let _ = plot.band(bounds);
160/// ```
161///
162/// ```compile_fail
163/// use lichtung_plot::{NumericValues, Plot, elements::{Significance, SignificanceMark}};
164/// let plot = Plot::scatter(
165/// NumericValues::new("x", [1.0]),
166/// NumericValues::new("y", [2.0]),
167/// );
168/// let _ = plot.significance([
169/// Significance::at("a", SignificanceMark::OneStar),
170/// ]);
171/// ```
172///
173/// ```compile_fail
174/// use lichtung_plot::{NumericValues, Plot, elements::{Significance, SignificanceMark}};
175/// let plot = Plot::scatter(
176/// NumericValues::new("x", [1.0]),
177/// NumericValues::new("y", [2.0]),
178/// );
179/// let _ = plot.significance([
180/// Significance::between("a", "b", SignificanceMark::OneStar),
181/// ]);
182/// ```
183///
184/// ```compile_fail
185/// use lichtung_plot::{CategoryValues, NumericValues, Plot, elements::{Significance, SignificanceMark}};
186/// let plot = Plot::boxplot(
187/// CategoryValues::new("category", ["a"]).unwrap(),
188/// NumericValues::new("value", [1.0]),
189/// );
190/// let _ = plot.significance([
191/// Significance::at("a", SignificanceMark::OneStar),
192/// ]);
193/// ```
194///
195/// ```compile_fail
196/// use lichtung_plot::{
197/// CategoryValues,
198/// NumericValues,
199/// Plot,
200/// elements::{Significance, SignificanceMark},
201/// plots::CoefficientInput,
202/// };
203/// let plot = Plot::coefficient(
204/// CategoryValues::new("term", ["a", "b"]).unwrap(),
205/// CoefficientInput::estimates(NumericValues::new("estimate", [1.0, 2.0])),
206/// );
207/// let _ = plot.significance([
208/// Significance::between("a", "b", SignificanceMark::OneStar),
209/// ]);
210/// ```
211///
212/// ```compile_fail
213/// use lichtung_plot::{CategoryValues, NumericValues, Plot, plots::CoefficientInput};
214/// struct Wrapped(CoefficientInput);
215/// impl Into<CoefficientInput> for Wrapped {
216/// fn into(self) -> CoefficientInput { self.0 }
217/// }
218/// let _ = Plot::coefficient(
219/// CategoryValues::new("term", ["a"]).unwrap(),
220/// Wrapped(CoefficientInput::estimates(NumericValues::new("estimate", [1.0]))),
221/// );
222/// ```
223///
224/// ```compile_fail
225/// use lichtung_plot::{CategoryValues, NumericValues, Plot, data::Bounds};
226/// let plot = Plot::trajectory(
227/// NumericValues::new("x", [1.0, 2.0]),
228/// NumericValues::new("y", [2.0, 3.0]),
229/// CategoryValues::new("subject", ["a", "a"]).unwrap(),
230/// );
231/// let _ = plot.band(Bounds::new(
232/// NumericValues::new("lower", [1.0, 2.0]),
233/// NumericValues::new("upper", [3.0, 4.0]),
234/// ));
235/// ```
236///
237/// ```compile_fail
238/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
239/// let plot = Plot::trajectory(
240/// NumericValues::new("x", [1.0, 2.0]),
241/// NumericValues::new("y", [2.0, 3.0]),
242/// CategoryValues::new("subject", ["a", "a"]).unwrap(),
243/// );
244/// let _ = plot.show_markers(false);
245/// ```
246///
247/// ```compile_fail
248/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
249/// let plot = Plot::trajectory(
250/// NumericValues::new("x", [1.0, 2.0]),
251/// NumericValues::new("y", [2.0, 3.0]),
252/// CategoryValues::new("subject", ["a", "a"]).unwrap(),
253/// );
254/// let _ = plot.group(CategoryValues::new("replacement", ["b", "b"]).unwrap());
255/// ```
256///
257/// ```compile_fail
258/// use lichtung_plot::{CategoryValues, NumericValues, Plot, data::Bounds};
259/// let plot = Plot::line_path_grouped(
260/// NumericValues::new("x", [1.0, 2.0]),
261/// NumericValues::new("y", [2.0, 3.0]),
262/// CategoryValues::new("path", ["a", "a"]).unwrap(),
263/// );
264/// let _ = plot.band(Bounds::new(
265/// NumericValues::new("lower", [1.0, 2.0]),
266/// NumericValues::new("upper", [3.0, 4.0]),
267/// ));
268/// ```
269///
270/// ```compile_fail
271/// use lichtung_plot::{CategoryValues, NumericValues, Plot, elements::PointLayer};
272/// let plot = Plot::line_path_grouped(
273/// NumericValues::new("x", [1.0, 2.0]),
274/// NumericValues::new("y", [2.0, 3.0]),
275/// CategoryValues::new("path", ["a", "a"]).unwrap(),
276/// );
277/// let layer = PointLayer::new(
278/// NumericValues::new("x", [1.5]),
279/// NumericValues::new("y", [2.5]),
280/// );
281/// let _ = plot.point_layer(layer);
282/// ```
283///
284/// ```compile_fail
285/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
286/// let plot = Plot::line_path_grouped(
287/// NumericValues::new("x", [1.0, 2.0]),
288/// NumericValues::new("y", [2.0, 3.0]),
289/// CategoryValues::new("path", ["a", "a"]).unwrap(),
290/// );
291/// let _ = plot.group(CategoryValues::new("replacement", ["b", "b"]).unwrap());
292/// ```
293///
294/// ```compile_fail
295/// use lichtung_plot::{CategoryValues, NumericValues, Plot, data::Bounds};
296/// let plot = Plot::interaction(
297/// CategoryValues::new("condition", ["before", "after"]).unwrap(),
298/// NumericValues::new("estimate", [1.0, 2.0]),
299/// CategoryValues::new("group", ["a", "a"]).unwrap(),
300/// Bounds::new(
301/// NumericValues::new("lower", [0.5, 1.5]),
302/// NumericValues::new("upper", [1.5, 2.5]),
303/// ),
304/// );
305/// let _ = plot.group(CategoryValues::new("replacement", ["b", "b"]).unwrap());
306/// ```
307///
308/// ```compile_fail
309/// use lichtung_plot::{NumericValues, Plot, plots::LinePathStyle};
310/// let plot = Plot::line(
311/// NumericValues::new("x", [1.0, 2.0]),
312/// NumericValues::new("y", [2.0, 3.0]),
313/// );
314/// let _ = plot.style(LinePathStyle::Hue);
315/// ```
316///
317/// ```compile_fail
318/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
319/// let _ = Plot::line(
320/// CategoryValues::new("x", ["before", "after"]).unwrap(),
321/// NumericValues::new("y", [1.0, 2.0]),
322/// );
323/// ```
324///
325/// ```compile_fail
326/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
327/// let _ = Plot::dumbbell(
328/// CategoryValues::new("category", ["a"]).unwrap(),
329/// NumericValues::new("left", [1.0]),
330/// NumericValues::new("right", [2.0]),
331/// );
332/// ```
333///
334/// ```compile_fail
335/// use lichtung_plot::{CategoryValues, NumericValues, Plot, plots::BarSpacing};
336/// let plot = Plot::stacked_bar(
337/// CategoryValues::new("category", ["a"]).unwrap(),
338/// NumericValues::new("value", [1.0]),
339/// CategoryValues::new("group", ["g"]).unwrap(),
340/// );
341/// let _ = plot.spacing(BarSpacing::Compact);
342/// ```
343///
344/// ```compile_fail
345/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
346/// let plot = Plot::stacked_bar(
347/// CategoryValues::new("category", ["a"]).unwrap(),
348/// NumericValues::new("value", [1.0]),
349/// CategoryValues::new("series", ["g"]).unwrap(),
350/// );
351/// let _ = plot.series(CategoryValues::new("replacement", ["h"]).unwrap());
352/// ```
353///
354/// ```compile_fail
355/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
356/// let plot = Plot::stacked_horizontal_bar(
357/// CategoryValues::new("category", ["a"]).unwrap(),
358/// NumericValues::new("value", [1.0]),
359/// CategoryValues::new("series", ["g"]).unwrap(),
360/// );
361/// let _ = plot.series(CategoryValues::new("replacement", ["h"]).unwrap());
362/// ```
363///
364/// ```compile_fail
365/// use lichtung_plot::{CategoryValues, NumericValues, Plot, plots::BarInput};
366/// let plot = Plot::bar(
367/// CategoryValues::new("category", ["a"]).unwrap(),
368/// BarInput::estimates(NumericValues::new("value", [1.0])),
369/// );
370/// let _ = plot.group(CategoryValues::new("series", ["g"]).unwrap());
371/// ```
372///
373/// ```compile_fail
374/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
375/// let plot = Plot::horizontal_bar(
376/// CategoryValues::new("category", ["a"]).unwrap(),
377/// lichtung_plot::plots::BarInput::estimates(NumericValues::new("value", [1.0])),
378/// );
379/// let _ = plot.hue(CategoryValues::new("series", ["g"]).unwrap());
380/// ```
381///
382/// ```compile_fail
383/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
384/// let plot = Plot::boxplot(
385/// CategoryValues::new("category", ["a"]).unwrap(),
386/// NumericValues::new("value", [1.0]),
387/// );
388/// let _ = plot.group(CategoryValues::new("series", ["g"]).unwrap());
389/// ```
390///
391/// ```compile_fail
392/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
393/// let plot = Plot::violin(
394/// CategoryValues::new("category", ["a", "a"]).unwrap(),
395/// NumericValues::new("value", [1.0, 2.0]),
396/// );
397/// let _ = plot.hue(CategoryValues::new("series", ["g", "g"]).unwrap());
398/// ```
399///
400/// ```compile_fail
401/// use lichtung_plot::{CategoryValues, NumericValues, Plot, plots::RaincloudInput};
402/// let plot = Plot::raincloud(RaincloudInput::samples(
403/// CategoryValues::new("category", ["a", "a"]).unwrap(),
404/// NumericValues::new("value", [1.0, 2.0]),
405/// ));
406/// let _ = plot.hue(CategoryValues::new("series", ["g", "g"]).unwrap());
407/// ```
408///
409/// ```compile_fail
410/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
411/// let plot = Plot::line(
412/// NumericValues::new("x", [1.0, 2.0]),
413/// NumericValues::new("y", [2.0, 3.0]),
414/// );
415/// let _ = plot.series(CategoryValues::new("series", ["g", "g"]).unwrap());
416/// ```
417///
418/// ```compile_fail
419/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
420/// let plot = Plot::heatmap(
421/// CategoryValues::new("x", ["a"]).unwrap(),
422/// CategoryValues::new("y", ["b"]).unwrap(),
423/// NumericValues::new("fill", [1.0]),
424/// );
425/// let _ = plot.color(NumericValues::new("replacement", [2.0]));
426/// ```
427///
428/// ```compile_fail
429/// use lichtung_plot::{CategoryValues, NumericValues, Plot};
430/// let plot = Plot::beeswarm_colored(
431/// CategoryValues::new("category", ["a"]).unwrap(),
432/// NumericValues::new("value", [1.0]),
433/// NumericValues::new("color", [2.0]),
434/// );
435/// let _ = plot.color(NumericValues::new("replacement", [3.0]));
436/// ```
437///
438/// ```compile_fail
439/// use lichtung_plot::{CategoryValues, NumericValues, Plot, layout::FacetSpec};
440/// let plot = Plot::matrix(vec![
441/// NumericValues::new("x", [1.0]),
442/// NumericValues::new("y", [2.0]),
443/// ]);
444/// let _ = plot.facet(FacetSpec::column(
445/// CategoryValues::new("panel", ["a"]).unwrap(),
446/// ));
447/// ```
448///
449/// ```compile_fail
450/// use lichtung_plot::{CategoryValues, NumericValues, Plot, layout::FacetSpec};
451/// let plot = Plot::marginal(
452/// NumericValues::new("x", [1.0]),
453/// NumericValues::new("y", [2.0]),
454/// );
455/// let _ = plot.facet(FacetSpec::column(
456/// CategoryValues::new("panel", ["a"]).unwrap(),
457/// ));
458/// ```
459#[derive(Clone)]
460pub struct Plot<K: crate::kind::Marker = crate::kind::Any> {
461 description: PlotDescription,
462 marker: PhantomData<K>,
463}
464
465pub(crate) mod data_access;
466mod description;
467pub(crate) use description::PlotDescription;
468
469#[derive(Clone, Default)]
470struct CategoryOrders {
471 primary: CategoryOrder,
472 x: CategoryOrder,
473 y: CategoryOrder,
474}
475
476/// Coordinated replacements and complete numeric policies for a fixed child owner.
477#[expect(
478 clippy::type_complexity,
479 reason = "the original narrow axis snapshots retain only comparison values, not authored or working axis owners"
480)]
481pub(crate) struct ComposedInputs {
482 output_dimensions: Option<(f64, f64)>,
483 text_layouts: crate::text::roles::TextRoles,
484 prose: [crate::Text; 3],
485 axis_titles: [crate::Text; 2],
486 x_axis_spec: (
487 Option<(f64, f64)>,
488 crate::layout::RangeProvenance,
489 crate::coordinates::TickGeneration,
490 crate::coordinates::CategoryLabelSelection,
491 Option<Arc<NumericFormat>>,
492 Option<Vec<crate::coordinates::AxisTick>>,
493 ),
494 y_axis_spec: (
495 Option<(f64, f64)>,
496 crate::layout::RangeProvenance,
497 crate::coordinates::TickGeneration,
498 crate::coordinates::CategoryLabelSelection,
499 Option<Arc<NumericFormat>>,
500 Option<Vec<crate::coordinates::AxisTick>>,
501 ),
502 resolved_facet_x_range: Option<(f64, f64)>,
503 global_hue_levels: Option<Vec<String>>,
504 global_group_levels: Option<Vec<String>>,
505 global_collective_pairs: Option<Vec<crate::data::CollectivePair>>,
506 global_line_condition_levels: Option<Vec<String>>,
507 axis_gutter_owner: crate::orchestrator::registration::GutterOwner,
508 y_tick_labels_visible: bool,
509 render_scope: Option<String>,
510 legend_policy: LegendPolicy,
511 suppress_continuous_guide: bool,
512 root_size: crate::layout::RootSize,
513 numeric: crate::numeric::inputs::NumericInputs,
514 alpha: crate::alpha::AlphaInputs,
515 pub(crate) guides: crate::decorations::legend::discrete::scoped::ScopedGuideInputs,
516}
517
518impl Plot<crate::kind::Any> {
519 fn new<K: crate::kind::Marker>(input: PlotInput, kind: PlotKind) -> Plot<K> {
520 Plot {
521 description: PlotDescription::new(input, kind),
522 marker: PhantomData,
523 }
524 }
525
526 /// Construct a scatter plot.
527 #[must_use]
528 pub fn scatter(x: NumericValues, y: NumericValues) -> Plot<crate::kind::Scatter> {
529 Self::new(PlotInput::Scatter { x, y }, PlotKind::Scatter)
530 }
531
532 /// Construct a histogram.
533 #[must_use]
534 pub fn histogram(input: HistogramInput) -> Plot<crate::kind::Histogram> {
535 Self::new(PlotInput::Histogram { input }, PlotKind::Histogram)
536 }
537
538 /// Construct a Gaussian density plot.
539 #[must_use]
540 pub fn density(values: NumericValues) -> Plot<crate::kind::Density> {
541 Self::new(PlotInput::Density { values }, PlotKind::Density)
542 }
543
544 /// Construct a normal Q-Q plot.
545 #[must_use]
546 pub fn qq(values: NumericValues) -> Plot<crate::kind::Qq> {
547 Self::new(PlotInput::Qq { values }, PlotKind::Qq)
548 }
549
550 /// Construct a point-interval plot.
551 #[must_use]
552 pub fn point_interval(
553 category: CategoryValues,
554 estimate: NumericValues,
555 bounds: Bounds,
556 ) -> Plot<crate::kind::PointInterval> {
557 Self::new(
558 PlotInput::PointInterval {
559 estimate,
560 category,
561 bounds,
562 },
563 PlotKind::PointInterval,
564 )
565 }
566
567 /// Construct an ordered line plot.
568 #[must_use]
569 pub fn line(x: NumericValues, y: NumericValues) -> Plot<crate::kind::Line> {
570 Self::new(
571 PlotInput::Line {
572 x: AxisValues::Numeric(x),
573 y,
574 },
575 PlotKind::Line,
576 )
577 }
578
579 /// Construct explicit-group repeated trajectories.
580 #[must_use]
581 pub fn trajectory(
582 x: impl Into<AxisValues>,
583 y: NumericValues,
584 group: CategoryValues,
585 ) -> Plot<crate::kind::Trajectory> {
586 Self::new(
587 PlotInput::Trajectory {
588 x: x.into(),
589 y,
590 group,
591 },
592 PlotKind::Trajectory,
593 )
594 }
595
596 /// Construct a single numeric path in source-row order, with no group key.
597 #[must_use]
598 pub fn line_path(x: NumericValues, y: NumericValues) -> Plot<crate::kind::LinePath> {
599 Self::new(
600 PlotInput::LinePath { x, y, group: None },
601 PlotKind::LinePath,
602 )
603 }
604
605 /// Construct separately connected numeric paths using explicit group keys.
606 #[must_use]
607 pub fn line_path_grouped(
608 x: NumericValues,
609 y: NumericValues,
610 group: CategoryValues,
611 ) -> Plot<crate::kind::LinePath> {
612 Self::new(
613 PlotInput::LinePath {
614 x,
615 y,
616 group: Some(group),
617 },
618 PlotKind::LinePath,
619 )
620 }
621
622 /// Construct a precomputed interaction-profile plot.
623 #[must_use]
624 pub fn interaction(
625 condition: CategoryValues,
626 estimate: NumericValues,
627 group: CategoryValues,
628 bounds: Bounds,
629 ) -> Plot<crate::kind::Interaction> {
630 Self::new(
631 PlotInput::Interaction {
632 condition,
633 estimate,
634 group,
635 bounds,
636 },
637 PlotKind::Interaction,
638 )
639 }
640
641 /// Construct a model-coefficient plot.
642 #[must_use]
643 pub fn coefficient(
644 term: CategoryValues,
645 input: CoefficientInput,
646 ) -> Plot<crate::kind::Coefficient> {
647 Self::new(
648 PlotInput::Coefficient { input, term },
649 PlotKind::Coefficient,
650 )
651 }
652
653 /// Construct a forest plot with complete bounds and a scientific reference.
654 #[must_use]
655 pub fn forest(
656 term: CategoryValues,
657 estimate: NumericValues,
658 bounds: Bounds,
659 reference: f64,
660 ) -> Plot<crate::kind::Forest> {
661 Self::new(
662 PlotInput::Forest {
663 estimate,
664 term,
665 bounds,
666 reference,
667 },
668 PlotKind::Forest,
669 )
670 }
671
672 /// Construct a horizontal paired-endpoint dumbbell plot.
673 #[must_use]
674 pub fn dumbbell(
675 category: CategoryValues,
676 left: NumericValues,
677 right: NumericValues,
678 endpoints: DumbbellEndpoints,
679 ) -> Plot<crate::kind::Dumbbell> {
680 Self::new(
681 PlotInput::Dumbbell {
682 category,
683 left,
684 right,
685 endpoints,
686 },
687 PlotKind::Dumbbell,
688 )
689 }
690
691 /// Construct a categorical heatmap.
692 #[must_use]
693 pub fn heatmap(
694 x: CategoryValues,
695 y: CategoryValues,
696 fill: NumericValues,
697 ) -> Plot<crate::kind::Heatmap> {
698 Self::new(PlotInput::Heatmap { x, y, fill }, PlotKind::Heatmap)
699 }
700
701 /// Construct a vertical bar plot.
702 #[must_use]
703 pub fn bar(category: CategoryValues, input: impl Into<BarInput>) -> Plot<crate::kind::Bar> {
704 Self::new(
705 PlotInput::Bar {
706 category,
707 input: input.into(),
708 },
709 PlotKind::Bar,
710 )
711 }
712
713 /// Construct a horizontal bar plot.
714 #[must_use]
715 pub fn horizontal_bar(
716 category: CategoryValues,
717 input: impl Into<BarInput>,
718 ) -> Plot<crate::kind::HorizontalBar> {
719 Self::new(
720 PlotInput::HorizontalBar {
721 category,
722 input: input.into(),
723 },
724 PlotKind::HorizontalBar,
725 )
726 }
727
728 /// Construct a stacked vertical bar plot.
729 #[must_use]
730 pub fn stacked_bar(
731 category: CategoryValues,
732 value: NumericValues,
733 series: CategoryValues,
734 ) -> Plot<crate::kind::StackedBar> {
735 Self::new(
736 PlotInput::StackedBar {
737 category,
738 value,
739 series,
740 },
741 PlotKind::StackedBar,
742 )
743 }
744
745 /// Construct a stacked horizontal bar plot.
746 #[must_use]
747 pub fn stacked_horizontal_bar(
748 category: CategoryValues,
749 value: NumericValues,
750 series: CategoryValues,
751 ) -> Plot<crate::kind::StackedHorizontalBar> {
752 Self::new(
753 PlotInput::StackedHorizontalBar {
754 category,
755 value,
756 series,
757 },
758 PlotKind::StackedHorizontalBar,
759 )
760 }
761
762 /// Construct a box-and-whisker plot.
763 #[must_use]
764 pub fn boxplot(category: CategoryValues, value: NumericValues) -> Plot<crate::kind::Boxplot> {
765 Self::new(PlotInput::Boxplot { category, value }, PlotKind::Boxplot)
766 }
767
768 /// Construct a violin plot.
769 #[must_use]
770 pub fn violin(category: CategoryValues, value: NumericValues) -> Plot<crate::kind::Violin> {
771 Self::new(PlotInput::Violin { category, value }, PlotKind::Violin)
772 }
773
774 /// Construct a raincloud plot.
775 #[must_use]
776 pub fn raincloud(input: RaincloudInput) -> Plot<crate::kind::Raincloud> {
777 Self::new(PlotInput::Raincloud { input }, PlotKind::Raincloud)
778 }
779
780 /// Construct a regression plot.
781 #[must_use]
782 pub fn regression(x: NumericValues, y: NumericValues) -> Plot<crate::kind::Regression> {
783 Self::new(PlotInput::Regression { x, y }, PlotKind::Regression)
784 }
785
786 /// Construct a marginal plot.
787 #[must_use]
788 pub fn marginal(x: NumericValues, y: NumericValues) -> Plot<crate::kind::Marginal> {
789 Self::new(PlotInput::Marginal { x, y }, PlotKind::Marginal)
790 }
791
792 /// Construct a scatter matrix from ordered named numeric variables.
793 #[must_use]
794 pub fn matrix(variables: Vec<NumericValues>) -> Plot<crate::kind::Matrix> {
795 Self::new(
796 PlotInput::Matrix {
797 variables: crate::data::VariableValues::from_columns(variables),
798 },
799 PlotKind::Matrix,
800 )
801 }
802
803 /// Construct a categorical beeswarm with the theme's default point color.
804 #[must_use]
805 pub fn beeswarm(category: CategoryValues, value: NumericValues) -> Plot<crate::kind::Beeswarm> {
806 Self::new(
807 PlotInput::Beeswarm {
808 value,
809 category,
810 color: None,
811 },
812 PlotKind::Beeswarm,
813 )
814 }
815
816 /// Construct a beeswarm with aligned numeric colors or a complete fixed color.
817 #[must_use]
818 pub fn beeswarm_colored(
819 category: CategoryValues,
820 value: NumericValues,
821 color: impl Into<crate::scales::ColorSource>,
822 ) -> Plot<crate::kind::Beeswarm> {
823 Self::new(
824 PlotInput::Beeswarm {
825 value,
826 category,
827 color: Some(color.into()),
828 },
829 PlotKind::Beeswarm,
830 )
831 }
832
833 /// Construct a cumulative signed-delta waterfall plot.
834 #[must_use]
835 pub fn waterfall(
836 labels: CategoryValues,
837 deltas: NumericValues,
838 base: f64,
839 ) -> Plot<crate::kind::Waterfall> {
840 Self::new(
841 PlotInput::Waterfall {
842 labels,
843 deltas,
844 base,
845 },
846 PlotKind::Waterfall,
847 )
848 }
849}
850
851impl<K: crate::kind::ExactFamily> Plot<K> {
852 /// Consume this plot and erase its compile-time family marker.
853 ///
854 /// Every owned input and configuration field is moved unchanged.
855 ///
856 /// An erased plot cannot be erased a second time:
857 ///
858 /// ```compile_fail
859 /// use lichtung_plot::{NumericValues, Plot};
860 /// let erased: Plot = Plot::scatter(
861 /// NumericValues::new("x", [1.0]),
862 /// NumericValues::new("y", [2.0]),
863 /// ).erase();
864 /// let _ = erased.erase();
865 /// ```
866 #[must_use]
867 pub fn erase(self) -> Plot<crate::kind::Any> {
868 Plot {
869 description: self.description,
870 marker: PhantomData,
871 }
872 }
873}