Skip to main content

lichtung_plot/builder/
configuration.rs

1use super::Plot;
2use crate::layers::{BarPointLayer, GuideSpec, PointLayer};
3use crate::layout::AxisTick;
4use crate::spec::{
5    AxisScale, BarSpacing, CategoryOrder, HeatmapAnnotations, HeatmapLayout,
6    HeatmapNullRowAnnotation, HistogramBins, HistogramComposite, HistogramStat, InteractionStyle,
7    LegendPolicy, LinePathStyle, PlotTextAnnotation, QqLine, RawOverlay, RegressionBand,
8    WaterfallLabels,
9};
10use crate::theme::Theme;
11use crate::Labels;
12use crate::{
13    BooleanValues, Bounds, CategoryValues, EstimateStatus, NumericFormat, Significance, SourceRows,
14};
15
16impl<K: crate::kind::Marker> Plot<K> {
17    /// Set original source-row identities aligned one-to-one with the chart data.
18    #[must_use]
19    pub fn source_rows(mut self, source_rows: SourceRows) -> Self
20    where
21        K: crate::kind::SourceRows,
22    {
23        self.description.payload_mut().source_rows = Some(source_rows);
24        self
25    }
26
27    /// Set categorical color identity independently of graphical grouping.
28    #[must_use]
29    pub fn hue(mut self, values: CategoryValues) -> Self
30    where
31        K: crate::kind::Hue,
32    {
33        self.description.payload_mut().hue = Some(values);
34        self
35    }
36
37    /// Set the graphical-group column used to partition collective marks.
38    #[must_use]
39    pub fn group(mut self, values: CategoryValues) -> Self
40    where
41        K: crate::kind::Group,
42    {
43        self.description.payload_mut().group = Some(values);
44        self
45    }
46
47    /// Set one positioned categorical series that owns dodge, color, legend,
48    /// and significance identity where supported.
49    #[must_use]
50    pub fn series(mut self, values: CategoryValues) -> Self
51    where
52        K: crate::kind::Series,
53    {
54        self.description.payload_mut().series = Some(values);
55        self
56    }
57
58    /// Set the title shown beside a continuous color scale.
59    /// Content-only replacement preserves earlier Text instructions. Shared guides
60    /// require equal complete title requests, including exact style and layout presence.
61    #[must_use]
62    pub fn color_label(mut self, label: impl Into<crate::Text>) -> Self
63    where
64        K: crate::kind::ContinuousColor,
65    {
66        self.description
67            .payload_mut()
68            .color_label
69            .replace_from(label.into())
70            .expect("public Text always has a source");
71        self
72    }
73
74    /// Replace numeric presentation for this Plot's continuous color guide.
75    /// Shared Canvas guides require identical complete effective formats before fonts.
76    /// ```compile_fail
77    /// use lichtung_plot::{kind, NumericFormat, Plot};
78    /// fn unsupported(plot: Plot<kind::Scatter>, format: NumericFormat) {
79    ///     let _ = plot.color_label_format(format);
80    /// }
81    /// ```
82    #[must_use]
83    pub fn color_label_format(mut self, format: NumericFormat) -> Self
84    where
85        K: crate::kind::ContinuousColor,
86    {
87        self.description.payload_mut().color_label_format = Some(std::sync::Arc::new(format));
88        self
89    }
90
91    /// Add caller-owned wrapped text for one fully-null Cartesian heatmap row.
92    #[must_use]
93    pub fn heatmap_null_row_annotation(mut self, annotation: HeatmapNullRowAnnotation) -> Self
94    where
95        K: crate::kind::HeatmapOptions,
96    {
97        self.description
98            .payload_mut()
99            .heatmap_null_row_annotations
100            .push(annotation);
101        self
102    }
103
104    /// Set a strict Boolean column whose true heatmap cells receive a prominent border.
105    #[must_use]
106    pub fn heatmap_highlight(mut self, values: BooleanValues) -> Self
107    where
108        K: crate::kind::HeatmapOptions,
109    {
110        self.description.payload_mut().heatmap_highlight = Some(values);
111        self
112    }
113
114    /// Draw numeric labels beside point-only Coefficient estimates.
115    /// One precision is selected from every available prepared estimate in the panel.
116    /// ```compile_fail
117    /// use lichtung_plot::{kind, NumericFormat, Plot};
118    /// fn unsupported(plot: Plot<kind::Forest>, format: NumericFormat) {
119    ///     let _ = plot.estimate_labels(format);
120    /// }
121    /// ```
122    #[must_use]
123    pub fn estimate_labels(mut self, format: NumericFormat) -> Self
124    where
125        K: crate::kind::CoefficientOptions,
126    {
127        self.description.payload_mut().estimate_labels = Some(format);
128        self
129    }
130
131    /// Configure significance typography independently of axis text.
132    #[must_use]
133    pub const fn significance_mark_style(mut self, style: crate::SignificanceMarkStyle) -> Self
134    where
135        K: crate::kind::SignificanceStyle,
136    {
137        self.description.significance_mark_style = Some(style);
138        self
139    }
140
141    /// Set the chart title.
142    ///
143    /// Plain strings replace content while retaining existing appearance and layout
144    /// instructions. A supplied Text's present style or whole layout replaces that
145    /// instruction; an explicit default layout resets it to owner defaults.
146    #[must_use]
147    pub fn title(mut self, title: impl Into<crate::Text>) -> Self {
148        self.description
149            .title
150            .replace_from(title.into())
151            .expect("public Text always has a source");
152        self
153    }
154
155    /// Set the chart subtitle.
156    /// Content and instruction replacement follow [`Self::title`].
157    #[must_use]
158    pub fn subtitle(mut self, subtitle: impl Into<crate::Text>) -> Self {
159        self.description
160            .subtitle
161            .replace_from(subtitle.into())
162            .expect("public Text always has a source");
163        self
164    }
165
166    /// Align this Plot's title and each of its lines to a physical edge or center.
167    ///
168    /// Overrides the Canvas panel default and this Plot's complete theme. Repeated
169    /// calls replace only this instruction; theme attachment order is independent.
170    /// Start and End are physical left and right after the owning content insets.
171    /// ```
172    /// use lichtung_plot::{NumericValues, Plot, Theme, TextAlignment};
173    /// let plot = Plot::scatter(
174    ///     NumericValues::new("x", [1.0, 2.0]),
175    ///     NumericValues::new("y", [2.0, 1.0]),
176    /// ).title("Title").subtitle("Independent subtitle")
177    ///  .title_alignment(TextAlignment::End)
178    ///  .subtitle_alignment(TextAlignment::Start)
179    ///  .theme(Theme::nature());
180    /// # let _ = plot;
181    /// ```
182    #[must_use]
183    pub const fn title_alignment(mut self, alignment: crate::TextAlignment) -> Self {
184        self.description.title.edit_alignment(alignment);
185        self
186    }
187
188    /// Align this Plot's subtitle independently of its title.
189    ///
190    /// Overrides the Canvas panel subtitle default, then the optional theme
191    /// subtitle alignment, then the final effective title alignment. Omitting this
192    /// call contributes no instruction; it does not bypass those lower owners.
193    #[must_use]
194    pub const fn subtitle_alignment(mut self, alignment: crate::TextAlignment) -> Self {
195        self.description.subtitle.edit_alignment(alignment);
196        self
197    }
198
199    /// Set the chart caption.
200    /// Content and instruction replacement follow [`Self::title`].
201    #[must_use]
202    pub fn caption(mut self, caption: impl Into<crate::Text>) -> Self {
203        self.description
204            .payload_mut()
205            .caption
206            .replace_from(caption.into())
207            .expect("public Text always has a source");
208        self
209    }
210
211    /// Set the x-axis label.
212    /// Local style and line alignment are independent of the y title. Replacement
213    /// follows [`Self::title`], preserving prior instructions when omitted.
214    #[must_use]
215    pub fn x_label(mut self, label: impl Into<crate::Text>) -> Self {
216        self.description
217            .payload_mut()
218            .x_label
219            .replace_from(label.into())
220            .expect("public Text always has a source");
221        self
222    }
223
224    /// Set the y-axis label.
225    /// Local line alignment applies before the existing vertical title transform.
226    /// Content and instruction replacement follow [`Self::title`].
227    #[must_use]
228    pub fn y_label(mut self, label: impl Into<crate::Text>) -> Self {
229        self.description
230            .payload_mut()
231            .y_label
232            .replace_from(label.into())
233            .expect("public Text always has a source");
234        self
235    }
236
237    /// Set presentation rules for every label inferred from data identities.
238    #[must_use]
239    pub fn labels(mut self, labels: Labels) -> Self {
240        self.description.payload_mut().labels = labels;
241        self
242    }
243
244    /// Select eligible x category labels without removing categories or ticks.
245    /// ```compile_fail
246    /// use lichtung_plot::{CategoryLabelSelection, NumericValues, Plot};
247    /// let _ = Plot::scatter(NumericValues::new("x",[1.0]),NumericValues::new("y",[1.0]))
248    ///     .x_category_labels(CategoryLabelSelection::all());
249    /// ```
250    #[must_use]
251    pub fn x_category_labels(mut self, value: crate::CategoryLabelSelection) -> Self
252    where
253        K: crate::kind::CategoricalX,
254    {
255        self.description.payload_mut().axes[0].category_labels = value;
256        self
257    }
258
259    /// Select eligible y category labels without removing categories or ticks.
260    #[must_use]
261    pub fn y_category_labels(mut self, value: crate::CategoryLabelSelection) -> Self
262    where
263        K: crate::kind::CategoricalY,
264    {
265        self.description.payload_mut().axes[1].category_labels = value;
266        self
267    }
268
269    /// Set explicit x-axis limits.
270    #[must_use]
271    pub fn x_limits(mut self, min: f64, max: f64) -> Self
272    where
273        K: crate::kind::NumericX,
274    {
275        let axis = &mut self.description.payload_mut().axes[0];
276        *axis = std::mem::take(axis).limits(min, max);
277        self
278    }
279
280    /// Set explicit y-axis limits.
281    #[must_use]
282    pub fn y_limits(mut self, min: f64, max: f64) -> Self
283    where
284        K: crate::kind::NumericY,
285    {
286        let axis = &mut self.description.payload_mut().axes[1];
287        *axis = std::mem::take(axis).limits(min, max);
288        self
289    }
290
291    /// Replace automatic x-axis ticks with caller-labeled numeric positions.
292    #[must_use]
293    pub fn x_ticks(mut self, ticks: Vec<AxisTick>) -> Self
294    where
295        K: crate::kind::NumericX,
296    {
297        self.description.payload_mut().axes[0].exact_ticks = Some(ticks);
298        self
299    }
300
301    /// Replace automatic y-axis ticks with caller-labeled numeric positions.
302    #[must_use]
303    pub fn y_ticks(mut self, ticks: Vec<AxisTick>) -> Self
304    where
305        K: crate::kind::NumericY,
306    {
307        self.description.payload_mut().axes[1].exact_ticks = Some(ticks);
308        self
309    }
310
311    /// Replace the complete generated x-tick selection.
312    ///
313    /// Exact [`Self::x_ticks`] take precedence in either setter order, independently
314    /// of generated count limits. Targets allow nice-step deviation; maxima bound
315    /// complete in-domain sources, including endpoints. Automatic resets a previous
316    /// interval or count. The actual axis must be numeric; an interval must also
317    /// match its linear/logarithmic scale. Mismatches reject before fonts.
318    /// ```
319    /// use lichtung_plot::{NumericValues, Plot, TickGeneration};
320    /// let plot = Plot::scatter(NumericValues::new("x", [0.0, 10.0]),
321    ///     NumericValues::new("y", [0.0, 4.0]))
322    ///     .x_tick_generation(TickGeneration::target(5)?)
323    ///     .y_tick_generation(TickGeneration::maximum(4)?)
324    ///     .x_tick_generation(TickGeneration::automatic());
325    /// # let _ = plot;
326    /// # Ok::<(), lichtung_plot::Error>(())
327    /// ```
328    /// ```compile_fail
329    /// use lichtung_plot::{kind, Plot, TickGeneration};
330    /// fn categorical(plot: Plot<kind::Bar>, generation: TickGeneration) {
331    ///     let _ = plot.x_tick_generation(generation);
332    /// }
333    /// ```
334    /// ```compile_fail
335    /// use lichtung_plot::{Plot, TickGeneration};
336    /// fn erased(plot: Plot, generation: TickGeneration) {
337    ///     let _ = plot.x_tick_generation(generation);
338    /// }
339    /// ```
340    /// ```compile_fail
341    /// use lichtung_plot::{kind, Plot, TickGeneration};
342    /// fn matrix(plot: Plot<kind::Matrix>, generation: TickGeneration) {
343    ///     let _ = plot.x_tick_generation(generation);
344    /// }
345    /// ```
346    /// ```compile_fail
347    /// use lichtung_plot::{kind, Plot, TickGeneration};
348    /// fn heatmap(plot: Plot<kind::Heatmap>, generation: TickGeneration) {
349    ///     let _ = plot.x_tick_generation(generation);
350    /// }
351    /// ```
352    #[must_use]
353    pub fn x_tick_generation(mut self, generation: crate::TickGeneration) -> Self
354    where
355        K: crate::kind::NumericX,
356    {
357        self.description.payload_mut().axes[0].generation = generation;
358        self
359    }
360
361    /// Replace the complete generated y-tick selection, independently of x.
362    ///
363    /// Exact [`Self::y_ticks`] take precedence in either setter order. Only the
364    /// existing sealed numeric-y families admit this instruction. Automatic resets
365    /// any previous interval or count; actual categorical axes reject other choices.
366    /// ```compile_fail
367    /// use lichtung_plot::{kind, Plot, TickGeneration};
368    /// fn categorical(plot: Plot<kind::Beeswarm>, generation: TickGeneration) {
369    ///     let _ = plot.y_tick_generation(generation);
370    /// }
371    /// ```
372    /// ```compile_fail
373    /// use lichtung_plot::{Plot, TickGeneration};
374    /// fn erased(plot: Plot, generation: TickGeneration) {
375    ///     let _ = plot.y_tick_generation(generation);
376    /// }
377    /// ```
378    /// ```compile_fail
379    /// use lichtung_plot::{kind, Plot, TickGeneration};
380    /// fn matrix(plot: Plot<kind::Matrix>, generation: TickGeneration) {
381    ///     let _ = plot.y_tick_generation(generation);
382    /// }
383    /// ```
384    /// ```compile_fail
385    /// use lichtung_plot::{kind, Plot, TickGeneration};
386    /// fn heatmap(plot: Plot<kind::Heatmap>, generation: TickGeneration) {
387    ///     let _ = plot.y_tick_generation(generation);
388    /// }
389    /// ```
390    #[must_use]
391    pub fn y_tick_generation(mut self, generation: crate::TickGeneration) -> Self
392    where
393        K: crate::kind::NumericY,
394    {
395        self.description.payload_mut().axes[1].generation = generation;
396        self
397    }
398
399    /// Replace generated numeric x-tick presentation; exact tick text takes precedence.
400    /// Forest Log10 ticks format mathematical powers, retaining exponent-space positions.
401    /// An actual categorical axis rejects this instruction before fonts.
402    /// ```compile_fail
403    /// use lichtung_plot::{kind, NumericFormat, Plot};
404    /// fn unsupported(plot: Plot<kind::Matrix>, format: NumericFormat) {
405    ///     let _ = plot.x_tick_format(format);
406    /// }
407    /// ```
408    #[must_use]
409    pub fn x_tick_format(mut self, format: NumericFormat) -> Self
410    where
411        K: crate::kind::NumericX,
412    {
413        self.description.payload_mut().axes[0].format = Some(std::sync::Arc::new(format));
414        self
415    }
416
417    /// Replace generated numeric y-tick presentation; exact tick text takes precedence.
418    /// This changes displayed numbers without changing scientific coordinates.
419    /// ```compile_fail
420    /// use lichtung_plot::{kind, NumericFormat, Plot};
421    /// fn unsupported(plot: Plot<kind::Waterfall>, format: NumericFormat) {
422    ///     let _ = plot.y_tick_format(format);
423    /// }
424    /// ```
425    #[must_use]
426    pub fn y_tick_format(mut self, format: NumericFormat) -> Self
427    where
428        K: crate::kind::NumericY,
429    {
430        self.description.payload_mut().axes[1].format = Some(std::sync::Arc::new(format));
431        self
432    }
433
434    /// Append a numeric guide rendered in data space.
435    #[must_use]
436    pub fn guide(mut self, guide: GuideSpec) -> Self
437    where
438        K: crate::kind::Guides,
439    {
440        self.description.payload_mut().guides.push(guide);
441        self
442    }
443
444    /// Set the primary categorical display order.
445    ///
446    /// Passing [`crate::ValueSort`] selects value-derived ordering after the
447    /// supported plot family has prepared one scalar value per category. This
448    /// option is supported by bar families and point-interval plots; heatmaps
449    /// use [`Self::x_category_order`] and [`Self::y_category_order`].
450    #[must_use]
451    pub fn category_order(mut self, order: impl Into<CategoryOrder>) -> Self
452    where
453        K: crate::kind::CategoryOrder,
454    {
455        self.description.payload_mut().category_orders.primary = order.into();
456        self
457    }
458
459    /// Set the horizontal categorical order for a heatmap.
460    ///
461    /// Heatmap axes accept observed or explicit ordering. Value-derived
462    /// ordering is ambiguous because one axis category owns multiple cells.
463    #[must_use]
464    pub fn x_category_order(mut self, order: impl Into<CategoryOrder>) -> Self
465    where
466        K: crate::kind::HeatmapOptions,
467    {
468        self.description.payload_mut().category_orders.x = order.into();
469        self
470    }
471
472    /// Set the vertical categorical order for a heatmap.
473    ///
474    /// Heatmap axes accept observed or explicit ordering. Value-derived
475    /// ordering is ambiguous because one axis category owns multiple cells.
476    #[must_use]
477    pub fn y_category_order(mut self, order: impl Into<CategoryOrder>) -> Self
478    where
479        K: crate::kind::HeatmapOptions,
480    {
481        self.description.payload_mut().category_orders.y = order.into();
482        self
483    }
484
485    /// Color positive and negative bars with distinct theme colors.
486    #[must_use]
487    pub fn color_by_sign(mut self) -> Self
488    where
489        K: crate::kind::SignedBars,
490    {
491        self.description.payload_mut().color_by_sign = true;
492        self
493    }
494
495    /// Draw a reference line at zero on the continuous axis.
496    #[must_use]
497    pub fn zero_line(mut self) -> Self
498    where
499        K: crate::kind::ZeroLine,
500    {
501        self.description.payload_mut().zero_line = true;
502        self
503    }
504
505    /// Set one typed availability value per aligned estimate row.
506    #[must_use]
507    pub fn estimate_status(mut self, statuses: impl IntoIterator<Item = EstimateStatus>) -> Self
508    where
509        K: crate::kind::EstimateStatus,
510    {
511        self.description.payload_mut().estimate_status =
512            Some(statuses.into_iter().collect::<Vec<_>>().into());
513        self
514    }
515
516    /// Set whether line-like plot families draw markers at estimates.
517    #[must_use]
518    pub fn markers(mut self, visible: bool) -> Self
519    where
520        K: crate::kind::Markers,
521    {
522        self.description.payload_mut().markers_visible = visible;
523        self
524    }
525
526    /// Replace the complete categorical palette/map selection for active raw identities.
527    #[must_use]
528    ///
529    /// The retired map-only setter is unavailable:
530    /// ```compile_fail
531    /// use lichtung_plot::{kind, CategoricalColorScale, Plot};
532    /// fn old_setter(plot: Plot<kind::Scatter>, scale: CategoricalColorScale) {
533    ///     let _ = plot.categorical_colors(scale);
534    /// }
535    /// ```
536    /// This attachment is not an accent-only or erased-family capability:
537    /// ```compile_fail
538    /// use lichtung_plot::{kind, CategoricalColorScale, Plot};
539    /// fn wrong_family(plot: Plot<kind::Regression>, scale: CategoricalColorScale) {
540    ///     let _ = plot.categorical_color_scale(scale);
541    /// }
542    /// ```
543    /// ```compile_fail
544    /// use lichtung_plot::{CategoricalColorScale, Plot};
545    /// fn erased(plot: Plot, scale: CategoricalColorScale) {
546    ///     let _ = plot.categorical_color_scale(scale);
547    /// }
548    /// ```
549    pub fn categorical_color_scale(mut self, scale: crate::CategoricalColorScale) -> Self
550    where
551        K: crate::kind::CategoricalScale,
552    {
553        self.description.payload_mut().categorical_color_scale = Some(scale);
554        self
555    }
556
557    /// Replace the complete continuous kind, optional gradient and optional exact domain.
558    #[must_use]
559    ///
560    /// Exact limits belong to the complete typed selection:
561    /// ```compile_fail
562    /// use lichtung_plot::{kind, Plot};
563    /// fn old_limits(plot: Plot<kind::Heatmap>) {
564    ///     let _ = plot.color_limits(0.0, 1.0);
565    /// }
566    /// ```
567    /// ```compile_fail
568    /// use lichtung_plot::{kind, Plot};
569    /// fn old_limits(plot: Plot<kind::Beeswarm>) {
570    ///     let _ = plot.color_limits(0.0, 1.0);
571    /// }
572    /// ```
573    /// ```compile_fail
574    /// use lichtung_plot::{kind, ContinuousColorScale, Plot};
575    /// fn old_selector(plot: Plot<kind::Heatmap>, scale: ContinuousColorScale) {
576    ///     let _ = plot.scale(scale);
577    /// }
578    /// ```
579    pub fn continuous_color_scale(mut self, scale: crate::ContinuousColorScale) -> Self
580    where
581        K: crate::kind::ContinuousColor,
582    {
583        self.description.payload_mut().continuous_color_scale = Some(scale);
584        self
585    }
586
587    /// Add a caller-owned text block at one corner inside the chart panel.
588    #[must_use]
589    pub fn text_annotation(mut self, annotation: PlotTextAnnotation) -> Self
590    where
591        K: crate::kind::TextAnnotation,
592    {
593        self.description
594            .payload_mut()
595            .panel_text_annotations
596            .push(annotation);
597        self
598    }
599
600    /// Render scatter-like point marks without the default white outline.
601    #[must_use]
602    pub fn hide_point_outline(mut self) -> Self
603    where
604        K: crate::kind::PointOutline,
605    {
606        self.description.payload_mut().point_outline_visible = false;
607        self
608    }
609
610    /// Set a complete explicit Plot theme.
611    ///
612    /// If omitted, a standalone Plot uses [`Theme::publication`] and a Plot in
613    /// a [`crate::Canvas`] inherits the Canvas root theme.
614    #[must_use]
615    pub fn theme(mut self, theme: Theme) -> Self {
616        self.description.payload_mut().theme = Some(std::sync::Arc::new(theme));
617        self
618    }
619
620    /// Append caller-owned significance annotations of one placement.
621    ///
622    /// Ordinary bars accept attached and pairwise annotations through separate
623    /// calls. Other plot families expose only their supported placement.
624    #[must_use]
625    pub fn significance<P>(mut self, annotations: impl IntoIterator<Item = Significance<P>>) -> Self
626    where
627        K: crate::kind::Significance<P>,
628    {
629        self.description
630            .payload_mut()
631            .significance
632            .extend(annotations.into_iter().map(Significance::into_annotation));
633        self
634    }
635
636    /// Allocate exactly this width in CSS pixels and derive height from content.
637    ///
638    /// Without a sizing call, both dimensions are derived from content. This
639    /// replaces the complete request, clearing any height set by [`Self::size`].
640    /// Zero is rejected during preflight, without a setter panic. An embedded
641    /// Plot's standalone request does not control its Canvas cell.
642    ///
643    /// ```no_run
644    /// use lichtung_plot::{NumericValues, Plot};
645    /// let output = Plot::scatter(
646    ///     NumericValues::new("x", [0.0, 1.0]),
647    ///     NumericValues::new("y", [1.0, 0.0]),
648    /// )
649    /// .size(600, 1)
650    /// .width(600)
651    /// .svg_preserve_text("figure")?;
652    /// assert_eq!(output.width(), 600);
653    /// assert!(output.height() > 0);
654    /// # Ok::<(), lichtung_plot::Error>(())
655    /// ```
656    #[must_use]
657    pub const fn width(mut self, width: u32) -> Self {
658        self.description.root_size = crate::layout::RootRequest::ExactWidth(width);
659        self
660    }
661
662    /// Cap the natural complete outer width and measure height at the selected width.
663    ///
664    /// This replaces any earlier sizing request. It does not fill a cap wider
665    /// than natural content, enable text reduction, or change embedded Canvas
666    /// assignments. Zero rejects during owning-root preflight, not in this setter.
667    /// ```
668    /// use lichtung_plot::{NumericValues, Plot};
669    /// let plot = Plot::scatter(NumericValues::new("x", [0.0, 1.0]),
670    ///                          NumericValues::new("y", [1.0, 0.0]))
671    ///     .title("A width-capped scientific figure").max_width(640);
672    /// # let _ = plot;
673    /// ```
674    #[must_use]
675    pub const fn max_width(mut self, maximum: u32) -> Self {
676        self.description.root_size = crate::layout::RootRequest::MaxWidth(maximum);
677        self
678    }
679
680    /// Allocate exactly this width and height in CSS pixels.
681    ///
682    /// This replaces the complete sizing request. Content must fit without
683    /// changing either dimension; zero dimensions reject during preflight.
684    /// Final canonical dimensions are available on [`crate::Output`].
685    #[must_use]
686    pub const fn size(mut self, width: u32, height: u32) -> Self {
687        self.description.root_size = crate::layout::RootRequest::ExactSize { width, height };
688        self
689    }
690
691    /// Set the legend visibility and placement policy.
692    ///
693    /// Native Heatmap/Beeswarm keep their right guide track and Matrix its trailing
694    /// grid track; Hidden suppresses their guide. Standard panels use the selected
695    /// inside/outside edge. All exact families can explain declared data alpha.
696    #[must_use]
697    pub fn legend(mut self, policy: LegendPolicy) -> Self
698    where
699        K: crate::kind::Legend,
700    {
701        self.description.payload_mut().legend_policy = policy;
702        self
703    }
704
705    /// Replace discrete guide composition; the default is Combined.
706    ///
707    /// This changes only section semantics, independently of theme, labels,
708    /// visibility, placement and sizing. Facets use this owning Plot selection.
709    /// Canvas Shared uses its own selection; PerPlot uses each Plot's value.
710    /// See [`crate::LegendComposition::Separate`] for truthful factorization.
711    /// ```
712    /// use lichtung_plot::{CategoryValues, LegendComposition, NumericValues, Plot};
713    /// let plot = Plot::scatter(NumericValues::new("x", [0.0, 1.0]),
714    ///     NumericValues::new("y", [1.0, 0.0]))
715    ///     .hue(CategoryValues::new("cohort", ["control", "treated"])?)
716    ///     .legend_composition(LegendComposition::Separate);
717    /// # let _ = plot;
718    /// # Ok::<(), lichtung_plot::Error>(())
719    /// ```
720    /// All exact families can explain declared alpha. Erased plots do not expose this setter:
721    /// ```compile_fail
722    /// use lichtung_plot::{LegendComposition, Plot};
723    /// fn erased(plot: Plot) { let _ = plot.legend_composition(LegendComposition::Separate); }
724    /// ```
725    #[must_use]
726    pub const fn legend_composition(mut self, composition: crate::LegendComposition) -> Self
727    where
728        K: crate::kind::Legend,
729    {
730        self.description.legend_composition = composition;
731        self
732    }
733
734    /// Select a centered or deterministic source-row-displaced raw-observation
735    /// overlay for a boxplot or violin plot.
736    #[must_use]
737    pub const fn raw_overlay(mut self, overlay: RawOverlay) -> Self
738    where
739        K: crate::kind::RawOverlay,
740    {
741        self.description.raw_overlay = overlay;
742        self
743    }
744}
745
746impl Plot<crate::kind::Heatmap> {
747    /// Select Cartesian or structural triangular heatmap cells.
748    #[must_use]
749    pub fn layout(mut self, layout: HeatmapLayout) -> Self {
750        self.description.payload_mut().heatmap_layout = layout;
751        self
752    }
753
754    /// Configure optional heatmap cell-value annotations.
755    #[must_use]
756    pub fn annotations(mut self, annotations: HeatmapAnnotations) -> Self {
757        self.description.payload_mut().heatmap_annotations = annotations;
758        self
759    }
760}
761
762impl Plot<crate::kind::Forest> {
763    /// Set the numeric axis scale used by a forest plot.
764    #[must_use]
765    pub fn scale(mut self, scale: AxisScale) -> Self {
766        self.description.payload_mut().axis_scale = scale;
767        self
768    }
769}
770
771impl Plot<crate::kind::Bar> {
772    /// Set spacing between series inside each category.
773    #[must_use]
774    pub fn spacing(mut self, spacing: BarSpacing) -> Self {
775        self.description.payload_mut().bar_spacing = spacing;
776        self
777    }
778
779    /// Append a strict point layer positioned at prepared bar centers.
780    #[must_use]
781    pub fn point_layer(mut self, layer: BarPointLayer) -> Self {
782        self.description.payload_mut().bar_point_layers.push(layer);
783        self
784    }
785}
786
787impl Plot<crate::kind::HorizontalBar> {
788    /// Set spacing between series inside each category.
789    #[must_use]
790    pub fn spacing(mut self, spacing: BarSpacing) -> Self {
791        self.description.payload_mut().bar_spacing = spacing;
792        self
793    }
794
795    /// Append a strict point layer positioned at prepared bar centers.
796    #[must_use]
797    pub fn point_layer(mut self, layer: BarPointLayer) -> Self {
798        self.description.payload_mut().bar_point_layers.push(layer);
799        self
800    }
801}
802
803impl Plot<crate::kind::Histogram> {
804    /// Set the histogram bin-selection strategy.
805    #[must_use]
806    pub fn bins(mut self, bins: HistogramBins) -> Self {
807        self.description.payload_mut().histogram_bins = bins;
808        self
809    }
810
811    /// Set the histogram vertical-axis normalization.
812    #[must_use]
813    pub fn stat(mut self, stat: HistogramStat) -> Self {
814        self.description.payload_mut().histogram_stat = stat;
815        self
816    }
817
818    /// Add a KDE density curve and complete source-observation rug.
819    #[must_use]
820    pub fn composite(mut self, composite: HistogramComposite) -> Self {
821        self.description.payload_mut().histogram_composite = std::sync::Arc::new(composite);
822        self
823    }
824}
825
826impl Plot<crate::kind::Raincloud> {
827    /// Set the discrete hue-guide title for supplied Raincloud geometry.
828    ///
829    /// Content is verbatim; plain strings retain earlier Text instructions.
830    /// Empty text remains present. Samples, geometry without hue, and hidden
831    /// legends retain but do not consume this title.
832    #[must_use]
833    pub fn hue_label(mut self, label: impl Into<crate::Text>) -> Self {
834        self.description
835            .payload_mut()
836            .raincloud_hue_label
837            .replace_from(label.into())
838            .expect("public Text always has a source");
839        self
840    }
841}
842
843impl Plot<crate::kind::Qq> {
844    /// Set the normal Q-Q reference-line policy.
845    #[must_use]
846    pub fn reference_line(mut self, line: QqLine) -> Self {
847        self.description.payload_mut().qq_line = line;
848        self
849    }
850}
851
852impl Plot<crate::kind::Line> {
853    /// Append an independently owned supplemental point layer.
854    ///
855    /// Layers accept arbitrary coordinates independent from the primary line
856    /// input. Per-point labels annotate coordinates, while a layer legend label
857    /// identifies the complete layer once.
858    #[must_use]
859    pub fn point_layer(mut self, layer: PointLayer) -> Self {
860        self.description.payload_mut().point_layers.push(layer);
861        self
862    }
863
864    /// Add a drawable band to the line series.
865    #[must_use]
866    pub fn band(mut self, bounds: Bounds) -> Self {
867        self.description.payload_mut().line_band = Some(bounds);
868        self
869    }
870}
871
872impl Plot<crate::kind::Regression> {
873    /// Select the generated confidence-band policy.
874    #[must_use]
875    pub const fn band(mut self, band: RegressionBand) -> Self {
876        self.description.regression_band = band;
877        self
878    }
879}
880
881impl Plot<crate::kind::LinePath> {
882    /// Select the complete-path color and legend policy.
883    #[must_use]
884    pub const fn style(mut self, style: LinePathStyle) -> Self {
885        self.description.line_path_style = style;
886        self
887    }
888}
889
890impl Plot<crate::kind::Coefficient> {
891    /// Set the scientific reference/null line.
892    #[must_use]
893    pub fn reference_line(mut self, value: f64) -> Self {
894        self.description.payload_mut().coefficient_reference_line = Some(value);
895        self
896    }
897}
898
899impl Plot<crate::kind::Interaction> {
900    /// Set whether connectors are drawn between category estimates.
901    #[must_use]
902    pub const fn connectors(mut self, visible: bool) -> Self {
903        self.description.interaction_connectors = Some(visible);
904        self
905    }
906
907    /// Set exactly one typed point-and-connector style per interaction group.
908    #[must_use]
909    pub fn styles(mut self, styles: impl IntoIterator<Item = InteractionStyle>) -> Self {
910        self.description.payload_mut().interaction_styles = Some(styles.into_iter().collect());
911        self
912    }
913}
914
915impl Plot<crate::kind::Waterfall> {
916    /// Replace numeric presentation for window-eligible base and terminal totals.
917    /// Verbatim names from [`Self::total_labels`] join the formatted number with one space.
918    /// ```compile_fail
919    /// use lichtung_plot::{kind, NumericFormat, Plot};
920    /// fn unsupported(plot: Plot<kind::HorizontalBar>, format: NumericFormat) {
921    ///     let _ = plot.total_format(format);
922    /// }
923    /// ```
924    #[must_use]
925    pub fn total_format(mut self, format: NumericFormat) -> Self {
926        self.description.payload_mut().total_format = Some(std::sync::Arc::new(format));
927        self
928    }
929
930    /// Set caller-owned labels for base and terminal annotations.
931    #[must_use]
932    pub fn total_labels(mut self, labels: WaterfallLabels) -> Self {
933        self.description.payload_mut().waterfall_labels = Some(labels);
934        self
935    }
936}