Skip to main content

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) mod recipe;
468pub(crate) use description::PlotDescription;
469
470#[derive(Clone, Default)]
471struct CategoryOrders {
472    primary: CategoryOrder,
473    x: CategoryOrder,
474    y: CategoryOrder,
475}
476
477/// Coordinated replacements and complete numeric policies for a fixed child owner.
478#[expect(
479    clippy::type_complexity,
480    reason = "the original narrow axis snapshots retain only comparison values, not authored or working axis owners"
481)]
482pub(crate) struct ComposedInputs {
483    output_dimensions: Option<(f64, f64)>,
484    text_layouts: crate::text::roles::TextRoles,
485    prose: [crate::Text; 3],
486    axis_titles: [crate::Text; 2],
487    x_axis_spec: (
488        Option<(f64, f64)>,
489        crate::layout::RangeProvenance,
490        crate::coordinates::TickGeneration,
491        crate::coordinates::CategoryLabelSelection,
492        Option<Arc<NumericFormat>>,
493        Option<Vec<crate::coordinates::AxisTick>>,
494    ),
495    y_axis_spec: (
496        Option<(f64, f64)>,
497        crate::layout::RangeProvenance,
498        crate::coordinates::TickGeneration,
499        crate::coordinates::CategoryLabelSelection,
500        Option<Arc<NumericFormat>>,
501        Option<Vec<crate::coordinates::AxisTick>>,
502    ),
503    resolved_facet_x_range: Option<(f64, f64)>,
504    global_hue_levels: Option<Vec<String>>,
505    global_group_levels: Option<Vec<String>>,
506    global_collective_pairs: Option<Vec<crate::data::CollectivePair>>,
507    global_line_condition_levels: Option<Vec<String>>,
508    axis_gutter_owner: crate::orchestrator::registration::GutterOwner,
509    y_tick_labels_visible: bool,
510    render_scope: Option<String>,
511    legend_policy: LegendPolicy,
512    suppress_continuous_guide: bool,
513    root_size: crate::layout::RootSize,
514    numeric: crate::numeric::inputs::NumericInputs,
515    alpha: crate::alpha::AlphaInputs,
516    pub(crate) guides: crate::decorations::legend::discrete::scoped::ScopedGuideInputs,
517}
518
519impl Plot<crate::kind::Any> {
520    fn new<K: crate::kind::Marker>(input: PlotInput, kind: PlotKind) -> Plot<K> {
521        Plot {
522            description: PlotDescription::new(input, kind),
523            marker: PhantomData,
524        }
525    }
526
527    /// Construct a scatter plot.
528    #[must_use]
529    pub fn scatter(x: NumericValues, y: NumericValues) -> Plot<crate::kind::Scatter> {
530        Self::new(PlotInput::Scatter { x, y }, PlotKind::Scatter)
531    }
532
533    /// Construct a histogram.
534    #[must_use]
535    pub fn histogram(input: HistogramInput) -> Plot<crate::kind::Histogram> {
536        Self::new(PlotInput::Histogram { input }, PlotKind::Histogram)
537    }
538
539    /// Construct a Gaussian density plot.
540    #[must_use]
541    pub fn density(values: NumericValues) -> Plot<crate::kind::Density> {
542        Self::new(PlotInput::Density { values }, PlotKind::Density)
543    }
544
545    /// Construct a normal Q-Q plot.
546    #[must_use]
547    pub fn qq(values: NumericValues) -> Plot<crate::kind::Qq> {
548        Self::new(PlotInput::Qq { values }, PlotKind::Qq)
549    }
550
551    /// Construct a point-interval plot.
552    #[must_use]
553    pub fn point_interval(
554        category: CategoryValues,
555        estimate: NumericValues,
556        bounds: Bounds,
557    ) -> Plot<crate::kind::PointInterval> {
558        Self::new(
559            PlotInput::PointInterval {
560                estimate,
561                category,
562                bounds,
563            },
564            PlotKind::PointInterval,
565        )
566    }
567
568    /// Construct an ordered line plot.
569    #[must_use]
570    pub fn line(x: NumericValues, y: NumericValues) -> Plot<crate::kind::Line> {
571        Self::new(
572            PlotInput::Line {
573                x: AxisValues::Numeric(x),
574                y,
575            },
576            PlotKind::Line,
577        )
578    }
579
580    /// Construct explicit-group repeated trajectories.
581    #[must_use]
582    pub fn trajectory(
583        x: impl Into<AxisValues>,
584        y: NumericValues,
585        group: CategoryValues,
586    ) -> Plot<crate::kind::Trajectory> {
587        Self::new(
588            PlotInput::Trajectory {
589                x: x.into(),
590                y,
591                group,
592            },
593            PlotKind::Trajectory,
594        )
595    }
596
597    /// Construct a single numeric path in source-row order, with no group key.
598    #[must_use]
599    pub fn line_path(x: NumericValues, y: NumericValues) -> Plot<crate::kind::LinePath> {
600        Self::new(
601            PlotInput::LinePath { x, y, group: None },
602            PlotKind::LinePath,
603        )
604    }
605
606    /// Construct separately connected numeric paths using explicit group keys.
607    #[must_use]
608    pub fn line_path_grouped(
609        x: NumericValues,
610        y: NumericValues,
611        group: CategoryValues,
612    ) -> Plot<crate::kind::LinePath> {
613        Self::new(
614            PlotInput::LinePath {
615                x,
616                y,
617                group: Some(group),
618            },
619            PlotKind::LinePath,
620        )
621    }
622
623    /// Construct a precomputed interaction-profile plot.
624    #[must_use]
625    pub fn interaction(
626        condition: CategoryValues,
627        estimate: NumericValues,
628        group: CategoryValues,
629        bounds: Bounds,
630    ) -> Plot<crate::kind::Interaction> {
631        Self::new(
632            PlotInput::Interaction {
633                condition,
634                estimate,
635                group,
636                bounds,
637            },
638            PlotKind::Interaction,
639        )
640    }
641
642    /// Construct a model-coefficient plot.
643    #[must_use]
644    pub fn coefficient(
645        term: CategoryValues,
646        input: CoefficientInput,
647    ) -> Plot<crate::kind::Coefficient> {
648        Self::new(
649            PlotInput::Coefficient { input, term },
650            PlotKind::Coefficient,
651        )
652    }
653
654    /// Construct a forest plot with complete bounds and a scientific reference.
655    #[must_use]
656    pub fn forest(
657        term: CategoryValues,
658        estimate: NumericValues,
659        bounds: Bounds,
660        reference: f64,
661    ) -> Plot<crate::kind::Forest> {
662        Self::new(
663            PlotInput::Forest {
664                estimate,
665                term,
666                bounds,
667                reference,
668            },
669            PlotKind::Forest,
670        )
671    }
672
673    /// Construct a horizontal paired-endpoint dumbbell plot.
674    #[must_use]
675    pub fn dumbbell(
676        category: CategoryValues,
677        left: NumericValues,
678        right: NumericValues,
679        endpoints: DumbbellEndpoints,
680    ) -> Plot<crate::kind::Dumbbell> {
681        Self::new(
682            PlotInput::Dumbbell {
683                category,
684                left,
685                right,
686                endpoints,
687            },
688            PlotKind::Dumbbell,
689        )
690    }
691
692    /// Construct a categorical heatmap.
693    #[must_use]
694    pub fn heatmap(
695        x: CategoryValues,
696        y: CategoryValues,
697        fill: NumericValues,
698    ) -> Plot<crate::kind::Heatmap> {
699        Self::new(PlotInput::Heatmap { x, y, fill }, PlotKind::Heatmap)
700    }
701
702    /// Construct a vertical bar plot.
703    #[must_use]
704    pub fn bar(category: CategoryValues, input: impl Into<BarInput>) -> Plot<crate::kind::Bar> {
705        Self::new(
706            PlotInput::Bar {
707                category,
708                input: input.into(),
709            },
710            PlotKind::Bar,
711        )
712    }
713
714    /// Construct a horizontal bar plot.
715    #[must_use]
716    pub fn horizontal_bar(
717        category: CategoryValues,
718        input: impl Into<BarInput>,
719    ) -> Plot<crate::kind::HorizontalBar> {
720        Self::new(
721            PlotInput::HorizontalBar {
722                category,
723                input: input.into(),
724            },
725            PlotKind::HorizontalBar,
726        )
727    }
728
729    /// Construct a stacked vertical bar plot.
730    #[must_use]
731    pub fn stacked_bar(
732        category: CategoryValues,
733        value: NumericValues,
734        series: CategoryValues,
735    ) -> Plot<crate::kind::StackedBar> {
736        Self::new(
737            PlotInput::StackedBar {
738                category,
739                value,
740                series,
741            },
742            PlotKind::StackedBar,
743        )
744    }
745
746    /// Construct a stacked horizontal bar plot.
747    #[must_use]
748    pub fn stacked_horizontal_bar(
749        category: CategoryValues,
750        value: NumericValues,
751        series: CategoryValues,
752    ) -> Plot<crate::kind::StackedHorizontalBar> {
753        Self::new(
754            PlotInput::StackedHorizontalBar {
755                category,
756                value,
757                series,
758            },
759            PlotKind::StackedHorizontalBar,
760        )
761    }
762
763    /// Construct a box-and-whisker plot.
764    #[must_use]
765    pub fn boxplot(category: CategoryValues, value: NumericValues) -> Plot<crate::kind::Boxplot> {
766        Self::new(PlotInput::Boxplot { category, value }, PlotKind::Boxplot)
767    }
768
769    /// Construct a violin plot.
770    #[must_use]
771    pub fn violin(category: CategoryValues, value: NumericValues) -> Plot<crate::kind::Violin> {
772        Self::new(PlotInput::Violin { category, value }, PlotKind::Violin)
773    }
774
775    /// Construct a raincloud plot.
776    #[must_use]
777    pub fn raincloud(input: RaincloudInput) -> Plot<crate::kind::Raincloud> {
778        Self::new(PlotInput::Raincloud { input }, PlotKind::Raincloud)
779    }
780
781    /// Construct a regression plot.
782    #[must_use]
783    pub fn regression(x: NumericValues, y: NumericValues) -> Plot<crate::kind::Regression> {
784        Self::new(PlotInput::Regression { x, y }, PlotKind::Regression)
785    }
786
787    /// Construct a marginal plot.
788    #[must_use]
789    pub fn marginal(x: NumericValues, y: NumericValues) -> Plot<crate::kind::Marginal> {
790        Self::new(PlotInput::Marginal { x, y }, PlotKind::Marginal)
791    }
792
793    /// Construct a scatter matrix from ordered named numeric variables.
794    #[must_use]
795    pub fn matrix(variables: Vec<NumericValues>) -> Plot<crate::kind::Matrix> {
796        Self::new(
797            PlotInput::Matrix {
798                variables: crate::data::VariableValues::from_columns(variables),
799            },
800            PlotKind::Matrix,
801        )
802    }
803
804    /// Construct a categorical beeswarm with the theme's default point color.
805    #[must_use]
806    pub fn beeswarm(category: CategoryValues, value: NumericValues) -> Plot<crate::kind::Beeswarm> {
807        Self::new(
808            PlotInput::Beeswarm {
809                value,
810                category,
811                color: None,
812            },
813            PlotKind::Beeswarm,
814        )
815    }
816
817    /// Construct a beeswarm with aligned numeric colors or a complete fixed color.
818    #[must_use]
819    pub fn beeswarm_colored(
820        category: CategoryValues,
821        value: NumericValues,
822        color: impl Into<crate::scales::ColorSource>,
823    ) -> Plot<crate::kind::Beeswarm> {
824        Self::new(
825            PlotInput::Beeswarm {
826                value,
827                category,
828                color: Some(color.into()),
829            },
830            PlotKind::Beeswarm,
831        )
832    }
833
834    /// Construct a cumulative signed-delta waterfall plot.
835    #[must_use]
836    pub fn waterfall(
837        labels: CategoryValues,
838        deltas: NumericValues,
839        base: f64,
840    ) -> Plot<crate::kind::Waterfall> {
841        Self::new(
842            PlotInput::Waterfall {
843                labels,
844                deltas,
845                base,
846            },
847            PlotKind::Waterfall,
848        )
849    }
850}
851
852impl<K: crate::kind::ExactFamily> Plot<K> {
853    /// Consume this plot and erase its compile-time family marker.
854    ///
855    /// Every owned input and configuration field is moved unchanged.
856    ///
857    /// An erased plot cannot be erased a second time:
858    ///
859    /// ```compile_fail
860    /// use lichtung_plot::{NumericValues, Plot};
861    /// let erased: Plot = Plot::scatter(
862    ///     NumericValues::new("x", [1.0]),
863    ///     NumericValues::new("y", [2.0]),
864    /// ).erase();
865    /// let _ = erased.erase();
866    /// ```
867    #[must_use]
868    pub fn erase(self) -> Plot<crate::kind::Any> {
869        Plot {
870            description: self.description,
871            marker: PhantomData,
872        }
873    }
874}