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