Skip to main content

ggplot_rs/
plot.rs

1#[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
2use plotters::prelude::IntoDrawingArea;
3
4use crate::aes::Aes;
5use crate::annotate::Annotation;
6use crate::build::PlotBuilder;
7use crate::coord::cartesian::CoordCartesian;
8use crate::coord::fixed::CoordFixed;
9use crate::coord::flip::CoordFlip;
10use crate::coord::polar::CoordPolar;
11use crate::coord::Coord;
12use crate::data::{DataFrame, GGData, Value};
13use crate::facet::{Facet, FacetLabeller, FacetScales, FacetSpace};
14use crate::geom::area::GeomArea;
15use crate::geom::bar::GeomBar;
16use crate::geom::bin2d::GeomBin2d;
17use crate::geom::blank::GeomBlank;
18use crate::geom::boxplot::GeomBoxplot;
19use crate::geom::col::GeomCol;
20use crate::geom::contour::GeomContour;
21use crate::geom::count::GeomCount;
22use crate::geom::crossbar::GeomCrossbar;
23use crate::geom::curve::GeomCurve;
24use crate::geom::density::GeomDensity;
25use crate::geom::density2d::GeomDensity2d;
26use crate::geom::dotplot::GeomDotplot;
27use crate::geom::errorbar::GeomErrorbar;
28use crate::geom::freqpoly::GeomFreqpoly;
29use crate::geom::hex::GeomHex;
30use crate::geom::histogram::GeomHistogram;
31use crate::geom::jitter::GeomJitter;
32use crate::geom::line::GeomLine;
33use crate::geom::linerange::GeomLinerange;
34use crate::geom::path::GeomPath;
35use crate::geom::point::GeomPoint;
36use crate::geom::pointrange::GeomPointrange;
37use crate::geom::polygon::GeomPolygon;
38use crate::geom::qq::{GeomQQ, GeomQQLine};
39use crate::geom::rect::GeomRect;
40use crate::geom::refline::{GeomAbline, GeomHline, GeomVline};
41use crate::geom::ribbon::GeomRibbon;
42use crate::geom::rug::GeomRug;
43use crate::geom::segment::GeomSegment;
44use crate::geom::smooth::GeomSmooth;
45use crate::geom::spoke::GeomSpoke;
46use crate::geom::step::GeomStep;
47use crate::geom::text::{GeomLabel, GeomText};
48use crate::geom::tile::GeomTile;
49use crate::geom::violin::GeomViolin;
50use crate::geom::{Geom, GeomParams};
51use crate::position::Position;
52use crate::render::layout::PlotLayout;
53#[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
54use crate::render::plotters_backend::PlottersAdapter;
55use crate::render::renderer::PlotRenderer;
56use crate::render::RenderError;
57use crate::scale::continuous::ScaleContinuous;
58use crate::scale::transform::ScaleTransform;
59use crate::scale::Scale;
60use crate::stat::Stat;
61use crate::theme::Theme;
62
63// Builder methods for the 0.17 statistical-graphics layers (QQ, step ribbons,
64// ECDF bands, Cook's contours, horizontal error bars).
65mod stat_layers;
66
67/// Labels for the plot.
68#[derive(Clone, Debug, Default)]
69pub struct Labels {
70    pub title: Option<String>,
71    pub subtitle: Option<String>,
72    pub x: Option<String>,
73    pub y: Option<String>,
74    pub caption: Option<String>,
75    /// Corner tag (R's `labs(tag = ...)`), e.g. "A" for figure panels.
76    pub tag: Option<String>,
77}
78
79/// A single layer in the plot.
80pub struct Layer {
81    pub data: Option<DataFrame>,
82    pub mapping: Aes,
83    pub geom: Box<dyn Geom>,
84    pub stat: Box<dyn Stat>,
85    pub position: Box<dyn Position>,
86    pub params: GeomParams,
87    pub show_legend: Option<bool>,
88    /// The geom was configured explicitly (`geom_*_with(...)`), so its colours
89    /// are deliberate and the theme's `primary_color` must not override them.
90    pub explicit_style: bool,
91    /// A `{column}` tooltip template ([`GGPlot::tooltip`]), rendered per row
92    /// from the *built* layer data (post-stat) once that's available.
93    pub tooltip_template: Option<String>,
94}
95
96/// The top-level plot specification — builder pattern.
97pub struct GGPlot {
98    pub(crate) data: DataFrame,
99    pub(crate) mapping: Aes,
100    pub(crate) layers: Vec<Layer>,
101    pub(crate) scales: Vec<Box<dyn Scale>>,
102    pub(crate) coord: Box<dyn Coord>,
103    pub(crate) theme: Theme,
104    pub(crate) labels: Labels,
105    pub(crate) facet: Facet,
106    pub(crate) annotations: Vec<Annotation>,
107    pub(crate) guide_legend: crate::guide::config::GuideLegend,
108    /// Warnings raised while specifying the plot (e.g. a clipped calendar
109    /// span); prepended to the build warnings.
110    pub(crate) warnings: Vec<String>,
111    /// Panel aspect ratio used when the theme sets none (helpers such as
112    /// `geom_calendar` need square cells; survives theme presets).
113    pub(crate) default_aspect_ratio: Option<f64>,
114    /// Plot-wide default locale for axis / legend labels ([`GGPlot::locale`]).
115    pub(crate) locale: Option<crate::locale::Locale>,
116    /// Whether the rendered root `<svg>` carries `data-plot`/`data-domain`/…
117    /// ([`svg_root_data`](Self::svg_root_data)). Default `true`; a consumer
118    /// overlaying a fragment on its own basemap (`render_svg_native_at`)
119    /// turns it off so the fragment adds no root attributes of its own.
120    pub(crate) svg_root_data: bool,
121    /// Extra root `<svg>` attributes requested via [`svg_attr`](Self::svg_attr)/
122    /// [`svg_attrs`](Self::svg_attrs), validated and applied at render time.
123    pub(crate) root_attrs: Vec<(String, String)>,
124    /// Whether to emit a `data-warnings` JSON array of the build warnings
125    /// on the root `<svg>` ([`svg_warnings_attr`](Self::svg_warnings_attr)).
126    /// Default off; byte-identical output when off.
127    pub(crate) svg_warnings_attr: bool,
128    /// Whether to emit `data-fvalue`/`data-fx` on marks
129    /// ([`svg_formatted_values`](Self::svg_formatted_values)). Default off;
130    /// byte-identical output when off.
131    pub(crate) svg_formatted_values: bool,
132    /// #221: validated [`layer_svg_attr`](Self::layer_svg_attr)s as
133    /// `(layer index, name, value)`, applied by wrapping that layer's geom
134    /// at build time.
135    pub(crate) layer_svg_attrs: Vec<(usize, String, String)>,
136    /// #255: accessible name for the rendered root `<svg>`
137    /// ([`svg_title`](Self::svg_title)) — a `<title>` first child plus
138    /// `role="img"`/`aria-labelledby`. Default `None`; byte-identical
139    /// output when not set.
140    pub(crate) svg_title: Option<String>,
141    /// #255: accessible description for the rendered root `<svg>`
142    /// ([`svg_desc`](Self::svg_desc)) — a `<desc>` first child plus
143    /// `role="img"`/`aria-describedby`. Default `None`; byte-identical
144    /// output when not set.
145    pub(crate) svg_desc: Option<String>,
146}
147
148impl GGPlot {
149    /// Create a new plot with the given data source.
150    ///
151    /// Never panics on malformed input: e.g. column-oriented data whose columns
152    /// have different lengths is padded with `NA` and reported as a
153    /// [`GGError::ValidationError`] by [`try_build`](Self::try_build) and the
154    /// `render_*`/`save*` methods.
155    pub fn new(data: impl GGData) -> Self {
156        GGPlot {
157            data: data.into_dataframe(),
158            mapping: Aes::default(),
159            layers: Vec::new(),
160            scales: Vec::new(),
161            coord: Box::new(CoordCartesian::new()),
162            theme: Theme::default(),
163            labels: Labels::default(),
164            facet: Facet::default(),
165            annotations: Vec::new(),
166            guide_legend: crate::guide::config::GuideLegend::default(),
167            warnings: Vec::new(),
168            default_aspect_ratio: None,
169            locale: None,
170            svg_root_data: true,
171            root_attrs: Vec::new(),
172            svg_warnings_attr: false,
173            svg_formatted_values: false,
174            layer_svg_attrs: Vec::new(),
175            svg_title: None,
176            svg_desc: None,
177        }
178    }
179
180    /// Set the plot-level aesthetic mapping.
181    pub fn aes(mut self, mapping: Aes) -> Self {
182        self.warnings.extend(mapping.warnings.iter().cloned());
183        self.mapping = mapping;
184        self
185    }
186
187    // ─── Geom shortcuts ──────────────────────────────────────────
188
189    pub fn geom_point(self) -> Self {
190        self.add_geom(GeomPoint::default())
191    }
192
193    pub fn geom_point_with(self, geom: GeomPoint) -> Self {
194        self.add_geom_with(geom)
195    }
196
197    pub fn geom_line(self) -> Self {
198        self.add_geom(GeomLine::default())
199    }
200
201    pub fn geom_line_with(self, geom: GeomLine) -> Self {
202        self.add_geom_with(geom)
203    }
204
205    pub fn geom_bar(self) -> Self {
206        self.add_geom(GeomBar::default())
207    }
208
209    pub fn geom_bar_with(self, geom: GeomBar) -> Self {
210        self.add_geom_with(geom)
211    }
212
213    pub fn geom_histogram(self) -> Self {
214        self.add_geom(GeomHistogram::default())
215    }
216
217    pub fn geom_histogram_with(self, geom: GeomHistogram) -> Self {
218        self.add_geom_with(geom)
219    }
220
221    pub fn geom_boxplot(self) -> Self {
222        self.add_geom(GeomBoxplot::default())
223    }
224
225    pub fn geom_boxplot_with(self, geom: GeomBoxplot) -> Self {
226        self.add_geom_with(geom)
227    }
228
229    pub fn geom_smooth(self) -> Self {
230        self.add_geom(GeomSmooth::default())
231    }
232
233    pub fn geom_smooth_with(self, geom: GeomSmooth) -> Self {
234        self.add_geom_with(geom)
235    }
236
237    pub fn geom_col(self) -> Self {
238        self.add_geom(GeomCol::default())
239    }
240
241    pub fn geom_col_with(self, geom: GeomCol) -> Self {
242        self.add_geom_with(geom)
243    }
244
245    /// Horizontal reference line at a constant `yintercept`. Like ggplot2 the
246    /// intercept trains the y scale (the line is always visible) and the line
247    /// appears in every facet panel.
248    pub fn geom_hline(self, yintercept: f64) -> Self {
249        self.add_refline(
250            GeomHline::new(yintercept),
251            false,
252            &[("yintercept", yintercept)],
253        )
254    }
255
256    /// Add a horizontal reference line with custom styling (color/linetype/width).
257    pub fn geom_hline_with(self, geom: GeomHline) -> Self {
258        let y = geom.yintercept;
259        self.add_refline(geom, true, &[("yintercept", y)])
260    }
261
262    /// Data-mapped horizontal lines (ggplot2's `geom_hline(aes(yintercept =
263    /// …))`): one line per row of the layer data (the plot data unless
264    /// [`layer_data`](Self::layer_data) follows), per facet panel, styled by
265    /// any `color`/`linetype`/`alpha` in `mapping`. The plot-level mapping is
266    /// not inherited.
267    ///
268    /// ```
269    /// # use ggplot_rs::prelude::*;
270    /// let thresholds: Vec<(String, Vec<Value>)> = vec![
271    ///     ("bound".into(), vec![Value::Float(-0.2), Value::Float(0.2)]),
272    /// ];
273    /// let svg = GGPlot::new(vec![
274    ///         ("lag".to_string(), vec![Value::Float(1.0), Value::Float(2.0)]),
275    ///         ("acf".to_string(), vec![Value::Float(0.5), Value::Float(0.1)]),
276    ///     ])
277    ///     .aes(Aes::new().x("lag").y("acf"))
278    ///     .geom_col()
279    ///     .geom_hline_aes(Aes::new().yintercept("bound"))
280    ///     .layer_data(thresholds)
281    ///     .render_svg_native()
282    ///     .unwrap();
283    /// assert!(svg.contains("data-value=\"0.2\""));
284    /// ```
285    pub fn geom_hline_aes(self, mapping: Aes) -> Self {
286        self.add_geom(GeomHline::mapped()).layer_aes(mapping)
287    }
288
289    /// [`geom_hline_aes`](Self::geom_hline_aes) with custom default styling.
290    pub fn geom_hline_aes_with(self, geom: GeomHline, mapping: Aes) -> Self {
291        self.add_geom_with(geom).layer_aes(mapping)
292    }
293
294    /// Vertical reference line at a constant `xintercept` (trains the x
295    /// scale, appears in every panel).
296    pub fn geom_vline(self, xintercept: f64) -> Self {
297        self.add_refline(
298            GeomVline::new(xintercept),
299            false,
300            &[("xintercept", xintercept)],
301        )
302    }
303
304    /// Add a vertical reference line with custom styling (color/linetype/width).
305    pub fn geom_vline_with(self, geom: GeomVline) -> Self {
306        let x = geom.xintercept;
307        self.add_refline(geom, true, &[("xintercept", x)])
308    }
309
310    /// Data-mapped vertical lines (`aes(xintercept = …)`), one per row; see
311    /// [`geom_hline_aes`](Self::geom_hline_aes).
312    pub fn geom_vline_aes(self, mapping: Aes) -> Self {
313        self.add_geom(GeomVline::mapped()).layer_aes(mapping)
314    }
315
316    /// [`geom_vline_aes`](Self::geom_vline_aes) with custom default styling.
317    pub fn geom_vline_aes_with(self, geom: GeomVline, mapping: Aes) -> Self {
318        self.add_geom_with(geom).layer_aes(mapping)
319    }
320
321    /// Line `y = intercept + slope · x` in data space, clipped to the panel.
322    /// It does not train any scale (as in ggplot2).
323    pub fn geom_abline(self, slope: f64, intercept: f64) -> Self {
324        self.add_refline(
325            GeomAbline::new(slope, intercept),
326            false,
327            &[("slope", slope), ("intercept", intercept)],
328        )
329    }
330
331    /// Add a slope/intercept reference line with custom styling.
332    pub fn geom_abline_with(self, geom: GeomAbline) -> Self {
333        let (b, a) = (geom.slope, geom.intercept);
334        self.add_refline(geom, true, &[("slope", b), ("intercept", a)])
335    }
336
337    /// Data-mapped ablines (`aes(slope = …, intercept = …)`), one per row; a
338    /// missing aesthetic defaults to slope 1 / intercept 0.
339    pub fn geom_abline_aes(self, mapping: Aes) -> Self {
340        self.add_geom(GeomAbline::mapped()).layer_aes(mapping)
341    }
342
343    /// [`geom_abline_aes`](Self::geom_abline_aes) with custom default styling.
344    pub fn geom_abline_aes_with(self, geom: GeomAbline, mapping: Aes) -> Self {
345        self.add_geom_with(geom).layer_aes(mapping)
346    }
347
348    /// A constant reference line as ggplot2 builds it: a one-row layer frame
349    /// holding the constants, mapped to their aesthetics.
350    fn add_refline(
351        self,
352        geom: impl Geom + 'static,
353        explicit: bool,
354        values: &[(&str, f64)],
355    ) -> Self {
356        let mut data = DataFrame::new();
357        let mut mapping = Aes::new();
358        for (col, v) in values {
359            data.add_column(col.to_string(), vec![Value::Float(*v)]);
360            mapping = match *col {
361                "xintercept" => mapping.xintercept(col),
362                "yintercept" => mapping.yintercept(col),
363                "slope" => mapping.slope(col),
364                _ => mapping.intercept(col),
365            };
366        }
367        let plot = if explicit {
368            self.add_geom_with(geom)
369        } else {
370            self.add_geom(geom)
371        };
372        plot.layer_aes(mapping).layer_data(data)
373    }
374
375    pub fn geom_text(self) -> Self {
376        self.add_geom(GeomText::default())
377    }
378
379    pub fn geom_text_with(self, geom: GeomText) -> Self {
380        self.add_geom_with(geom)
381    }
382
383    /// Annotate with a correlation coefficient + p-value (`ggpubr::stat_cor()`).
384    /// Adds a text layer whose statistic computes Pearson correlation via
385    /// anofox-statistics; use [`GGPlot::stat_cor_with`] to pick Spearman or set
386    /// the label position.
387    #[cfg(feature = "ggpubr")]
388    pub fn stat_cor(self) -> Self {
389        self.geom_text().stat(crate::stat::cor::StatCor::default())
390    }
391
392    /// [`GGPlot::stat_cor`] with an explicit [`StatCor`](crate::stat::cor::StatCor)
393    /// (method / label position).
394    #[cfg(feature = "ggpubr")]
395    pub fn stat_cor_with(self, stat: crate::stat::cor::StatCor) -> Self {
396        self.geom_text().stat(stat)
397    }
398
399    /// Annotate grouped data with a group-comparison p-value
400    /// (`ggpubr::stat_compare_means()`). Adds a text layer whose statistic
401    /// compares y across the discrete x groups (Wilcoxon for two groups,
402    /// Kruskal-Wallis for more) via anofox-statistics.
403    #[cfg(feature = "ggpubr")]
404    pub fn stat_compare_means(self) -> Self {
405        self.geom_text()
406            .stat(crate::stat::compare_means::StatCompareMeans::default())
407    }
408
409    /// [`GGPlot::stat_compare_means`] with an explicit
410    /// [`StatCompareMeans`](crate::stat::compare_means::StatCompareMeans).
411    #[cfg(feature = "ggpubr")]
412    pub fn stat_compare_means_with(
413        self,
414        stat: crate::stat::compare_means::StatCompareMeans,
415    ) -> Self {
416        self.geom_text().stat(stat)
417    }
418
419    /// Auto-draw pairwise significance brackets (`ggpubr::stat_compare_means`
420    /// with `comparisons`). For each `(group_a, group_b)` pair it runs a
421    /// two-sample test (Wilcoxon by default) on the plot's y-values, formats the
422    /// p-value, and stacks a labelled [`geom_bracket`](GGPlot::geom_bracket)
423    /// above the data. Uses the plot's x/y mapping and data.
424    #[cfg(feature = "ggpubr")]
425    pub fn stat_compare_means_pairwise(self, comparisons: &[(&str, &str)]) -> Self {
426        self.stat_compare_means_pairwise_with(
427            crate::stat::compare_means::CompareMethod::Auto,
428            comparisons,
429        )
430    }
431
432    /// [`stat_compare_means_pairwise`](GGPlot::stat_compare_means_pairwise) with
433    /// an explicit test method.
434    #[cfg(feature = "ggpubr")]
435    pub fn stat_compare_means_pairwise_with(
436        self,
437        method: crate::stat::compare_means::CompareMethod,
438        comparisons: &[(&str, &str)],
439    ) -> Self {
440        // Resolve the x/y source columns from the plot mapping + data.
441        let col_for = |a: crate::aes::Aesthetic| {
442            self.mapping
443                .mappings
444                .iter()
445                .find(|m| m.aesthetic == a)
446                .map(|m| m.column.clone())
447        };
448        let (xcol, ycol) = match (
449            col_for(crate::aes::Aesthetic::X),
450            col_for(crate::aes::Aesthetic::Y),
451        ) {
452            (Some(x), Some(y)) => (x, y),
453            _ => return self,
454        };
455        let (xc, yc) = match (self.data.column(&xcol), self.data.column(&ycol)) {
456            (Some(x), Some(y)) => (x, y),
457            _ => return self,
458        };
459
460        // Bucket y by x category (first-seen order) and find the data range.
461        let mut groups: Vec<(String, Vec<f64>)> = Vec::new();
462        let mut ymax = f64::NEG_INFINITY;
463        let mut ymin = f64::INFINITY;
464        for (xv, yv) in xc.iter().zip(yc.iter()) {
465            let y = match yv.as_f64() {
466                Some(y) if y.is_finite() => y,
467                _ => continue,
468            };
469            ymax = ymax.max(y);
470            ymin = ymin.min(y);
471            let key = xv.to_group_key();
472            if let Some(g) = groups.iter_mut().find(|(k, _)| *k == key) {
473                g.1.push(y);
474            } else {
475                groups.push((key, vec![y]));
476            }
477        }
478        if groups.len() < 2 {
479            return self;
480        }
481        let step = if ymax > ymin {
482            (ymax - ymin) * 0.12
483        } else {
484            1.0
485        };
486
487        // One stacked bracket per resolvable comparison.
488        let (mut xmin_v, mut xmax_v, mut y_v, mut label_v) =
489            (Vec::new(), Vec::new(), Vec::new(), Vec::new());
490        let mut placed = 0usize;
491        for (a, b) in comparisons {
492            let ga = groups.iter().find(|(k, _)| k == a).map(|g| &g.1);
493            let gb = groups.iter().find(|(k, _)| k == b).map(|g| &g.1);
494            let (ga, gb) = match (ga, gb) {
495                (Some(x), Some(y)) if x.len() >= 2 && y.len() >= 2 => (x, y),
496                _ => continue,
497            };
498            let p = match crate::stat::compare_means::pairwise_p(method, ga, gb) {
499                Some(p) => p,
500                None => continue,
501            };
502            xmin_v.push(Value::Str((*a).to_string()));
503            xmax_v.push(Value::Str((*b).to_string()));
504            y_v.push(Value::Float(ymax + step * (placed as f64 + 1.0)));
505            label_v.push(Value::Str(crate::stat::cor::format_p_value(p)));
506            placed += 1;
507        }
508        if xmin_v.is_empty() {
509            return self;
510        }
511
512        let data = vec![
513            ("xmin".to_string(), xmin_v),
514            ("xmax".to_string(), xmax_v),
515            ("y".to_string(), y_v),
516            ("label".to_string(), label_v),
517        ];
518        self.add_geom(crate::geom::bracket::GeomBracket::default())
519            .layer_data(data)
520            .layer_aes(Aes::new().xmin("xmin").xmax("xmax").y("y").label("label"))
521    }
522
523    /// Draw a significance bracket over the plot (`ggpubr::geom_bracket`): a bar
524    /// spanning the categorical positions `xmin`..`xmax` at height `y`, captioned
525    /// with `label` (e.g. a p-value or significance stars). Pairs with a boxplot
526    /// and [`stat_compare_means`](GGPlot::stat_compare_means).
527    pub fn geom_bracket(self, xmin: &str, xmax: &str, y: f64, label: &str) -> Self {
528        self.geom_bracket_many(
529            crate::geom::bracket::GeomBracket::default(),
530            &[(xmin, xmax, y, label)],
531        )
532    }
533
534    /// Draw several significance brackets at once with a configured
535    /// [`GeomBracket`](crate::geom::bracket::GeomBracket). Each tuple is
536    /// `(xmin, xmax, y, label)`.
537    pub fn geom_bracket_many(
538        self,
539        geom: crate::geom::bracket::GeomBracket,
540        brackets: &[(&str, &str, f64, &str)],
541    ) -> Self {
542        let xmin = brackets
543            .iter()
544            .map(|b| Value::Str(b.0.to_string()))
545            .collect::<Vec<_>>();
546        let xmax = brackets
547            .iter()
548            .map(|b| Value::Str(b.1.to_string()))
549            .collect::<Vec<_>>();
550        let y = brackets
551            .iter()
552            .map(|b| Value::Float(b.2))
553            .collect::<Vec<_>>();
554        let label = brackets
555            .iter()
556            .map(|b| Value::Str(b.3.to_string()))
557            .collect::<Vec<_>>();
558        let data = vec![
559            ("xmin".to_string(), xmin),
560            ("xmax".to_string(), xmax),
561            ("y".to_string(), y),
562            ("label".to_string(), label),
563        ];
564        self.add_geom(geom)
565            .layer_data(data)
566            .layer_aes(Aes::new().xmin("xmin").xmax("xmax").y("y").label("label"))
567    }
568
569    /// Significance brackets from a precomputed test table (ggpubr's
570    /// `stat_pvalue_manual` / `geom_bracket(data = …)`) — e.g. the anofox
571    /// `test` contract output (`group1`, `group2`, `p_adj`/`p_value`, optional
572    /// `y_position`, `label`). No test is recomputed. See
573    /// [`BracketTable`](crate::geom::bracket::BracketTable) for the column
574    /// rules, label templates (`"p = {p_adj}"`, `"{p.signif}"`) and the
575    /// automatic stacking of rows without a `y_position`. Rows that cannot be
576    /// drawn are dropped with a build warning.
577    pub fn geom_bracket_table(
578        mut self,
579        table: impl GGData,
580        spec: crate::geom::bracket::BracketTable,
581    ) -> Self {
582        let table = table.into_dataframe();
583        let col_for = |a: crate::aes::Aesthetic| {
584            self.mapping
585                .mappings
586                .iter()
587                .find(|m| m.aesthetic == a)
588                .map(|m| m.column.clone())
589        };
590        // The plot's x categories (to reject unknown groups) and finite y
591        // range (to stack brackets above the data).
592        let x_levels: Vec<String> = col_for(crate::aes::Aesthetic::X)
593            .and_then(|c| self.data.column(&c))
594            .filter(|col| col.iter().any(|v| matches!(v, Value::Str(_))))
595            .map(|col| {
596                let mut seen = std::collections::HashSet::new();
597                col.iter()
598                    .filter(|v| !v.is_na())
599                    .map(|v| v.to_group_key())
600                    .filter(|k| seen.insert(k.clone()))
601                    .collect()
602            })
603            .unwrap_or_default();
604        let y_range = col_for(crate::aes::Aesthetic::Y)
605            .and_then(|c| self.data.column(&c))
606            .and_then(|col| {
607                let (lo, hi) = col
608                    .iter()
609                    .filter_map(|v| v.as_f64())
610                    .filter(|v| v.is_finite())
611                    .fold((f64::INFINITY, f64::NEG_INFINITY), |(lo, hi), v| {
612                        (lo.min(v), hi.max(v))
613                    });
614                (lo <= hi).then_some((lo, hi))
615            });
616        let mut warnings = Vec::new();
617        let resolved = spec.resolve(&table, &x_levels, y_range, &mut warnings);
618        self.warnings.extend(warnings);
619        match resolved {
620            Some(data) => self
621                .add_geom(crate::geom::bracket::GeomBracket::from(spec.geom))
622                .layer_data(data)
623                .layer_aes(Aes::new().xmin("xmin").xmax("xmax").y("y").label("label")),
624            None => self,
625        }
626    }
627
628    /// Text labels that repel each other and their points
629    /// (`ggrepel::geom_text_repel`) — deterministic (seeded) layout; see
630    /// [`GeomTextRepel`](crate::geom::repel::GeomTextRepel). Requires `x`,
631    /// `y` and `label`.
632    pub fn geom_text_repel(self) -> Self {
633        self.add_geom(crate::geom::repel::GeomTextRepel::default())
634    }
635
636    /// [`geom_text_repel`](Self::geom_text_repel) with a configured geom
637    /// (padding, nudge, `max_overlaps`, seed, …).
638    pub fn geom_text_repel_with(self, geom: crate::geom::repel::GeomTextRepel) -> Self {
639        self.add_geom_with(geom)
640    }
641
642    /// Boxed labels that repel each other and their points
643    /// (`ggrepel::geom_label_repel`).
644    pub fn geom_label_repel(self) -> Self {
645        self.add_geom(crate::geom::repel::GeomLabelRepel::default())
646    }
647
648    /// [`geom_label_repel`](Self::geom_label_repel) with a configured geom.
649    pub fn geom_label_repel_with(self, geom: crate::geom::repel::GeomLabelRepel) -> Self {
650        self.add_geom_with(geom)
651    }
652
653    pub fn geom_label(self) -> Self {
654        self.add_geom(GeomLabel::default())
655    }
656
657    pub fn geom_label_with(self, geom: GeomLabel) -> Self {
658        self.add_geom_with(geom)
659    }
660
661    pub fn geom_area(self) -> Self {
662        self.add_geom(GeomArea::default())
663    }
664
665    pub fn geom_area_with(self, geom: GeomArea) -> Self {
666        self.add_geom_with(geom)
667    }
668
669    pub fn geom_ribbon(self) -> Self {
670        self.add_geom(GeomRibbon::default())
671    }
672
673    pub fn geom_ribbon_with(self, geom: GeomRibbon) -> Self {
674        self.add_geom_with(geom)
675    }
676
677    pub fn geom_errorbar(self) -> Self {
678        self.add_geom(GeomErrorbar::default())
679    }
680
681    pub fn geom_errorbar_with(self, geom: GeomErrorbar) -> Self {
682        self.add_geom_with(geom)
683    }
684
685    pub fn geom_segment(self) -> Self {
686        self.add_geom(GeomSegment::default())
687    }
688
689    pub fn geom_segment_with(self, geom: GeomSegment) -> Self {
690        self.add_geom_with(geom)
691    }
692
693    pub fn geom_density(self) -> Self {
694        self.add_geom(GeomDensity::default())
695    }
696
697    pub fn geom_density_with(self, geom: GeomDensity) -> Self {
698        self.add_geom_with(geom)
699    }
700
701    pub fn geom_rug(self) -> Self {
702        self.add_geom(GeomRug::default())
703    }
704
705    pub fn geom_rug_with(self, geom: GeomRug) -> Self {
706        self.add_geom_with(geom)
707    }
708
709    pub fn geom_jitter(self) -> Self {
710        self.add_geom(GeomJitter::default())
711    }
712
713    pub fn geom_jitter_with(self, geom: GeomJitter) -> Self {
714        self.add_geom_with(geom)
715    }
716
717    pub fn geom_path(self) -> Self {
718        self.add_geom(GeomPath::default())
719    }
720
721    pub fn geom_path_with(self, geom: GeomPath) -> Self {
722        self.add_geom_with(geom)
723    }
724
725    /// Add a confidence-ellipse layer (default 95%) as a path per group.
726    pub fn stat_ellipse(self) -> Self {
727        self.geom_path()
728            .stat(crate::stat::ellipse::StatEllipse::default())
729    }
730
731    /// Add a confidence-ellipse layer at the given level (0, 1).
732    pub fn stat_ellipse_level(self, level: f64) -> Self {
733        self.geom_path()
734            .stat(crate::stat::ellipse::StatEllipse::new(level))
735    }
736
737    /// Add a quantile-regression line for each `tau` as a separate path layer
738    /// (R's `stat_quantile`). Backed by anofox-regression (feature `regression`).
739    #[cfg(feature = "regression")]
740    pub fn stat_quantile(mut self, taus: &[f64]) -> Self {
741        for &tau in taus {
742            self = self
743                .geom_path()
744                .stat(crate::stat::quantile::StatQuantile::new(tau));
745        }
746        self
747    }
748
749    /// Quantile-regression lines at the quartiles (0.25, 0.5, 0.75).
750    #[cfg(feature = "regression")]
751    pub fn geom_quantile(self) -> Self {
752        self.stat_quantile(&[0.25, 0.5, 0.75])
753    }
754
755    pub fn geom_step(self) -> Self {
756        self.add_geom(GeomStep::default())
757    }
758
759    pub fn geom_step_with(self, geom: GeomStep) -> Self {
760        self.add_geom_with(geom)
761    }
762
763    pub fn geom_freqpoly(self) -> Self {
764        self.add_geom(GeomFreqpoly::default())
765    }
766
767    pub fn geom_freqpoly_with(self, geom: GeomFreqpoly) -> Self {
768        self.add_geom_with(geom)
769    }
770
771    pub fn geom_linerange(self) -> Self {
772        self.add_geom(GeomLinerange::default())
773    }
774
775    pub fn geom_linerange_with(self, geom: GeomLinerange) -> Self {
776        self.add_geom_with(geom)
777    }
778
779    pub fn geom_pointrange(self) -> Self {
780        self.add_geom(GeomPointrange::default())
781    }
782
783    pub fn geom_pointrange_with(self, geom: GeomPointrange) -> Self {
784        self.add_geom_with(geom)
785    }
786
787    pub fn geom_crossbar(self) -> Self {
788        self.add_geom(GeomCrossbar::default())
789    }
790
791    pub fn geom_crossbar_with(self, geom: GeomCrossbar) -> Self {
792        self.add_geom_with(geom)
793    }
794
795    pub fn geom_spoke(self) -> Self {
796        self.add_geom(GeomSpoke::default())
797    }
798
799    pub fn geom_spoke_with(self, geom: GeomSpoke) -> Self {
800        self.add_geom_with(geom)
801    }
802
803    pub fn geom_rect(self) -> Self {
804        self.add_geom(GeomRect::default())
805    }
806
807    pub fn geom_rect_with(self, geom: GeomRect) -> Self {
808        self.add_geom_with(geom)
809    }
810
811    pub fn geom_tile(self) -> Self {
812        self.add_geom(GeomTile::default())
813    }
814
815    pub fn geom_tile_with(self, geom: GeomTile) -> Self {
816        self.add_geom_with(geom)
817    }
818
819    /// Dense regular grid of filled cells (heatmap/raster) from x, y, fill.
820    pub fn geom_raster(self) -> Self {
821        self.add_geom(crate::geom::raster::GeomRaster::default())
822    }
823
824    pub fn geom_raster_with(self, geom: crate::geom::raster::GeomRaster) -> Self {
825        self.add_geom_with(geom)
826    }
827
828    pub fn geom_polygon(self) -> Self {
829        self.add_geom(GeomPolygon::default())
830    }
831
832    pub fn geom_polygon_with(self, geom: GeomPolygon) -> Self {
833        self.add_geom_with(geom)
834    }
835
836    /// Render simple-features geometry from a WKT `geometry` column (feature `sf`).
837    #[cfg(feature = "sf")]
838    pub fn geom_sf(self) -> Self {
839        self.add_geom(crate::geom::sf::GeomSf::default())
840    }
841
842    #[cfg(feature = "sf")]
843    pub fn geom_sf_with(self, geom: crate::geom::sf::GeomSf) -> Self {
844        self.add_geom_with(geom)
845    }
846
847    /// Labels placed inside `geom_sf` polygons (feature `sf`): `label`
848    /// aesthetic, `geometry` column — ggplot2's `geom_sf_text`.
849    #[cfg(feature = "sf")]
850    pub fn geom_sf_text(self) -> Self {
851        self.add_geom(crate::geom::sf_text::GeomSfText::default())
852    }
853
854    #[cfg(feature = "sf")]
855    pub fn geom_sf_text_with(self, geom: crate::geom::sf_text::GeomSfText) -> Self {
856        self.add_geom_with(geom)
857    }
858
859    /// Like [`geom_sf_text`](Self::geom_sf_text), on a background box —
860    /// ggplot2's `geom_sf_label`.
861    #[cfg(feature = "sf")]
862    pub fn geom_sf_label(self) -> Self {
863        self.add_geom(crate::geom::sf_text::GeomSfLabel::default())
864    }
865
866    #[cfg(feature = "sf")]
867    pub fn geom_sf_label_with(self, geom: crate::geom::sf_text::GeomSfLabel) -> Self {
868        self.add_geom_with(geom)
869    }
870
871    /// A metric scale bar in a panel corner (feature `sf`) — ggspatial's
872    /// `annotation_scale()`. Assumes the X/Y domain is in metres (any
873    /// `geom_sf` projection other than `SfProjection::PlateCarree`).
874    #[cfg(feature = "sf")]
875    pub fn annotation_scale(self) -> Self {
876        self.add_geom(crate::geom::annotation_scale::GeomAnnotationScale::default())
877    }
878
879    #[cfg(feature = "sf")]
880    pub fn annotation_scale_with(
881        self,
882        geom: crate::geom::annotation_scale::GeomAnnotationScale,
883    ) -> Self {
884        self.add_geom_with(geom)
885    }
886
887    /// A small, muted attribution text in a panel corner (feature `sf`) —
888    /// e.g. `"© Natural Earth"`.
889    #[cfg(feature = "sf")]
890    pub fn annotation_attribution(self, text: impl Into<String>) -> Self {
891        self.add_geom(crate::geom::annotation_scale::GeomAttribution::new(text))
892    }
893
894    #[cfg(feature = "sf")]
895    pub fn annotation_attribution_with(
896        self,
897        geom: crate::geom::annotation_scale::GeomAttribution,
898    ) -> Self {
899        self.add_geom_with(geom)
900    }
901
902    /// The square `n × m` guide for a
903    /// [`ScaleFillBivariate`](crate::scale::bivariate::ScaleFillBivariate)
904    /// fill scale (feature `sf`): two axis captions, biscale's own legend
905    /// shape — drawn as a furniture layer, not through the ordinary
906    /// per-aesthetic legend (set `.legend_position(LegendPosition::None)`
907    /// to hide that one). [`GeomBivariateLegend::from_scale`] builds this
908    /// from the same scale used for the fill, so the swatches match.
909    #[cfg(feature = "sf")]
910    pub fn bivariate_legend(
911        self,
912        geom: crate::geom::bivariate_legend::GeomBivariateLegend,
913    ) -> Self {
914        self.add_geom(geom)
915    }
916
917    pub fn geom_curve(self) -> Self {
918        self.add_geom(GeomCurve::default())
919    }
920
921    pub fn geom_curve_with(self, geom: GeomCurve) -> Self {
922        self.add_geom_with(geom)
923    }
924
925    pub fn geom_violin(self) -> Self {
926        self.add_geom(GeomViolin::default())
927    }
928
929    pub fn geom_violin_with(self, geom: GeomViolin) -> Self {
930        self.add_geom_with(geom)
931    }
932
933    pub fn geom_dotplot(self) -> Self {
934        self.add_geom(GeomDotplot::default())
935    }
936
937    pub fn geom_dotplot_with(self, geom: GeomDotplot) -> Self {
938        self.add_geom_with(geom)
939    }
940
941    pub fn geom_qq(self) -> Self {
942        self.add_geom(GeomQQ::default())
943    }
944
945    pub fn geom_qq_with(self, geom: GeomQQ) -> Self {
946        self.add_geom_with(geom)
947    }
948
949    pub fn geom_qq_line(self) -> Self {
950        self.add_geom(GeomQQLine::default())
951    }
952
953    pub fn geom_qq_line_with(self, geom: GeomQQLine) -> Self {
954        self.add_geom_with(geom)
955    }
956
957    pub fn geom_bin2d(self) -> Self {
958        self.add_geom(GeomBin2d::default())
959    }
960
961    pub fn geom_bin2d_with(self, geom: GeomBin2d) -> Self {
962        self.add_geom_with(geom)
963    }
964
965    pub fn geom_hex(self) -> Self {
966        self.add_geom(GeomHex::default())
967    }
968
969    pub fn geom_hex_with(self, geom: GeomHex) -> Self {
970        self.add_geom_with(geom)
971    }
972
973    pub fn geom_count(self) -> Self {
974        self.add_geom(GeomCount::default())
975    }
976
977    pub fn geom_count_with(self, geom: GeomCount) -> Self {
978        self.add_geom_with(geom)
979    }
980
981    pub fn geom_contour(self) -> Self {
982        self.add_geom(GeomContour::default())
983    }
984
985    pub fn geom_contour_with(self, geom: GeomContour) -> Self {
986        self.add_geom_with(geom)
987    }
988
989    /// Filled contour bands from gridded (x, y, z) data — draws polygons filled by
990    /// band level. Pair with a continuous fill scale (e.g. `scale_fill_viridis_c`).
991    pub fn geom_contour_filled(self) -> Self {
992        self.add_geom(GeomPolygon {
993            line_width: 0.0,
994            alpha: 1.0,
995            ..GeomPolygon::default()
996        })
997        .stat(crate::stat::contour_filled::StatContourFilled::default())
998    }
999
1000    pub fn geom_density2d(self) -> Self {
1001        self.add_geom(GeomDensity2d::default())
1002    }
1003
1004    pub fn geom_density2d_with(self, geom: GeomDensity2d) -> Self {
1005        self.add_geom_with(geom)
1006    }
1007
1008    /// Candlestick chart: map `x` and `open`/`high`/`low`/`close`
1009    /// (`Aes::new().x("date").open("o").high("h").low("l").close("c")`).
1010    pub fn geom_candlestick(self) -> Self {
1011        self.add_geom(crate::geom::candlestick::GeomCandlestick::default())
1012    }
1013
1014    pub fn geom_candlestick_with(self, geom: crate::geom::candlestick::GeomCandlestick) -> Self {
1015        self.add_geom_with(geom)
1016    }
1017
1018    /// OHLC bar chart (high–low bar with open/close ticks); same aesthetics as
1019    /// [`geom_candlestick`](Self::geom_candlestick).
1020    pub fn geom_ohlc(self) -> Self {
1021        self.add_geom(crate::geom::candlestick::GeomOhlc::default())
1022    }
1023
1024    pub fn geom_ohlc_with(self, geom: crate::geom::candlestick::GeomOhlc) -> Self {
1025        self.add_geom_with(geom)
1026    }
1027
1028    /// Calendar heatmap (GitHub/ECharts style): the plot's `x` (a date —
1029    /// `DateTime`, epoch seconds or `"YYYY-MM-DD"`) is laid out as week
1030    /// columns × weekday rows (Sunday on top) via [`StatCalendar`], coloured
1031    /// by `fill`, with month labels along x, Mon/Wed/Fri along y, month
1032    /// boundary outlines and square cells. Spans over
1033    /// [`MAX_CALENDAR_YEARS`] are clipped to the most recent years (with a
1034    /// build warning). Uses the plot-level data and `x` mapping.
1035    ///
1036    /// [`StatCalendar`]: crate::stat::calendar::StatCalendar
1037    /// [`MAX_CALENDAR_YEARS`]: crate::stat::calendar::MAX_CALENDAR_YEARS
1038    pub fn geom_calendar(self) -> Self {
1039        self.geom_calendar_with(
1040            GeomTile {
1041                color: (255, 255, 255),
1042                line_width: 1.5,
1043                ..Default::default()
1044            },
1045            false,
1046        )
1047    }
1048
1049    /// [`geom_calendar`](Self::geom_calendar) with a custom cell style and
1050    /// optionally Monday-first weeks.
1051    pub fn geom_calendar_with(mut self, tile: GeomTile, monday_first: bool) -> Self {
1052        use crate::stat::calendar::{
1053            day_number, month_boundaries, month_breaks, month_breaks_localized, weekday_labels,
1054            CalendarGrid, StatCalendar, MAX_CALENDAR_YEARS,
1055        };
1056        let grid = self
1057            .mapping
1058            .get_mapping(&crate::aes::Aesthetic::X)
1059            .and_then(|c| self.data.column(c))
1060            .and_then(|col| {
1061                CalendarGrid::from_days(col.iter().filter_map(day_number), monday_first)
1062            });
1063        let Some((grid, clipped)) = grid else {
1064            // No dates: an empty calendar layer (renders an empty panel).
1065            return self.add_geom_with(tile).stat(StatCalendar {
1066                grid: None,
1067                monday_first,
1068            });
1069        };
1070        if clipped {
1071            self.warnings.push(format!(
1072                "geom_calendar: dates more than {MAX_CALENDAR_YEARS} years before the newest were dropped"
1073            ));
1074        }
1075        let (xb, _) = month_breaks(&grid);
1076        // Rows: y = 6 - weekday; label Mon/Wed/Fri.
1077        let yb = if monday_first {
1078            vec![6.0, 4.0, 2.0]
1079        } else {
1080            vec![5.0, 3.0, 1.0]
1081        };
1082        let segs = month_boundaries(&grid);
1083        let col = |f: fn(&(f64, f64, f64, f64)) -> f64| -> Vec<Value> {
1084            segs.iter().map(|s| Value::Float(f(s))).collect()
1085        };
1086        let boundaries = vec![
1087            ("x".to_string(), col(|s| s.0)),
1088            ("y".to_string(), col(|s| s.1)),
1089            ("xend".to_string(), col(|s| s.2)),
1090            ("yend".to_string(), col(|s| s.3)),
1091        ];
1092        self.default_aspect_ratio = Some(7.0 / grid.n_weeks().max(1) as f64);
1093        let plot = self
1094            .add_geom_with(tile)
1095            .stat(StatCalendar {
1096                grid: Some(grid),
1097                monday_first,
1098            })
1099            .scale_x_continuous(
1100                ScaleContinuous::new()
1101                    .with_breaks(xb)
1102                    // Month / weekday names follow the plot locale.
1103                    .with_localized_labels(move |l| month_breaks_localized(&grid, l).1)
1104                    .with_expand(0.0, 0.0),
1105            )
1106            .scale_y_continuous(
1107                ScaleContinuous::new()
1108                    .with_breaks(yb)
1109                    .with_localized_labels(weekday_labels)
1110                    .with_expand(0.0, 0.0),
1111            );
1112        if segs.is_empty() {
1113            return plot;
1114        }
1115        // Styled by the theme's `calendar_month_border` at draw time.
1116        plot.add_geom_with(crate::geom::segment::GeomCalendarBorder)
1117            .layer_data(boundaries)
1118            .layer_aes(Aes::new().x("x").y("y").xend("xend").yend("yend"))
1119            .show_legend(false)
1120    }
1121
1122    pub fn geom_blank(self) -> Self {
1123        self.add_geom(GeomBlank)
1124    }
1125
1126    /// Add a geom configured explicitly by the caller: its colours win over
1127    /// the theme's `primary_color`.
1128    fn add_geom_with(self, geom: impl Geom + 'static) -> Self {
1129        let mut plot = self.add_geom(geom);
1130        if let Some(layer) = plot.layers.last_mut() {
1131            layer.explicit_style = true;
1132        }
1133        plot
1134    }
1135
1136    fn add_geom(mut self, geom: impl Geom + 'static) -> Self {
1137        let stat = geom.default_stat();
1138        let position = geom.default_position();
1139        let params = geom.default_params();
1140        self.layers.push(Layer {
1141            data: None,
1142            mapping: Aes::default(),
1143            geom: Box::new(geom),
1144            stat,
1145            position,
1146            params,
1147            show_legend: None,
1148            explicit_style: false,
1149            tooltip_template: None,
1150        });
1151        self
1152    }
1153
1154    // ─── Layer-level overrides ──────────────────────────────────
1155
1156    /// Override the stat for the most recently added layer.
1157    pub fn stat(mut self, stat: impl Stat + 'static) -> Self {
1158        if let Some(layer) = self.layers.last_mut() {
1159            layer.stat = Box::new(stat);
1160        }
1161        self
1162    }
1163
1164    /// Override the position for the most recently added layer.
1165    pub fn position(mut self, pos: impl Position + 'static) -> Self {
1166        if let Some(layer) = self.layers.last_mut() {
1167            layer.position = Box::new(pos);
1168        }
1169        self
1170    }
1171
1172    /// Override the data for the most recently added layer.
1173    pub fn layer_data(mut self, data: impl GGData) -> Self {
1174        if let Some(layer) = self.layers.last_mut() {
1175            layer.data = Some(data.into_dataframe());
1176        }
1177        self
1178    }
1179
1180    /// Override the aesthetic mapping for the most recently added layer.
1181    pub fn layer_aes(mut self, mapping: Aes) -> Self {
1182        self.warnings.extend(mapping.warnings.iter().cloned());
1183        if let Some(layer) = self.layers.last_mut() {
1184            layer.mapping = mapping;
1185        }
1186        self
1187    }
1188
1189    /// A `{column}` tooltip template for the most recently added layer
1190    /// (B2-5), replacing its default `<title>`. Each placeholder is filled
1191    /// from the *built* layer data (so it can reference a stat-computed
1192    /// column, e.g. a hex's `count`) at that row, formatted through the
1193    /// column's scale when it has one
1194    /// ([`Scale::format_value`](crate::scale::Scale::format_value)) — the same
1195    /// `data-fvalue` logic, but written into the tooltip text. A column
1196    /// that doesn't exist on this layer renders as empty, not literally
1197    /// `{column}`. `{{`/`}}` are literal braces. The result is escaped the
1198    /// same way any other tooltip is — this is plain text, not markup.
1199    pub fn tooltip(mut self, template: &str) -> Self {
1200        if let Some(layer) = self.layers.last_mut() {
1201            layer.tooltip_template = Some(template.to_string());
1202        }
1203        self
1204    }
1205
1206    /// Control whether the most recently added layer contributes to the legend.
1207    /// `true` = always show, `false` = always hide, default (None) = auto.
1208    pub fn show_legend(mut self, show: bool) -> Self {
1209        if let Some(layer) = self.layers.last_mut() {
1210            layer.show_legend = Some(show);
1211        }
1212        self
1213    }
1214
1215    /// Mark the most recently added layer as purely decorative (#161): its
1216    /// drawn marks keep their own visual style but carry **no** per-mark
1217    /// metadata — no `<title>`, no `data-x`/`data-series`/`data-value`/
1218    /// `data-fvalue`, and no [`data_attr`](crate::aes::Aes::data_attr)
1219    /// attributes — so an SVG consumer that treats "has a non-empty
1220    /// `<title>`" as "is a selectable data point" doesn't pick it up. Meant
1221    /// for a layer that annotates another layer's mark rather than drawing
1222    /// data in its own right (e.g. a boxplot's own mean dot, drawn by a
1223    /// separate `geom_point` layer purely to mark the spot the box's own
1224    /// tooltip already names). Each mark also carries
1225    /// `data-part="decorative"` and `aria-hidden="true"` (`data-geom`
1226    /// still names the real geom), so a consumer can select decorative
1227    /// marks positively rather than only infer them from what they lack.
1228    /// Their class is `gg-mark gg-decorative`, not the geom's own
1229    /// (`gg-point`, `gg-line`, …).
1230    ///
1231    /// A `.tooltip(...)` template on a decorative layer has no visible
1232    /// effect — the geom's marks carry no tooltip regardless — and warns,
1233    /// like the existing has-no-effect warning for a geom that doesn't
1234    /// read tooltips at all.
1235    ///
1236    /// A decorative layer also defaults to hidden in the legend (it's
1237    /// annotating, not its own series), unless
1238    /// [`show_legend`](Self::show_legend)`(true)` is called — before or
1239    /// after `decorative()` — to opt back in; an explicit
1240    /// `show_legend(false)` is left untouched either way.
1241    pub fn decorative(mut self) -> Self {
1242        if let Some(layer) = self.layers.last_mut() {
1243            let geom = std::mem::replace(&mut layer.geom, Box::new(GeomBlank));
1244            layer.geom = Box::new(crate::geom::decorative::Decorative(geom));
1245            if layer.show_legend.is_none() {
1246                layer.show_legend = Some(false);
1247            }
1248        }
1249        self
1250    }
1251
1252    /// Add one SVG attribute to **every mark** of the most recently added
1253    /// layer (#221) — e.g. to make a label layer drawn over node boxes inert,
1254    /// so hover/click and screen readers pass through to the box below:
1255    ///
1256    /// ```
1257    /// # use ggplot_rs::{aes::Aes, data::Value, GGPlot};
1258    /// # let data = vec![("x".to_string(), vec![Value::Float(1.0)]),
1259    /// #   ("y".to_string(), vec![Value::Float(1.0)]),
1260    /// #   ("l".to_string(), vec![Value::Str("a".into())])];
1261    /// let svg = GGPlot::new(data)
1262    ///     .aes(Aes::new().x("x").y("y").label("l"))
1263    ///     .geom_text()
1264    ///     .layer_svg_attr("aria-hidden", "true")
1265    ///     .layer_svg_attr("pointer-events", "none")
1266    ///     .layer_svg_attr("class", "node-label")
1267    ///     .render_svg_native_with_size(200, 200)
1268    ///     .unwrap();
1269    /// assert!(svg.contains(r#"aria-hidden="true" pointer-events="none""#));
1270    /// ```
1271    ///
1272    /// The allow-list is deliberately small: `class` (space-separated
1273    /// tokens, each `[A-Za-z][A-Za-z0-9_-]*`, appended to the mark's own
1274    /// `gg-mark gg-<kind>` class; invalid tokens are dropped), `aria-hidden`
1275    /// (`"true"`/`"false"`), `pointer-events` (an SVG keyword such as
1276    /// `"none"`), and `data-*` (`Aes::data_attr`'s name rules; a row's own
1277    /// `data_attr` of the same name wins). Anything else — in particular
1278    /// `style` and `on*` handlers, so a strict CSP without `unsafe-inline`
1279    /// still holds — is ignored with a build warning, as is a bad value or a
1280    /// call before any layer. A later call with the same name replaces the
1281    /// earlier value. Values are escaped on output. For a per-row class use
1282    /// [`Aes::class`](crate::aes::Aes::class); to wrap several layers' marks
1283    /// per key in one `<g>`, [`Aes::group_wrap`](crate::aes::Aes::group_wrap).
1284    /// SVG backend only.
1285    pub fn layer_svg_attr(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
1286        let (name, value) = (name.into(), value.into());
1287        let Some(idx) = self.layers.len().checked_sub(1) else {
1288            self.warnings.push(format!(
1289                "layer_svg_attr(\"{name}\") called before any layer was added — ignored"
1290            ));
1291            return self;
1292        };
1293        if let Some((n, v)) =
1294            crate::render::svg_backend::validate_layer_svg_attr(&name, &value, &mut self.warnings)
1295        {
1296            self.layer_svg_attrs
1297                .retain(|(i, k, _)| !(*i == idx && *k == n));
1298            self.layer_svg_attrs.push((idx, n, v));
1299        }
1300        self
1301    }
1302
1303    /// [`layer_svg_attr`](Self::layer_svg_attr) for several attributes at once.
1304    pub fn layer_svg_attrs<N: Into<String>, V: Into<String>>(
1305        self,
1306        attrs: impl IntoIterator<Item = (N, V)>,
1307    ) -> Self {
1308        attrs
1309            .into_iter()
1310            .fold(self, |p, (n, v)| p.layer_svg_attr(n, v))
1311    }
1312
1313    // ─── Scales ──────────────────────────────────────────────────
1314
1315    pub fn scale_x_continuous(mut self, s: ScaleContinuous) -> Self {
1316        let s = s.for_aesthetic(crate::aes::Aesthetic::X);
1317        self.scales.push(Box::new(s));
1318        self
1319    }
1320
1321    pub fn scale_y_continuous(mut self, s: ScaleContinuous) -> Self {
1322        let s = s.for_aesthetic(crate::aes::Aesthetic::Y);
1323        self.scales.push(Box::new(s));
1324        self
1325    }
1326
1327    pub fn scale_x_discrete(mut self, s: crate::scale::discrete::ScaleDiscrete) -> Self {
1328        let s = s.for_aesthetic(crate::aes::Aesthetic::X);
1329        self.scales.push(Box::new(s));
1330        self
1331    }
1332
1333    pub fn scale_y_discrete(mut self, s: crate::scale::discrete::ScaleDiscrete) -> Self {
1334        let s = s.for_aesthetic(crate::aes::Aesthetic::Y);
1335        self.scales.push(Box::new(s));
1336        self
1337    }
1338
1339    pub fn scale_color(mut self, s: impl Scale + 'static) -> Self {
1340        self.scales.push(Box::new(s));
1341        self
1342    }
1343
1344    pub fn scale_fill(mut self, s: impl Scale + 'static) -> Self {
1345        self.scales.push(Box::new(s));
1346        self
1347    }
1348
1349    pub fn scale_color_manual(self, values: Vec<(&str, crate::scale::color::RGBAColor)>) -> Self {
1350        let s = crate::scale::manual::ScaleManual::new(crate::aes::Aesthetic::Color, values);
1351        self.scale_color(s)
1352    }
1353
1354    pub fn scale_fill_manual(self, values: Vec<(&str, crate::scale::color::RGBAColor)>) -> Self {
1355        let s = crate::scale::manual::ScaleManual::new(crate::aes::Aesthetic::Fill, values);
1356        self.scale_fill(s)
1357    }
1358
1359    /// The `colour`/`color` column already holds `#RRGGBB`/`#RGB` hex
1360    /// strings, used as-is — no legend (R's `scale_colour_identity`).
1361    pub fn scale_colour_identity(self) -> Self {
1362        let s = crate::scale::identity::ScaleColorIdentity::new(crate::aes::Aesthetic::Color);
1363        self.scale_color(s)
1364    }
1365
1366    /// American-spelling alias for [`scale_colour_identity`](Self::scale_colour_identity).
1367    pub fn scale_color_identity(self) -> Self {
1368        self.scale_colour_identity()
1369    }
1370
1371    /// The `fill` column already holds `#RRGGBB`/`#RGB` hex strings, used
1372    /// as-is — no legend (R's `scale_fill_identity`).
1373    pub fn scale_fill_identity(self) -> Self {
1374        let s = crate::scale::identity::ScaleColorIdentity::new(crate::aes::Aesthetic::Fill);
1375        self.scale_fill(s)
1376    }
1377
1378    pub fn scale_color_viridis(self) -> Self {
1379        use crate::scale::color::ScaleColorDiscrete;
1380        use crate::scale::palettes::PaletteName;
1381        let s = ScaleColorDiscrete::new(crate::aes::Aesthetic::Color)
1382            .with_named_palette(&PaletteName::Viridis);
1383        self.scale_color(s)
1384    }
1385
1386    pub fn scale_color_brewer(self, name: crate::scale::palettes::PaletteName) -> Self {
1387        use crate::scale::color::ScaleColorDiscrete;
1388        let s = ScaleColorDiscrete::new(crate::aes::Aesthetic::Color).with_named_palette(&name);
1389        self.scale_color(s)
1390    }
1391
1392    pub fn scale_color_gradient(
1393        self,
1394        low: crate::scale::color::RGBAColor,
1395        high: crate::scale::color::RGBAColor,
1396    ) -> Self {
1397        use crate::scale::color::ScaleColorContinuous;
1398        let s = ScaleColorContinuous::new(crate::aes::Aesthetic::Color).with_colors(low, high);
1399        self.scale_color(s)
1400    }
1401
1402    pub fn scale_fill_gradient(
1403        self,
1404        low: crate::scale::color::RGBAColor,
1405        high: crate::scale::color::RGBAColor,
1406    ) -> Self {
1407        use crate::scale::color::ScaleColorContinuous;
1408        let s = ScaleColorContinuous::new(crate::aes::Aesthetic::Fill).with_colors(low, high);
1409        self.scale_fill(s)
1410    }
1411
1412    pub fn scale_color_gradient2(
1413        self,
1414        low: crate::scale::color::RGBAColor,
1415        mid: crate::scale::color::RGBAColor,
1416        high: crate::scale::color::RGBAColor,
1417    ) -> Self {
1418        use crate::scale::gradient::ScaleColorGradient2;
1419        let s = ScaleColorGradient2::new(crate::aes::Aesthetic::Color).with_colors(low, mid, high);
1420        self.scale_color(s)
1421    }
1422
1423    pub fn scale_fill_gradient2(
1424        self,
1425        low: crate::scale::color::RGBAColor,
1426        mid: crate::scale::color::RGBAColor,
1427        high: crate::scale::color::RGBAColor,
1428    ) -> Self {
1429        use crate::scale::gradient::ScaleColorGradient2;
1430        let s = ScaleColorGradient2::new(crate::aes::Aesthetic::Fill).with_colors(low, mid, high);
1431        self.scale_fill(s)
1432    }
1433
1434    pub fn scale_fill_viridis(self) -> Self {
1435        use crate::scale::color::ScaleColorDiscrete;
1436        use crate::scale::palettes::PaletteName;
1437        let s = ScaleColorDiscrete::new(crate::aes::Aesthetic::Fill)
1438            .with_named_palette(&PaletteName::Viridis);
1439        self.scale_fill(s)
1440    }
1441
1442    /// Continuous viridis color scale (for numeric data).
1443    pub fn scale_color_viridis_c(self) -> Self {
1444        use crate::scale::gradient_n::ScaleColorGradientN;
1445        let s = ScaleColorGradientN::viridis(crate::aes::Aesthetic::Color);
1446        self.scale_color(s)
1447    }
1448
1449    /// Continuous viridis fill scale (for numeric data).
1450    pub fn scale_fill_viridis_c(self) -> Self {
1451        use crate::scale::gradient_n::ScaleColorGradientN;
1452        let s = ScaleColorGradientN::viridis(crate::aes::Aesthetic::Fill);
1453        self.scale_fill(s)
1454    }
1455
1456    /// N-stop continuous color gradient.
1457    pub fn scale_color_gradientn(self, stops: Vec<(f64, crate::scale::color::RGBAColor)>) -> Self {
1458        use crate::scale::gradient_n::ScaleColorGradientN;
1459        let s = ScaleColorGradientN::new(crate::aes::Aesthetic::Color, stops);
1460        self.scale_color(s)
1461    }
1462
1463    /// N-stop continuous fill gradient.
1464    pub fn scale_fill_gradientn(self, stops: Vec<(f64, crate::scale::color::RGBAColor)>) -> Self {
1465        use crate::scale::gradient_n::ScaleColorGradientN;
1466        let s = ScaleColorGradientN::new(crate::aes::Aesthetic::Fill, stops);
1467        self.scale_fill(s)
1468    }
1469
1470    /// Binned (stepped) two-colour continuous colour scale — buckets the mapped
1471    /// variable into `n_bins` bins, each a discrete colour, with a stepped legend.
1472    pub fn scale_color_steps(
1473        self,
1474        low: crate::scale::color::RGBAColor,
1475        high: crate::scale::color::RGBAColor,
1476        n_bins: usize,
1477    ) -> Self {
1478        let s = crate::scale::steps::ScaleColorSteps::two(
1479            crate::aes::Aesthetic::Color,
1480            (low.r, low.g, low.b),
1481            (high.r, high.g, high.b),
1482            n_bins,
1483        );
1484        self.scale_color(s)
1485    }
1486
1487    /// Binned N-stop continuous colour scale.
1488    pub fn scale_color_stepsn(
1489        self,
1490        stops: Vec<crate::scale::color::RGBAColor>,
1491        n_bins: usize,
1492    ) -> Self {
1493        let s = crate::scale::steps::ScaleColorSteps::new(
1494            crate::aes::Aesthetic::Color,
1495            stops.iter().map(|c| (c.r, c.g, c.b)).collect(),
1496            n_bins,
1497        );
1498        self.scale_color(s)
1499    }
1500
1501    /// Binned ColorBrewer colour scale (R's `scale_color_fermenter`).
1502    pub fn scale_color_fermenter(
1503        self,
1504        name: crate::scale::palettes::PaletteName,
1505        n_bins: usize,
1506    ) -> Self {
1507        let stops = crate::scale::palettes::palette(&name)
1508            .iter()
1509            .map(|c| (c.r, c.g, c.b))
1510            .collect();
1511        let s =
1512            crate::scale::steps::ScaleColorSteps::new(crate::aes::Aesthetic::Color, stops, n_bins);
1513        self.scale_color(s)
1514    }
1515
1516    /// Binned (stepped) two-colour continuous fill scale.
1517    pub fn scale_fill_steps(
1518        self,
1519        low: crate::scale::color::RGBAColor,
1520        high: crate::scale::color::RGBAColor,
1521        n_bins: usize,
1522    ) -> Self {
1523        let s = crate::scale::steps::ScaleColorSteps::two(
1524            crate::aes::Aesthetic::Fill,
1525            (low.r, low.g, low.b),
1526            (high.r, high.g, high.b),
1527            n_bins,
1528        );
1529        self.scale_fill(s)
1530    }
1531
1532    /// Binned fill scale with *explicit* class breaks (`n + 1` increasing
1533    /// edges, `n` colours interpolated `low`→`high`) — R's
1534    /// `scale_fill_steps(breaks = ...)`, backing a `::CLASSES` choropleth
1535    /// (sipemu/ggplot-rs#180). The legend shows each class's trained count
1536    /// (`"10–50 (12)"`) and, when any value is `Value::Na`, a "no data" key
1537    /// in `na_color` (default grey50) — see
1538    /// [`ScaleFillBinned`](crate::scale::binned::ScaleFillBinned) for the
1539    /// `.na_color(..)`/`.na_label(..)` builders. The break maths itself
1540    /// (quantile/Jenks/equal-width/custom edges) is the caller's job; this
1541    /// only bins against the edges it's given.
1542    pub fn scale_fill_binned(
1543        self,
1544        breaks: Vec<f64>,
1545        low: crate::scale::color::RGBAColor,
1546        high: crate::scale::color::RGBAColor,
1547    ) -> Self {
1548        let s = crate::scale::binned::ScaleFillBinned::two(
1549            crate::aes::Aesthetic::Fill,
1550            breaks,
1551            (low.r, low.g, low.b),
1552            (high.r, high.g, high.b),
1553        );
1554        self.scale_fill(s)
1555    }
1556
1557    /// Like [`scale_fill_binned`](Self::scale_fill_binned), with an
1558    /// explicit colour per bin (`colors.len()` should be
1559    /// `breaks.len() - 1`) instead of a two-colour interpolation.
1560    pub fn scale_fill_binned_with(
1561        self,
1562        breaks: Vec<f64>,
1563        colors: Vec<crate::scale::color::RGBAColor>,
1564    ) -> Self {
1565        let s = crate::scale::binned::ScaleFillBinned::new(
1566            crate::aes::Aesthetic::Fill,
1567            breaks,
1568            colors.iter().map(|c| (c.r, c.g, c.b)).collect(),
1569        );
1570        self.scale_fill(s)
1571    }
1572
1573    /// Binned ColorBrewer fill scale.
1574    pub fn scale_fill_fermenter(
1575        self,
1576        name: crate::scale::palettes::PaletteName,
1577        n_bins: usize,
1578    ) -> Self {
1579        let stops = crate::scale::palettes::palette(&name)
1580            .iter()
1581            .map(|c| (c.r, c.g, c.b))
1582            .collect();
1583        let s =
1584            crate::scale::steps::ScaleColorSteps::new(crate::aes::Aesthetic::Fill, stops, n_bins);
1585        self.scale_fill(s)
1586    }
1587
1588    /// Default discrete fill palette with levels in sorted order, so a level
1589    /// keeps its colour across charts regardless of data order. For a custom
1590    /// palette: `scale_fill(ScaleColorDiscrete::new(Aesthetic::Fill)
1591    /// .with_palette(p).sorted())`.
1592    pub fn scale_fill_discrete_sorted(self) -> Self {
1593        use crate::scale::color::ScaleColorDiscrete;
1594        self.scale_fill(ScaleColorDiscrete::new(crate::aes::Aesthetic::Fill).sorted())
1595    }
1596
1597    /// Default discrete colour palette with levels in sorted order (see
1598    /// [`scale_fill_discrete_sorted`](Self::scale_fill_discrete_sorted)).
1599    pub fn scale_color_discrete_sorted(self) -> Self {
1600        use crate::scale::color::ScaleColorDiscrete;
1601        self.scale_color(ScaleColorDiscrete::new(crate::aes::Aesthetic::Color).sorted())
1602    }
1603
1604    pub fn scale_fill_brewer(self, name: crate::scale::palettes::PaletteName) -> Self {
1605        use crate::scale::color::ScaleColorDiscrete;
1606        let s = ScaleColorDiscrete::new(crate::aes::Aesthetic::Fill).with_named_palette(&name);
1607        self.scale_fill(s)
1608    }
1609
1610    pub fn scale_linetype_manual(
1611        self,
1612        values: Vec<(&str, crate::render::backend::Linetype)>,
1613    ) -> Self {
1614        let s = crate::scale::linetype_manual::ScaleLinetypeManual::new(values);
1615        self.scale_color(s)
1616    }
1617
1618    pub fn scale_shape_manual(
1619        self,
1620        values: Vec<(&str, crate::render::backend::PointShape)>,
1621    ) -> Self {
1622        let s = crate::scale::shape_manual::ScaleShapeManual::new(values);
1623        self.scale_color(s)
1624    }
1625
1626    pub fn scale_color_grey(self) -> Self {
1627        let s = crate::scale::grey::ScaleColorGrey::new(crate::aes::Aesthetic::Color);
1628        self.scale_color(s)
1629    }
1630
1631    pub fn scale_fill_grey(self) -> Self {
1632        let s = crate::scale::grey::ScaleColorGrey::new(crate::aes::Aesthetic::Fill);
1633        self.scale_fill(s)
1634    }
1635
1636    pub fn scale_color_grey_with(self, s: crate::scale::grey::ScaleColorGrey) -> Self {
1637        self.scale_color(s)
1638    }
1639
1640    pub fn scale_fill_grey_with(self, s: crate::scale::grey::ScaleColorGrey) -> Self {
1641        self.scale_fill(s)
1642    }
1643
1644    pub fn scale_x_reverse(self) -> Self {
1645        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Reverse))
1646    }
1647
1648    pub fn scale_y_reverse(self) -> Self {
1649        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Reverse))
1650    }
1651
1652    pub fn scale_x_datetime(mut self, s: crate::scale::datetime::ScaleDateTime) -> Self {
1653        let s = s.for_aesthetic(crate::aes::Aesthetic::X);
1654        self.scales.push(Box::new(s));
1655        self
1656    }
1657
1658    pub fn scale_y_datetime(mut self, s: crate::scale::datetime::ScaleDateTime) -> Self {
1659        let s = s.for_aesthetic(crate::aes::Aesthetic::Y);
1660        self.scales.push(Box::new(s));
1661        self
1662    }
1663
1664    pub fn scale_size(mut self, s: crate::scale::size::ScaleSizeContinuous) -> Self {
1665        self.scales.push(Box::new(s));
1666        self
1667    }
1668
1669    /// `scale_size_area(max_size)` (ggplot2, B3-6): size scale where
1670    /// *area*, not radius, is proportional to the value, and 0 maps to 0.
1671    /// See [`ScaleSizeContinuous::area`](crate::scale::size::ScaleSizeContinuous::area).
1672    pub fn scale_size_area(mut self, max_size: f64) -> Self {
1673        self.scales.push(Box::new(
1674            crate::scale::size::ScaleSizeContinuous::new().area(max_size),
1675        ));
1676        self
1677    }
1678
1679    pub fn scale_alpha(mut self, s: crate::scale::alpha::ScaleAlphaContinuous) -> Self {
1680        self.scales.push(Box::new(s));
1681        self
1682    }
1683
1684    /// Continuous linewidth scale for the `linewidth` aesthetic (segment,
1685    /// curve, path, line) — see
1686    /// [`ScaleLinewidthContinuous`](crate::scale::linewidth::ScaleLinewidthContinuous).
1687    pub fn scale_linewidth(mut self, s: crate::scale::linewidth::ScaleLinewidthContinuous) -> Self {
1688        self.scales.push(Box::new(s));
1689        self
1690    }
1691
1692    pub fn xlim(self, min: f64, max: f64) -> Self {
1693        self.scale_x_continuous(ScaleContinuous::new().with_limits(min, max))
1694    }
1695
1696    pub fn ylim(self, min: f64, max: f64) -> Self {
1697        self.scale_y_continuous(ScaleContinuous::new().with_limits(min, max))
1698    }
1699
1700    pub fn scale_x_log10(self) -> Self {
1701        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Log10))
1702    }
1703
1704    pub fn scale_y_log10(self) -> Self {
1705        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Log10))
1706    }
1707
1708    pub fn scale_x_sqrt(self) -> Self {
1709        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Sqrt))
1710    }
1711
1712    pub fn scale_y_sqrt(self) -> Self {
1713        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Sqrt))
1714    }
1715
1716    pub fn scale_x_log2(self) -> Self {
1717        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Log2))
1718    }
1719
1720    pub fn scale_y_log2(self) -> Self {
1721        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Log2))
1722    }
1723
1724    pub fn scale_x_ln(self) -> Self {
1725        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Ln))
1726    }
1727
1728    pub fn scale_y_ln(self) -> Self {
1729        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Ln))
1730    }
1731
1732    /// Logit-transformed x axis (for proportions in (0, 1)).
1733    pub fn scale_x_logit(self) -> Self {
1734        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Logit))
1735    }
1736
1737    pub fn scale_y_logit(self) -> Self {
1738        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Logit))
1739    }
1740
1741    /// Probit-transformed x axis (inverse normal CDF, for proportions in (0, 1)).
1742    pub fn scale_x_probit(self) -> Self {
1743        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Probit))
1744    }
1745
1746    pub fn scale_y_probit(self) -> Self {
1747        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Probit))
1748    }
1749
1750    /// Sign-preserving pseudo-log x axis (handles zero and negative values).
1751    pub fn scale_x_pseudo_log(self) -> Self {
1752        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::PseudoLog))
1753    }
1754
1755    pub fn scale_y_pseudo_log(self) -> Self {
1756        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::PseudoLog))
1757    }
1758
1759    /// Reciprocal (1/x) x axis.
1760    pub fn scale_x_reciprocal(self) -> Self {
1761        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Reciprocal))
1762    }
1763
1764    pub fn scale_y_reciprocal(self) -> Self {
1765        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Reciprocal))
1766    }
1767
1768    /// Exponential x axis (labels spaced logarithmically).
1769    pub fn scale_x_exp(self) -> Self {
1770        self.scale_x_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Exp))
1771    }
1772
1773    pub fn scale_y_exp(self) -> Self {
1774        self.scale_y_continuous(ScaleContinuous::new().with_transform(ScaleTransform::Exp))
1775    }
1776
1777    /// Box–Cox x axis with the given lambda (x > 0).
1778    pub fn scale_x_boxcox(self, lambda: f64) -> Self {
1779        self.scale_x_continuous(
1780            ScaleContinuous::new().with_transform(ScaleTransform::BoxCox(lambda)),
1781        )
1782    }
1783
1784    pub fn scale_y_boxcox(self, lambda: f64) -> Self {
1785        self.scale_y_continuous(
1786            ScaleContinuous::new().with_transform(ScaleTransform::BoxCox(lambda)),
1787        )
1788    }
1789
1790    // ─── Faceting ─────────────────────────────────────────────────
1791
1792    pub fn facet_wrap(mut self, var: &str, ncol: Option<usize>) -> Self {
1793        self.facet = Facet::Wrap {
1794            var: var.to_string(),
1795            ncol,
1796            scales: FacetScales::Fixed,
1797            labeller: FacetLabeller::default(),
1798        };
1799        self
1800    }
1801
1802    pub fn facet_wrap_free(mut self, var: &str, ncol: Option<usize>, scales: FacetScales) -> Self {
1803        self.facet = Facet::Wrap {
1804            var: var.to_string(),
1805            ncol,
1806            scales,
1807            labeller: FacetLabeller::default(),
1808        };
1809        self
1810    }
1811
1812    pub fn facet_wrap_labeller(
1813        mut self,
1814        var: &str,
1815        ncol: Option<usize>,
1816        labeller: FacetLabeller,
1817    ) -> Self {
1818        self.facet = Facet::Wrap {
1819            var: var.to_string(),
1820            ncol,
1821            scales: FacetScales::Fixed,
1822            labeller,
1823        };
1824        self
1825    }
1826
1827    pub fn facet_grid(mut self, row: Option<&str>, col: Option<&str>) -> Self {
1828        self.facet = Facet::Grid {
1829            row_var: row.map(String::from),
1830            col_var: col.map(String::from),
1831            scales: FacetScales::Fixed,
1832            labeller: FacetLabeller::default(),
1833            space: FacetSpace::Fixed,
1834        };
1835        self
1836    }
1837
1838    pub fn facet_grid_free(
1839        mut self,
1840        row: Option<&str>,
1841        col: Option<&str>,
1842        scales: FacetScales,
1843    ) -> Self {
1844        self.facet = Facet::Grid {
1845            row_var: row.map(String::from),
1846            col_var: col.map(String::from),
1847            scales,
1848            labeller: FacetLabeller::default(),
1849            space: FacetSpace::Fixed,
1850        };
1851        self
1852    }
1853
1854    /// `facet_grid` over **multiple** column variables (R's `rows ~ b + c`):
1855    /// the columns become the combination of the given variables' values.
1856    /// Also accepts free scales + proportional `space` for full parity.
1857    pub fn facet_grid_multi(
1858        mut self,
1859        row: Option<&str>,
1860        cols: &[&str],
1861        scales: FacetScales,
1862        space: FacetSpace,
1863    ) -> Self {
1864        let col_var = if cols.len() > 1 {
1865            // Build a synthetic column that is the interaction of the col vars.
1866            let n = self.data.nrows();
1867            let mut combined = Vec::with_capacity(n);
1868            for i in 0..n {
1869                let parts: Vec<String> = cols
1870                    .iter()
1871                    .map(|c| {
1872                        self.data
1873                            .column(c)
1874                            .and_then(|col| col.get(i))
1875                            .map(|v| v.to_group_key())
1876                            .unwrap_or_default()
1877                    })
1878                    .collect();
1879                combined.push(crate::data::Value::Str(parts.join(" . ")));
1880            }
1881            let name = "__facet_cols__".to_string();
1882            self.data.add_column(name.clone(), combined);
1883            Some(name)
1884        } else {
1885            cols.first().map(|s| s.to_string())
1886        };
1887        self.facet = Facet::Grid {
1888            row_var: row.map(String::from),
1889            col_var,
1890            scales,
1891            labeller: FacetLabeller::default(),
1892            space,
1893        };
1894        self
1895    }
1896
1897    /// `facet_grid` with proportional panel sizing (R's `space =`): panels are
1898    /// sized to their data range. Typically paired with free scales.
1899    pub fn facet_grid_space(
1900        mut self,
1901        row: Option<&str>,
1902        col: Option<&str>,
1903        scales: FacetScales,
1904        space: FacetSpace,
1905    ) -> Self {
1906        self.facet = Facet::Grid {
1907            row_var: row.map(String::from),
1908            col_var: col.map(String::from),
1909            scales,
1910            labeller: FacetLabeller::default(),
1911            space,
1912        };
1913        self
1914    }
1915
1916    pub fn facet_grid_labeller(
1917        mut self,
1918        row: Option<&str>,
1919        col: Option<&str>,
1920        labeller: FacetLabeller,
1921    ) -> Self {
1922        self.facet = Facet::Grid {
1923            row_var: row.map(String::from),
1924            col_var: col.map(String::from),
1925            scales: FacetScales::Fixed,
1926            labeller,
1927            space: FacetSpace::Fixed,
1928        };
1929        self
1930    }
1931
1932    // ─── Coordinates ─────────────────────────────────────────────
1933
1934    pub fn coord_flip(mut self) -> Self {
1935        self.coord = Box::new(CoordFlip);
1936        self
1937    }
1938
1939    pub fn coord_fixed(mut self, ratio: f64) -> Self {
1940        self.coord = Box::new(CoordFixed::new(ratio));
1941        self
1942    }
1943
1944    /// Spatial coordinate system (feature `sf`): equal aspect derived from the
1945    /// data extent, so projected geometry keeps its shape. Pair with a `geom_sf`
1946    /// projection (e.g. Mercator) for a conformal map.
1947    #[cfg(feature = "sf")]
1948    pub fn coord_sf(mut self) -> Self {
1949        self.coord = Box::new(crate::coord::sf::CoordSf::new());
1950        self
1951    }
1952
1953    /// Spatial coordinate system with explicit settings (`xlim`/`ylim`/
1954    /// `expand`) — see [`CoordSf`](crate::coord::sf::CoordSf). Used by
1955    /// consumers overlaying a fragment on their own projected basemap: a
1956    /// fixed pixel window via `xlim`/`ylim` and `expand(false)`.
1957    #[cfg(feature = "sf")]
1958    pub fn coord_sf_with(mut self, coord: crate::coord::sf::CoordSf) -> Self {
1959        self.coord = Box::new(coord);
1960        self
1961    }
1962
1963    /// Whether the rendered root `<svg>` carries `data-plot`/`data-domain`/
1964    /// `data-xdomain`/… (see [`root_data_attrs`](crate::render::svg_backend::root_data_attrs)).
1965    /// Default `true`. A consumer that overlays a rendered fragment
1966    /// (`render_svg_native_at`) on its own basemap — which already has its
1967    /// own root attributes and window — turns this off so the fragment adds
1968    /// none of its own.
1969    pub fn svg_root_data(mut self, on: bool) -> Self {
1970        self.svg_root_data = on;
1971        self
1972    }
1973
1974    /// Add one extra attribute to the root `<svg>` element — a host
1975    /// identifier, an ARIA label, a CSS hook, and so on. Allowed names:
1976    /// `data-*`, `aria-*`, `role`, `id`, `class`, `lang`; anything else, or
1977    /// a name ggplot-rs already writes to the root (`data-plot`,
1978    /// `data-domain`, …, or `data-warnings` when
1979    /// [`svg_warnings_attr`](Self::svg_warnings_attr) is also on), is
1980    /// ignored with a build warning instead of being silently dropped or
1981    /// duplicated. Values are escaped on output. PlotGrid has the same API
1982    /// ([`PlotGrid::svg_attr`](crate::compose::PlotGrid::svg_attr)).
1983    pub fn svg_attr(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
1984        self.root_attrs.push((name.into(), value.into()));
1985        self
1986    }
1987
1988    /// [`svg_attr`](Self::svg_attr) for several attributes at once.
1989    pub fn svg_attrs(mut self, attrs: impl IntoIterator<Item = (String, String)>) -> Self {
1990        self.root_attrs.extend(attrs);
1991        self
1992    }
1993
1994    /// Emit a `data-warnings` attribute on the root `<svg>` — a JSON array
1995    /// of the build warnings (rows dropped for non-finite positions, an
1996    /// `svg_attr` name rejected, …), `[]` when there are none. Default off,
1997    /// and omitted entirely when off (byte-identical to not calling this).
1998    pub fn svg_warnings_attr(mut self, on: bool) -> Self {
1999        self.svg_warnings_attr = on;
2000        self
2001    }
2002
2003    /// Emit `data-fvalue`/`data-fx` on marks: the value aesthetic's raw
2004    /// data value (y for most geoms, fill for hex/tile) and the x value,
2005    /// each formatted through its scale (currency/locale numbers, ISO or
2006    /// locale dates — [`Scale::format_value`](crate::scale::Scale::format_value))
2007    /// rather than the plain display string `data-value`/`data-x` already
2008    /// carry. `data-fx` is omitted when it would just duplicate `data-x`.
2009    /// Default off, and omitted entirely when off (byte-identical to not
2010    /// calling this).
2011    pub fn svg_formatted_values(mut self, on: bool) -> Self {
2012        self.svg_formatted_values = on;
2013        self
2014    }
2015
2016    /// Give the rendered root `<svg>` an accessible name: a `<title>`
2017    /// written (escaped) as its first child, plus `role="img"` and
2018    /// `aria-labelledby` pointing at that `<title>`'s id — the SVG
2019    /// accessibility practice of naming an image via `role="img"` +
2020    /// `aria-labelledby`/`-describedby` rather than a bare `<title>`,
2021    /// which some screen readers treat as a tooltip instead of a name.
2022    /// Pairs with [`svg_desc`](Self::svg_desc) for a longer description;
2023    /// either may be set alone. Default off (`None`), so output stays
2024    /// byte-identical when this is never called.
2025    pub fn svg_title(mut self, title: impl Into<String>) -> Self {
2026        self.svg_title = Some(title.into());
2027        self
2028    }
2029
2030    /// Give the rendered root `<svg>` an accessible description: a
2031    /// `<desc>` written (escaped) as a first child (after `<title>`, if
2032    /// [`svg_title`](Self::svg_title) is also set), plus `role="img"` and
2033    /// `aria-describedby` pointing at that `<desc>`'s id — e.g. a data
2034    /// summary like "4 regions; highest Bavaria (5.2); 1 without data".
2035    /// Default off (`None`), so output stays byte-identical when this is
2036    /// never called.
2037    pub fn svg_desc(mut self, desc: impl Into<String>) -> Self {
2038        self.svg_desc = Some(desc.into());
2039        self
2040    }
2041
2042    /// Transform the coordinate space at draw time (R's `coord_trans`) — stats are
2043    /// computed on raw data but drawn on non-linear axes. Pass a per-axis
2044    /// [`ScaleTransform`] (e.g. `Some(ScaleTransform::Log10)`), `None` to leave an
2045    /// axis linear.
2046    pub fn coord_trans(mut self, x: Option<ScaleTransform>, y: Option<ScaleTransform>) -> Self {
2047        self.coord = Box::new(crate::coord::trans::CoordTrans::new(x, y));
2048        self
2049    }
2050
2051    /// `coord_trans` on the y-axis only.
2052    pub fn coord_trans_y(self, y: ScaleTransform) -> Self {
2053        self.coord_trans(None, Some(y))
2054    }
2055
2056    /// `coord_trans` on the x-axis only.
2057    pub fn coord_trans_x(self, x: ScaleTransform) -> Self {
2058        self.coord_trans(Some(x), None)
2059    }
2060
2061    /// Zoom into a region without filtering data (unlike xlim/ylim which filter).
2062    pub fn coord_cartesian_zoom(
2063        mut self,
2064        xlim: Option<(f64, f64)>,
2065        ylim: Option<(f64, f64)>,
2066    ) -> Self {
2067        let mut c = CoordCartesian::new();
2068        if let Some((min, max)) = xlim {
2069            c = c.xlim(min, max);
2070        }
2071        if let Some((min, max)) = ylim {
2072            c = c.ylim(min, max);
2073        }
2074        self.coord = Box::new(c);
2075        self
2076    }
2077
2078    pub fn coord_polar(mut self) -> Self {
2079        self.coord = Box::new(CoordPolar::new());
2080        self
2081    }
2082
2083    /// Radar / spider chart coordinates: discrete `x` → spokes, `y` → distance
2084    /// from the centre, straight segments, ring/spoke guides. Pair with
2085    /// `geom_polygon` (closed series) and `geom_point`.
2086    pub fn coord_radar(mut self) -> Self {
2087        self.coord = Box::new(crate::coord::radar::CoordRadar::new());
2088        self
2089    }
2090
2091    pub fn coord_radar_with(mut self, coord: crate::coord::radar::CoordRadar) -> Self {
2092        self.coord = Box::new(coord);
2093        self
2094    }
2095
2096    pub fn coord_polar_with(mut self, coord: CoordPolar) -> Self {
2097        self.coord = Box::new(coord);
2098        self
2099    }
2100
2101    // ─── Theme ───────────────────────────────────────────────────
2102
2103    /// Replace the theme. A `primary_color` set earlier is kept unless the new
2104    /// theme sets its own, so preset/`primary_color` call order doesn't matter.
2105    pub fn theme(mut self, theme: Theme) -> Self {
2106        self.apply_preset(theme);
2107        self
2108    }
2109
2110    fn apply_preset(&mut self, theme: Theme) {
2111        let primary = self.theme.primary;
2112        self.theme = theme;
2113        if self.theme.primary.is_none() {
2114            self.theme.primary = primary;
2115        }
2116    }
2117
2118    /// Rotate the x-axis tick labels by `degrees` (R's
2119    /// `guides(x = guide_axis(angle = ...))` / `axis.text.x = element_text(angle)`).
2120    /// Useful for long category labels. Call after any `theme_*()` preset.
2121    pub fn axis_text_x_angle(mut self, degrees: f64) -> Self {
2122        self.theme.axis_text_x.angle = degrees;
2123        self
2124    }
2125
2126    /// Rotate the y-axis tick labels by `degrees`.
2127    pub fn axis_text_y_angle(mut self, degrees: f64) -> Self {
2128        self.theme.axis_text_y.angle = degrees;
2129        self
2130    }
2131
2132    /// Stagger x-axis tick labels across `n` rows to avoid overlap (R's
2133    /// `guides(x = guide_axis(n.dodge = n))`). `1` = no dodging.
2134    pub fn axis_text_x_dodge(mut self, n: usize) -> Self {
2135        self.theme.axis_text_x_dodge = n.max(1);
2136        self
2137    }
2138
2139    /// Fix the panel's height:width ratio (R's `aspect.ratio`). Call after any
2140    /// `theme_*()` preset.
2141    pub fn aspect_ratio(mut self, ratio: f64) -> Self {
2142        self.theme.aspect_ratio = Some(ratio);
2143        self
2144    }
2145
2146    /// Draw gridlines on top of the data layers (R's `panel.ontop`).
2147    pub fn panel_ontop(mut self) -> Self {
2148        self.theme.panel_ontop = true;
2149        self
2150    }
2151
2152    /// Draw minor tick marks between major ticks (R's `axis.minor.ticks`).
2153    pub fn axis_minor_ticks(mut self) -> Self {
2154        self.theme.axis_minor_ticks = true;
2155        self
2156    }
2157
2158    /// How an outer x-axis label that would cross the SVG edge is handled
2159    /// (B3-1, D6): [`AxisLabelFit::Reserve`](crate::theme::AxisLabelFit::Reserve) (default), `Shift`, or `Clip`
2160    /// (the 0.21 behaviour). Use `Shift` for panels that must stay
2161    /// pixel-aligned with other elements (e.g. a host's own aligned strip).
2162    pub fn axis_text_x_fit(mut self, fit: crate::theme::AxisLabelFit) -> Self {
2163        self.theme.axis_text_x_fit = fit;
2164        self
2165    }
2166
2167    /// Thin crowded x-axis labels in ggplot2's `check.overlap` priority
2168    /// order (B3-1, D5). Off by default.
2169    pub fn axis_text_x_check_overlap(mut self, on: bool) -> Self {
2170        self.theme.axis_text_x_check_overlap = on;
2171        self
2172    }
2173
2174    /// Align title/subtitle/caption to the panel or the whole plot width
2175    /// (R's `plot.title.position`).
2176    pub fn title_position(mut self, pos: crate::theme::TitlePosition) -> Self {
2177        self.theme.title_position = pos;
2178        self
2179    }
2180
2181    /// Corner for the `tag()` label (R's `plot.tag.position`).
2182    pub fn tag_position(mut self, pos: crate::theme::TagPosition) -> Self {
2183        self.theme.tag_position = pos;
2184        self
2185    }
2186
2187    /// Force the legend key layout direction (R's `legend.direction`).
2188    pub fn legend_direction(mut self, dir: crate::theme::LegendDirection) -> Self {
2189        self.theme.legend_direction = Some(dir);
2190        self
2191    }
2192
2193    /// Set the brand/primary color used as the default for single-series geoms
2194    /// that have no color/fill aesthetic mapped. Composes with any theme — one
2195    /// render process can serve different tenants' brands at render time.
2196    /// Place the legend inside the panel at panel-relative coordinates
2197    /// (0..1, 0..1) — `(0,0)` bottom-left, `(1,1)` top-right (R's
2198    /// `legend.position = c(x, y)`).
2199    pub fn legend_position_inside(mut self, x: f64, y: f64) -> Self {
2200        self.theme.legend_position = crate::theme::LegendPosition::Inside(x, y);
2201        self
2202    }
2203
2204    /// Set the legend position (R's `legend.position`, e.g. `Top`/`Bottom`).
2205    pub fn legend_position(mut self, pos: crate::theme::LegendPosition) -> Self {
2206        self.theme.legend_position = pos;
2207        self
2208    }
2209
2210    pub fn primary_color(mut self, color: (u8, u8, u8)) -> Self {
2211        self.theme.primary = Some(color);
2212        self
2213    }
2214
2215    pub fn theme_minimal(mut self) -> Self {
2216        self.apply_preset(crate::theme::presets::theme_minimal());
2217        self
2218    }
2219
2220    pub fn theme_bw(mut self) -> Self {
2221        self.apply_preset(crate::theme::presets::theme_bw());
2222        self
2223    }
2224
2225    pub fn theme_gray(mut self) -> Self {
2226        self.apply_preset(crate::theme::presets::theme_gray());
2227        self
2228    }
2229
2230    pub fn theme_classic(mut self) -> Self {
2231        self.apply_preset(crate::theme::presets::theme_classic());
2232        self
2233    }
2234
2235    /// Apply the publication-ready `theme_pubr()` (ggpubr style).
2236    pub fn theme_pubr(mut self) -> Self {
2237        self.apply_preset(crate::theme::presets::theme_pubr());
2238        self
2239    }
2240
2241    pub fn theme_linedraw(mut self) -> Self {
2242        self.apply_preset(crate::theme::presets::theme_linedraw());
2243        self
2244    }
2245
2246    pub fn theme_light(mut self) -> Self {
2247        self.apply_preset(crate::theme::presets::theme_light());
2248        self
2249    }
2250
2251    pub fn theme_dark(mut self) -> Self {
2252        self.apply_preset(crate::theme::presets::theme_dark());
2253        self
2254    }
2255
2256    pub fn theme_void(mut self) -> Self {
2257        self.apply_preset(crate::theme::presets::theme_void());
2258        self
2259    }
2260
2261    /// Apply incremental theme modifications on top of the current theme.
2262    /// Like R's `+ theme(axis.text.x = element_text(...))`.
2263    pub fn theme_update(mut self, update: crate::theme::ThemeUpdate) -> Self {
2264        self.theme = self.theme.update(update);
2265        self
2266    }
2267
2268    // ─── Guides ──────────────────────────────────────────────────
2269
2270    /// Configure legend guide (title, ncol, reverse).
2271    pub fn guides(mut self, guide: crate::guide::config::GuideLegend) -> Self {
2272        self.guide_legend = guide;
2273        self
2274    }
2275
2276    // ─── Labels ──────────────────────────────────────────────────
2277
2278    pub fn labs(mut self, labels: Labels) -> Self {
2279        if labels.title.is_some() {
2280            self.labels.title = labels.title;
2281        }
2282        if labels.subtitle.is_some() {
2283            self.labels.subtitle = labels.subtitle;
2284        }
2285        if labels.x.is_some() {
2286            self.labels.x = labels.x;
2287        }
2288        if labels.y.is_some() {
2289            self.labels.y = labels.y;
2290        }
2291        if labels.caption.is_some() {
2292            self.labels.caption = labels.caption;
2293        }
2294        if labels.tag.is_some() {
2295            self.labels.tag = labels.tag;
2296        }
2297        self
2298    }
2299
2300    pub fn title(mut self, title: &str) -> Self {
2301        self.labels.title = Some(title.to_string());
2302        self
2303    }
2304
2305    /// Corner tag label (R's `labs(tag = ...)`), drawn at the top-left — handy
2306    /// for labelling figure panels ("A", "B", …).
2307    pub fn tag(mut self, tag: &str) -> Self {
2308        self.labels.tag = Some(tag.to_string());
2309        self
2310    }
2311
2312    pub fn subtitle(mut self, subtitle: &str) -> Self {
2313        self.labels.subtitle = Some(subtitle.to_string());
2314        self
2315    }
2316
2317    pub fn xlab(mut self, label: &str) -> Self {
2318        self.labels.x = Some(label.to_string());
2319        self
2320    }
2321
2322    pub fn ylab(mut self, label: &str) -> Self {
2323        self.labels.y = Some(label.to_string());
2324        self
2325    }
2326
2327    pub fn caption(mut self, caption: &str) -> Self {
2328        self.labels.caption = Some(caption.to_string());
2329        self
2330    }
2331
2332    // ─── Annotations ──────────────────────────────────────────────
2333
2334    /// Add an annotation to the plot.
2335    pub fn annotate(mut self, annotation: Annotation) -> Self {
2336        self.annotations.push(annotation);
2337        self
2338    }
2339
2340    /// Add a text annotation at data coordinates.
2341    pub fn annotate_text(self, label: &str, x: f64, y: f64) -> Self {
2342        self.annotate(Annotation::text(label, x, y))
2343    }
2344
2345    /// Add a rectangle annotation at data coordinates.
2346    pub fn annotate_rect(self, xmin: f64, xmax: f64, ymin: f64, ymax: f64) -> Self {
2347        self.annotate(Annotation::rect(xmin, xmax, ymin, ymax))
2348    }
2349
2350    /// Add a segment annotation between data coordinates.
2351    pub fn annotate_segment(self, x: f64, y: f64, xend: f64, yend: f64) -> Self {
2352        self.annotate(Annotation::segment(x, y, xend, yend))
2353    }
2354
2355    /// Default [`Locale`](crate::locale::Locale) for the plot's axis and
2356    /// legend labels: every continuous position / colour / size / alpha scale
2357    /// labels its default breaks with the locale's decimal and thousands marks
2358    /// (`1.000`, `2,5` in de-DE), and date/datetime scales use its month names
2359    /// and date formats (`Okt. 2026`, `15. Okt. 2026`). A scale's own
2360    /// `with_locale` and custom `label_*` formatters take precedence.
2361    /// Without this call labels are unchanged (`.` decimal mark, no grouping,
2362    /// ISO dates).
2363    ///
2364    /// ```
2365    /// use ggplot_rs::prelude::*;
2366    /// let svg = GGPlot::new(vec![
2367    ///         ("x".to_string(), vec![Value::Float(0.0), Value::Float(3000.0)]),
2368    ///         ("y".to_string(), vec![Value::Float(0.5), Value::Float(2.5)]),
2369    ///     ])
2370    ///     .aes(Aes::new().x("x").y("y"))
2371    ///     .geom_point()
2372    ///     .locale(Locale::from_tag("de-DE"))
2373    ///     .render_svg_native()
2374    ///     .unwrap();
2375    /// assert!(svg.contains(">1.000<") && svg.contains(">1,5<"));
2376    /// ```
2377    pub fn locale(mut self, locale: crate::locale::Locale) -> Self {
2378        self.locale = Some(locale);
2379        self
2380    }
2381
2382    // ─── Build and Render ────────────────────────────────────────
2383
2384    /// Build the plot without rendering, returning errors on validation failure.
2385    pub fn try_build(self) -> Result<crate::build::BuiltPlot, GGError> {
2386        PlotBuilder::build(self)
2387    }
2388
2389    /// Build the plot without rendering (analogous to R's ggplot_build()).
2390    /// Returns the fully computed BuiltPlot with layer data ready for inspection.
2391    /// Panics on validation errors — use `try_build()` for error handling.
2392    pub fn build(self) -> crate::build::BuiltPlot {
2393        self.try_build().expect("plot build failed")
2394    }
2395
2396    /// Build and save the plot to a file. Format determined by extension.
2397    /// (Requires the `plotters` feature, on by default; native only — wasm has
2398    /// no filesystem/plotters backend.)
2399    #[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
2400    pub fn save(self, path: &str) -> Result<(), GGError> {
2401        self.save_with_size(path, 800, 600)
2402    }
2403
2404    /// Build and save with custom dimensions. (Feature `plotters`; native only.)
2405    #[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
2406    pub fn save_with_size(self, path: &str, w: u32, h: u32) -> Result<(), GGError> {
2407        let (built, layout) = self.prepare(w, h)?;
2408
2409        // Determine backend from file extension
2410        let ext = path.rsplit('.').next().unwrap_or("svg").to_lowercase();
2411
2412        match ext.as_str() {
2413            "svg" => {
2414                let backend = plotters::prelude::SVGBackend::new(path, (w, h));
2415                Self::render_into(backend.into_drawing_area(), &built, &layout)?;
2416            }
2417            "png" | "bmp" | "gif" | "jpeg" | "jpg" | "tiff" => {
2418                let backend = plotters::prelude::BitMapBackend::new(path, (w, h));
2419                Self::render_into(backend.into_drawing_area(), &built, &layout)?;
2420            }
2421            _ => {
2422                return Err(GGError::UnsupportedFormat(ext));
2423            }
2424        }
2425
2426        Ok(())
2427    }
2428
2429    /// Render the plot to an in-memory SVG document (default 800x600).
2430    ///
2431    /// Unlike [`save`](Self::save), this writes nothing to disk — handy for
2432    /// serving charts from a web/MCP service. Requires the `plotters` feature
2433    /// (on by default); without it use [`render_svg_native`](Self::render_svg_native).
2434    #[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
2435    pub fn render_svg(self) -> Result<String, GGError> {
2436        self.render_svg_with_size(800, 600)
2437    }
2438
2439    /// Render the plot to an in-memory SVG document with custom dimensions.
2440    /// (Feature `plotters`; native only — on wasm or without `plotters` use
2441    /// [`render_svg_native_with_size`](Self::render_svg_native_with_size).)
2442    #[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
2443    pub fn render_svg_with_size(self, w: u32, h: u32) -> Result<String, GGError> {
2444        let (built, layout) = self.prepare(w, h)?;
2445        let mut buf = String::new();
2446        {
2447            let backend = plotters::prelude::SVGBackend::with_string(&mut buf, (w, h));
2448            Self::render_into(backend.into_drawing_area(), &built, &layout)?;
2449        }
2450        Ok(buf)
2451    }
2452
2453    /// Render to SVG through the self-contained [`SvgBackend`](crate::render::svg_backend::SvgBackend)
2454    /// — a plotters-free path that drives the same `PlotRenderer`, proving the
2455    /// `DrawBackend` abstraction (and needing no glyph rasterization).
2456    pub fn render_svg_native(self) -> Result<String, GGError> {
2457        self.render_svg_native_with_size(800, 600)
2458    }
2459
2460    /// [`render_svg_native`](Self::render_svg_native) with an explicit size.
2461    ///
2462    /// The root `<svg>` carries `data-plot="x y w h"` (the panel rect) and the
2463    /// trained position domains (`data-domain`, `data-xdomain`, `data-ydomain`,
2464    /// `data-xlevels`, `data-ylevels` — see
2465    /// [`root_data_attrs`](crate::render::svg_backend::root_data_attrs)).
2466    /// Hoverable marks carry `data-x`, `data-series` and `data-value`.
2467    pub fn render_svg_native_with_size(self, w: u32, h: u32) -> Result<String, GGError> {
2468        Ok(self.render_native(w, h)?.0.finish())
2469    }
2470
2471    /// Like [`render_svg_native_with_size`](Self::render_svg_native_with_size),
2472    /// but also returns the build warnings (rows dropped for non-finite
2473    /// positions, layers whose stat produced no data, …) — see
2474    /// [`BuiltPlot::warnings`](crate::build::BuiltPlot::warnings).
2475    pub fn render_svg_native_with_warnings(
2476        self,
2477        w: u32,
2478        h: u32,
2479    ) -> Result<(String, Vec<String>), GGError> {
2480        let (backend, warnings, _) = self.render_native(w, h)?;
2481        Ok((backend.finish(), warnings))
2482    }
2483
2484    /// Render a `w`×`h` chart as a *nested* `<svg x=.. y=.. width=.. height=..
2485    /// viewBox="0 0 w h">` fragment for composition into a larger SVG (a
2486    /// dashboard): no `xmlns` (inherited from the parent), positioned at
2487    /// `(x, y)` in the parent's user space. No string surgery needed:
2488    ///
2489    /// ```ignore
2490    /// let mut page = String::from(r#"<svg xmlns="http://www.w3.org/2000/svg" width="900" height="400">"#);
2491    /// page += &plot_a.render_svg_native_at(0.0, 0.0, 450, 400)?;
2492    /// page += &plot_b.render_svg_native_at(450.0, 0.0, 450, 400)?;
2493    /// page += "</svg>";
2494    /// ```
2495    pub fn render_svg_native_at(self, x: f64, y: f64, w: u32, h: u32) -> Result<String, GGError> {
2496        Ok(self.render_native(w, h)?.0.finish_fragment(x, y))
2497    }
2498
2499    /// Like [`render_svg_native_with_size`](Self::render_svg_native_with_size),
2500    /// but also returns the panel rect in pixels `[x, y, w, h]` — enough to
2501    /// overlay a WebGL/canvas layer that draws marks in data coordinates.
2502    pub fn render_svg_area_with_size(self, w: u32, h: u32) -> Result<(String, [f64; 4]), GGError> {
2503        let (backend, _, pa) = self.render_native(w, h)?;
2504        Ok((backend.finish(), [pa.x, pa.y, pa.width, pa.height]))
2505    }
2506
2507    /// Shared native-SVG pipeline: build, lay out, render into an
2508    /// [`SvgBackend`](crate::render::svg_backend::SvgBackend) (not yet
2509    /// finished), returning it with the build warnings and the panel rect.
2510    fn render_native(
2511        self,
2512        w: u32,
2513        h: u32,
2514    ) -> Result<
2515        (
2516            crate::render::svg_backend::SvgBackend,
2517            Vec<String>,
2518            crate::render::Rect,
2519        ),
2520        GGError,
2521    > {
2522        let svg_root_data = self.svg_root_data;
2523        let root_attrs = self.root_attrs.clone();
2524        let svg_warnings_attr = self.svg_warnings_attr;
2525        let svg_formatted_values = self.svg_formatted_values;
2526        let svg_title = self.svg_title.clone();
2527        let svg_desc = self.svg_desc.clone();
2528        let (built, layout) = self.prepare(w, h)?;
2529        let pa = layout.plot_area.clone();
2530        let mut backend = crate::render::svg_backend::SvgBackend::new(w, h, pa.clone());
2531        backend.set_formatted_values(svg_formatted_values);
2532        backend.set_svg_title(svg_title.clone());
2533        backend.set_svg_desc(svg_desc.clone());
2534        let existing_root_attrs = if svg_root_data {
2535            let attrs = crate::render::svg_backend::root_data_attrs(&built);
2536            backend.set_root_attrs(attrs.clone());
2537            // `data-plot` itself is a literal in `root_open`, not part of
2538            // `root_data_attrs`'s output, but it is written whenever
2539            // `svg_root_data` is on — reserve it too.
2540            let mut reserved = attrs;
2541            reserved.push(("data-plot".to_string(), String::new()));
2542            reserved
2543        } else {
2544            backend.without_plot_attr();
2545            Vec::new()
2546        };
2547        PlotRenderer::render(&built, &mut backend).map_err(GGError::Render)?;
2548        let mut warnings = built.warnings;
2549        warnings.extend(backend.take_warnings());
2550
2551        let mut reserved = existing_root_attrs;
2552        if svg_warnings_attr {
2553            reserved.push(("data-warnings".to_string(), String::new()));
2554        }
2555        // #255: `role`/`aria-labelledby`/`aria-describedby` are written
2556        // directly by `SvgBackend::root_open` when a title/desc was set —
2557        // reserve them so a colliding `svg_attr` call is dropped with a
2558        // warning instead of duplicating the attribute.
2559        if svg_title.is_some() || svg_desc.is_some() {
2560            reserved.push(("role".to_string(), String::new()));
2561        }
2562        if svg_title.is_some() {
2563            reserved.push(("aria-labelledby".to_string(), String::new()));
2564        }
2565        if svg_desc.is_some() {
2566            reserved.push(("aria-describedby".to_string(), String::new()));
2567        }
2568        let extra = crate::render::svg_backend::validate_root_attrs(
2569            &root_attrs,
2570            &reserved,
2571            "plot",
2572            &mut warnings,
2573        );
2574        backend.extend_root_attrs(extra);
2575        if svg_warnings_attr {
2576            backend.extend_root_attrs(vec![(
2577                "data-warnings".to_string(),
2578                crate::render::svg_backend::warnings_json(&warnings),
2579            )]);
2580        }
2581        Ok((backend, warnings, pa))
2582    }
2583
2584    /// Render to a raw RGBA pixel buffer via the self-contained raster
2585    /// [`PixelBackend`](crate::render::pixel_backend::PixelBackend) (feature
2586    /// `canvas`) — fast for large-N and wasm-compatible. Returns `(w, h, rgba)`,
2587    /// ready for a `<canvas>` `putImageData`.
2588    #[cfg(feature = "canvas")]
2589    pub fn render_rgba_with_size(self, w: u32, h: u32) -> Result<(u32, u32, Vec<u8>), GGError> {
2590        let (built, layout) = self.prepare(w, h)?;
2591        let mut backend =
2592            crate::render::pixel_backend::PixelBackend::new(w, h, layout.plot_area.clone());
2593        PlotRenderer::render(&built, &mut backend).map_err(GGError::Render)?;
2594        let (ow, oh) = backend.dimensions();
2595        Ok((ow, oh, backend.into_rgba()))
2596    }
2597
2598    /// Like [`render_rgba_with_size`](Self::render_rgba_with_size), but also
2599    /// returns the panel rect in pixels `[x, y, w, h]` — enough to map a canvas
2600    /// pixel back to data coordinates for interactive hover.
2601    #[cfg(feature = "canvas")]
2602    pub fn render_rgba_area_with_size(
2603        self,
2604        w: u32,
2605        h: u32,
2606    ) -> Result<(Vec<u8>, [f64; 4]), GGError> {
2607        let (built, layout) = self.prepare(w, h)?;
2608        let pa = layout.plot_area.clone();
2609        let mut backend = crate::render::pixel_backend::PixelBackend::new(w, h, pa.clone());
2610        PlotRenderer::render(&built, &mut backend).map_err(GGError::Render)?;
2611        Ok((backend.into_rgba(), [pa.x, pa.y, pa.width, pa.height]))
2612    }
2613
2614    /// Render to PNG bytes via the raster [`PixelBackend`](crate::render::pixel_backend::PixelBackend)
2615    /// (feature `canvas`). Unlike [`render_png`](Self::render_png) this needs no
2616    /// plotters bitmap backend, so it also works on wasm.
2617    #[cfg(feature = "canvas")]
2618    pub fn render_png_raster_with_size(self, w: u32, h: u32) -> Result<Vec<u8>, GGError> {
2619        let (built, layout) = self.prepare(w, h)?;
2620        let mut backend =
2621            crate::render::pixel_backend::PixelBackend::new(w, h, layout.plot_area.clone());
2622        PlotRenderer::render(&built, &mut backend).map_err(GGError::Render)?;
2623        backend.into_png().map_err(GGError::Render)
2624    }
2625
2626    /// Render the plot to in-memory PNG bytes (default 800x600).
2627    ///
2628    /// Returns a fully-encoded PNG, ready to write to an HTTP response or
2629    /// embed as a data URI — no temp files involved. Requires the `plotters`
2630    /// feature (on by default); see also `render_png_raster_with_size`
2631    /// (feature `canvas`).
2632    #[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
2633    pub fn render_png(self) -> Result<Vec<u8>, GGError> {
2634        self.render_png_with_size(800, 600)
2635    }
2636
2637    /// Render the plot to in-memory PNG bytes with custom dimensions. (Feature
2638    /// `plotters`; native only — PNG needs the plotters bitmap backend.)
2639    #[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
2640    pub fn render_png_with_size(self, w: u32, h: u32) -> Result<Vec<u8>, GGError> {
2641        let (built, layout) = self.prepare(w, h)?;
2642
2643        // plotters' BitMapBackend draws into a raw RGB buffer; we then encode
2644        // that buffer to PNG via the `image` crate.
2645        let mut rgb = vec![0u8; (w as usize) * (h as usize) * 3];
2646        {
2647            let backend = plotters::prelude::BitMapBackend::with_buffer(&mut rgb, (w, h));
2648            Self::render_into(backend.into_drawing_area(), &built, &layout)?;
2649        }
2650
2651        let img = image::RgbImage::from_raw(w, h, rgb).ok_or_else(|| {
2652            GGError::Render(RenderError::BackendError(
2653                "PNG buffer size mismatch".to_string(),
2654            ))
2655        })?;
2656        let mut out = std::io::Cursor::new(Vec::new());
2657        img.write_to(&mut out, image::ImageOutputFormat::Png)
2658            .map_err(|e| GGError::Render(RenderError::BackendError(format!("{:?}", e))))?;
2659        Ok(out.into_inner())
2660    }
2661
2662    /// Shared pipeline: build the plot, apply label overrides, compute layout.
2663    pub(crate) fn prepare(
2664        self,
2665        w: u32,
2666        h: u32,
2667    ) -> Result<(crate::build::BuiltPlot, PlotLayout), GGError> {
2668        let (mut built, meta) = self.build_for_render()?;
2669        let layout = Self::layout_built(&mut built, &meta, w, h);
2670        Ok((built, layout))
2671    }
2672
2673    /// The size-independent half of [`prepare`](Self::prepare): build the plot,
2674    /// resolve theme inheritance and apply axis-label overrides.
2675    pub(crate) fn build_for_render(self) -> Result<(crate::build::BuiltPlot, RenderMeta), GGError> {
2676        let plot = self;
2677
2678        let has_title = plot.labels.title.is_some();
2679        let has_subtitle = plot.labels.subtitle.is_some();
2680        let has_caption = plot.labels.caption.is_some();
2681        let has_legend = plot.has_legend_mapping();
2682        let x_label = plot.labels.x.clone();
2683        let y_label = plot.labels.y.clone();
2684
2685        let mut built = PlotBuilder::build(plot)?;
2686
2687        // Resolve R-style theme inheritance (root `text` → child text elements).
2688        built.theme.resolve_inheritance();
2689
2690        // Apply user label overrides to scales
2691        if let Some(ref label) = x_label {
2692            if let Some(s) = built.scales.get_mut(&crate::aes::Aesthetic::X) {
2693                s.set_name(label);
2694            }
2695        }
2696        if let Some(ref label) = y_label {
2697            if let Some(s) = built.scales.get_mut(&crate::aes::Aesthetic::Y) {
2698                s.set_name(label);
2699            }
2700        }
2701
2702        Ok((
2703            built,
2704            RenderMeta {
2705                has_title,
2706                has_subtitle,
2707                has_caption,
2708                has_legend,
2709            },
2710        ))
2711    }
2712
2713    /// The size-dependent half of [`prepare`](Self::prepare): auto-tune axis
2714    /// labels for `w`×`h` and compute the layout.
2715    pub(crate) fn layout_built(
2716        built: &mut crate::build::BuiltPlot,
2717        meta: &RenderMeta,
2718        w: u32,
2719        h: u32,
2720    ) -> PlotLayout {
2721        let RenderMeta {
2722            has_title,
2723            has_subtitle,
2724            has_caption,
2725            has_legend,
2726        } = *meta;
2727        let x_axis_top = built
2728            .scales
2729            .get(&crate::aes::Aesthetic::X)
2730            .map(|s| s.axis_position_opposite())
2731            .unwrap_or(false);
2732
2733        // Auto-tune categorical axis labels so they stay legible. A flip swaps
2734        // which trained scale sits on each visual axis: the bottom (x) axis reads
2735        // the Y scale when flipped, the left (y) axis reads the X scale.
2736        let flipped = built.coord.is_flipped();
2737        let h_aes = if flipped {
2738            crate::aes::Aesthetic::Y
2739        } else {
2740            crate::aes::Aesthetic::X
2741        };
2742        let v_aes = if flipped {
2743            crate::aes::Aesthetic::X
2744        } else {
2745            crate::aes::Aesthetic::Y
2746        };
2747
2748        // The bottom-axis break labels that are actually drawn — a
2749        // `with_labels` override can blank some entries ("" ) to thin a
2750        // crowded discrete axis while keeping every position (sipemu/
2751        // ggplot-rs#162); a blanked entry is never drawn, so it never
2752        // competes for horizontal room or vertical drop below. Shared by
2753        // the auto-rotate decision and the rotated-label height reservation
2754        // just below, so both agree on what's actually on the canvas.
2755        let drawn_x_labels: Vec<(f64, String)> = built
2756            .scales
2757            .get(&h_aes)
2758            .map(|hs| {
2759                hs.breaks()
2760                    .into_iter()
2761                    .filter(|(_, label)| !label.is_empty())
2762                    .collect()
2763            })
2764            .unwrap_or_default();
2765        let h_is_discrete = built
2766            .scales
2767            .get(&h_aes)
2768            .map(|hs| hs.is_discrete())
2769            .unwrap_or(false);
2770
2771        // Crowded discrete bottom-axis labels overlap when drawn flat. Rotate
2772        // them — −45° if that de-collides, otherwise vertical (270°) — only
2773        // when no explicit angle was set (angle.abs() < 1.0 reads as
2774        // "unset"). Sized from the real, measured widths of the labels that
2775        // are actually drawn (`render::text::text_width`), both for the
2776        // label-width estimate and the crowding count `n` — #162's bug was
2777        // counting every break, blanked or not, which over-triggered the
2778        // rotation. −45° (not +45°) is ggplot2's own idiom (`angle = 45,
2779        // hjust = 1`, mirrored to this library's angle sign, sipemu/
2780        // ggplot-rs#163): it ends each label at its own tick and slants
2781        // down-left, rather than +45°'s down-right slant, which — anchored
2782        // the same way — would start under the *previous* category.
2783        if built.theme.axis_text_x.visible
2784            && built.theme.axis_text_x.angle.abs() < 1.0
2785            && h_is_discrete
2786            && !drawn_x_labels.is_empty()
2787        {
2788            let font = built.theme.axis_text_x.size;
2789            let max_w = drawn_x_labels
2790                .iter()
2791                .map(|(_, l)| crate::render::text::text_width(l, font))
2792                .fold(0.0_f64, f64::max);
2793            let n = drawn_x_labels.len() as f64;
2794            let spacing = 0.9 * w as f64 / n; // ~px between adjacent *drawn* labels
2795            if max_w > spacing {
2796                // 45° shrinks the horizontal footprint by cos45; if that
2797                // still doesn't fit, go vertical (270° = reads bottom-to-
2798                // top, hanging below the axis like the y-axis title).
2799                let vertical = max_w * std::f64::consts::FRAC_1_SQRT_2 > spacing;
2800                built.theme.axis_text_x.angle = if vertical { 270.0 } else { -45.0 };
2801            }
2802        }
2803
2804        // Whenever the bottom-axis labels end up rotated — by the
2805        // auto-rotate decision just above, or by an explicit
2806        // `axis_text_x_angle` call — reserve the real bottom space their
2807        // rotated bounding boxes need, so they never overlap the panel
2808        // (#162 also asked for this: an explicit angle reserved nothing).
2809        // `rotated_label_rise_fall` is the same helper `draw_x_axis` calls
2810        // to place its rotation anchor — `rise` must be reserved (as well
2811        // as placed) or a long enough label's far end swings right back up
2812        // over the panel regardless of how much height is reserved here;
2813        // `fall` just needs reserving so the label stays on-canvas. Both
2814        // here and in `draw_x_axis`, the anchor fraction must come from
2815        // `rotated_anchor_frac(angle, dir)` (sipemu/ggplot-rs#163) — not a
2816        // blind `1.0` — or the reservation is sized for the wrong end of
2817        // the label; `dir` matches `draw_x_axis`'s own tick direction.
2818        let x_label_height = if built.theme.axis_text_x.visible
2819            && built.theme.axis_text_x.angle.abs() > 10.0
2820            && !drawn_x_labels.is_empty()
2821        {
2822            let font = built.theme.axis_text_x.size;
2823            let angle = built.theme.axis_text_x.angle;
2824            let dir = if x_axis_top { -1.0 } else { 1.0 };
2825            let (rise, fall) = crate::render::text::rotated_label_rise_fall(
2826                drawn_x_labels.iter().map(|(_, l)| l.as_str()),
2827                font,
2828                angle,
2829                crate::render::text::rotated_anchor_frac(angle, dir),
2830            );
2831            Some(built.theme.legend_spacing / 2.0 + font / 2.0 + rise + fall)
2832        } else {
2833            None
2834        };
2835
2836        // Reserve the left-axis width from the actual label lengths so long
2837        // category names (a flipped bar chart's rows) aren't clipped. Never
2838        // shrinks below the default; grows up to 35% of the width.
2839        let y_label_width = if built.theme.axis_text_y.visible {
2840            built.scales.get(&v_aes).map(|vs| {
2841                let max_ch = vs
2842                    .breaks()
2843                    .iter()
2844                    .map(|(_, l)| l.chars().count())
2845                    .max()
2846                    .unwrap_or(0);
2847                let measured = max_ch as f64 * built.theme.axis_text_y.size * 0.6 + 4.0;
2848                let default = built.theme.axis_text_y.size * 3.5 + 4.0;
2849                measured.max(default).min(0.35 * w as f64)
2850            })
2851        } else {
2852            None
2853        };
2854
2855        // B3-1 (D6): reserve just enough left/right panel margin to keep
2856        // the outer x-axis labels on-canvas under `AxisLabelFit::Reserve`,
2857        // for a single (non-faceted) panel on a coord that supports it
2858        // (not polar/radar/sf, where a label bbox doesn't map to a simple
2859        // panel margin). A faceted plot skips the layout-level reservation
2860        // and relies on `draw_x_axis`'s own draw-time shift instead — so
2861        // does `AxisLabelFit::Shift` (the panel must keep its size) and
2862        // `Clip` (the 0.21 behaviour, no correction at all). A plot whose
2863        // outer labels already fit never grows `extras`, so it stays
2864        // byte-identical.
2865        let mut extras = crate::render::layout::LayoutExtras::default();
2866        let can_reserve = built.facet.is_none()
2867            && built.theme.axis_text_x.visible
2868            && built.theme.axis_text_x_fit == crate::theme::AxisLabelFit::Reserve
2869            && !built.coord.is_polar()
2870            && built.coord.supports_axis_reserve();
2871        if can_reserve {
2872            if let Some(hs) = built.scales.get(&h_aes) {
2873                let breaks = hs.breaks();
2874                if let (Some(first), Some(last)) = (breaks.first(), breaks.last()) {
2875                    let angle = built.theme.axis_text_x.angle;
2876                    let anchor = if angle.abs() > 10.0 {
2877                        let dir = if x_axis_top { -1.0 } else { 1.0 };
2878                        crate::render::text::rotated_anchor_frac(angle, dir)
2879                    } else {
2880                        0.5
2881                    };
2882                    let size = built.theme.axis_text_x.size;
2883                    const EDGE: f64 = 1.0;
2884                    for _ in 0..2 {
2885                        let layout = PlotLayout::compute_with_extras(
2886                            w as f64,
2887                            h as f64,
2888                            &built.theme,
2889                            has_title,
2890                            has_subtitle,
2891                            has_caption,
2892                            has_legend,
2893                            x_axis_top,
2894                            y_label_width,
2895                            x_label_height,
2896                            extras,
2897                        );
2898                        let area = &layout.plot_area;
2899                        let edge_y = area.y + area.height;
2900                        let to_point = |pos: f64| if flipped { (0.0, pos) } else { (pos, 0.0) };
2901                        let px_first = built.coord.transform(to_point(first.0), area).0;
2902                        let px_last = built.coord.transform(to_point(last.0), area).0;
2903                        let bbox_first = crate::render::text::label_bbox(
2904                            &first.1, size, px_first, edge_y, angle, anchor,
2905                        );
2906                        let bbox_last = crate::render::text::label_bbox(
2907                            &last.1, size, px_last, edge_y, angle, anchor,
2908                        );
2909                        let deficit_left = (EDGE - bbox_first[0]).max(0.0);
2910                        let deficit_right = (bbox_last[2] - (w as f64 - EDGE)).max(0.0);
2911                        if deficit_left <= 0.0 && deficit_right <= 0.0 {
2912                            break;
2913                        }
2914                        // Growing the left margin by `eL` (holding `eR` fixed)
2915                        // shifts a label at normalized position `p` right by
2916                        // `eL·(1 − p)`; growing the right margin by `eR`
2917                        // shifts it left by `eR·p` (the panel maps [0, 1] to
2918                        // [plot_x + eL, plot_x + width − eR]). Review #158
2919                        // finding 1: the original code divided by `p_first`
2920                        // and `1 − p_last` — inverted, which over-reserved
2921                        // by about `1/p_last` (≈20× at a typical p_last≈0.95).
2922                        let (p_first, p_last) = (first.0, last.0);
2923                        if deficit_left > 0.0 && deficit_right > 0.0 {
2924                            // Both ends overflow: solve the 2×2 system so
2925                            // each margin accounts for its effect on *both*
2926                            // labels, rather than correcting them
2927                            // independently and overshooting.
2928                            let (a, b, c, d) = (1.0 - p_first, p_first, 1.0 - p_last, p_last);
2929                            let det = a * d - b * c;
2930                            if det.abs() > 0.01 {
2931                                extras.left +=
2932                                    ((deficit_left * d + deficit_right * b) / det).max(0.0);
2933                                extras.right +=
2934                                    ((deficit_right * a + deficit_left * c) / det).max(0.0);
2935                            } else {
2936                                // Degenerate (first and last break nearly
2937                                // coincide in normalized space): fall back
2938                                // to the independent, clamped formulas.
2939                                extras.left += deficit_left / (1.0 - p_first).max(0.05);
2940                                extras.right += deficit_right / p_last.max(0.05);
2941                            }
2942                        } else if deficit_left > 0.0 {
2943                            extras.left += deficit_left / (1.0 - p_first).max(0.05);
2944                        } else if deficit_right > 0.0 {
2945                            extras.right += deficit_right / p_last.max(0.05);
2946                        }
2947                        // Cap the total reservation so the panel can never
2948                        // start beyond the canvas or be squeezed past the
2949                        // same minimum width `compute_with_extras` already
2950                        // floors it to (review #158's second re-review,
2951                        // LOW): a single, centred break whose label is
2952                        // wider than the whole canvas has `p_first ==
2953                        // p_last == 0.5`, so both `eL` and `eR` grow by
2954                        // roughly the same (large) deficit independently,
2955                        // and together they could place `plot_x` past `w`
2956                        // entirely. Past this cap, Reserve simply stops
2957                        // growing and `draw_x_axis`'s own draw-time shift
2958                        // — which always runs for Reserve, the same as for
2959                        // `Shift` — takes over for whatever's still
2960                        // outside, exactly as D6 already documents.
2961                        const MIN_PANEL_WIDTH: f64 = 50.0;
2962                        let cap = (w as f64 - MIN_PANEL_WIDTH).max(0.0);
2963                        if extras.left + extras.right > cap {
2964                            let scale = if extras.left + extras.right > 0.0 {
2965                                cap / (extras.left + extras.right)
2966                            } else {
2967                                0.0
2968                            };
2969                            extras.left *= scale;
2970                            extras.right *= scale;
2971                        }
2972                    }
2973                }
2974            }
2975        }
2976
2977        // B3-2 (D7): a discrete legend's `nrow`/`ncol` grid, or an
2978        // overflowing horizontal legend's auto-wrap, reserves extra legend
2979        // rows/width. Several simultaneous discrete guides (e.g. `colour`
2980        // and `shape` both mapped to discrete variables) each get their
2981        // own independent plan (sipemu/ggplot-rs#155) — previously this
2982        // only ran for exactly one discrete guide.
2983        if has_legend
2984            && !matches!(
2985                built.theme.legend_position,
2986                crate::theme::LegendPosition::None
2987            )
2988        {
2989            let legend_aes =
2990                crate::guide::legend::legend_scales_for(&built.scales, &built.suppressed_aes);
2991            // #236: a mapped `size` legend's largest key can need a bigger
2992            // box than the theme's flat defaults — independent of B3-2's
2993            // discrete-guide wrap planning below, so computed unconditionally.
2994            extras.legend_max_key_size = legend_aes
2995                .iter()
2996                .filter_map(|aes| {
2997                    let scale = built.scales.get(aes)?;
2998                    let breaks = scale.breaks();
2999                    Some(crate::guide::legend::size_key_diameter(
3000                        aes,
3001                        &built.scales,
3002                        scale,
3003                        &breaks,
3004                    ))
3005                })
3006                .fold(0.0_f64, f64::max);
3007            let has_discrete_guide = legend_aes.iter().any(|a| {
3008                built
3009                    .scales
3010                    .get(a)
3011                    .map(|s| s.is_discrete())
3012                    .unwrap_or(false)
3013            });
3014            if has_discrete_guide {
3015                let layout0 = PlotLayout::compute_with_extras(
3016                    w as f64,
3017                    h as f64,
3018                    &built.theme,
3019                    has_title,
3020                    has_subtitle,
3021                    has_caption,
3022                    has_legend,
3023                    x_axis_top,
3024                    y_label_width,
3025                    x_label_height,
3026                    extras,
3027                );
3028                // `y` is unused here (only `legend_x0`/`is_horizontal` feed
3029                // the avail-width check below), so #233's `Top` row-count
3030                // parameter can't matter — `extras.legend_extra_rows` isn't
3031                // even known yet at this point in the pass. Likewise #225's
3032                // `Inside` justification only matters when `legend_x0` is
3033                // actually used for an `Inside` position combined with an
3034                // explicit `nrow`/`ncol` grid (`Inside` otherwise never
3035                // reaches this discrete-guide-wrap check), too rare to
3036                // bother estimating a real size hint for here.
3037                let (legend_x0, _, mut is_horizontal) = crate::guide::legend::legend_position(
3038                    &built.theme,
3039                    &layout0.plot_area,
3040                    extras.legend_extra_rows,
3041                    (0.0, 0.0),
3042                    &layout0.total,
3043                );
3044                if let Some(dir) = built.theme.legend_direction {
3045                    is_horizontal = matches!(dir, crate::theme::LegendDirection::Horizontal);
3046                }
3047                let avail_width =
3048                    (layout0.plot_area.x + layout0.plot_area.width - legend_x0).max(0.0);
3049                let mut legend_warnings = Vec::new();
3050                let mut plans = crate::guide::legend::plan_all_legends(
3051                    &built.scales,
3052                    &built.guide_legend,
3053                    &built.theme,
3054                    &built.suppressed_aes,
3055                    avail_width,
3056                    layout0.plot_area.height,
3057                    is_horizontal,
3058                    &mut legend_warnings,
3059                );
3060                built.warnings.extend(legend_warnings);
3061
3062                if !plans.is_empty() {
3063                    // Guides laid out side by side (a horizontal legend
3064                    // position) share one row band — its height is set
3065                    // by whichever guide wrapped to the most rows.
3066                    // Guides stacked vertically each add their own full
3067                    // height, so their extra rows sum.
3068                    extras.legend_extra_rows = if is_horizontal {
3069                        plans
3070                            .values()
3071                            .map(|p| p.rows.saturating_sub(1))
3072                            .max()
3073                            .unwrap_or(0)
3074                    } else {
3075                        plans.values().map(|p| p.rows.saturating_sub(1)).sum()
3076                    };
3077                    // Review #158 finding 3: a vertical explicit grid
3078                    // with ncol > 1 needs extra legend band width for
3079                    // its later columns, or they run off the canvas
3080                    // (cols is always 1, col_pitch 0, for the horizontal
3081                    // auto-wrap path, so this is a no-op there). Several
3082                    // guides take the widest requirement among them.
3083                    extras.legend_extra_width = plans
3084                        .values()
3085                        .map(|p| p.cols.saturating_sub(1) as f64 * p.col_pitch)
3086                        .fold(0.0_f64, f64::max);
3087
3088                    // #159: a vertical multi-column grid that still runs
3089                    // off the canvas even after reserving the extra width
3090                    // above now (1) tries reducing its column count
3091                    // toward 1 — the first count that fits wins; (2) if
3092                    // even one column still overflows, truncates labels
3093                    // with an ellipsis (keeping the full label available
3094                    // via a `<title>`); and only (3) warns if it still
3095                    // doesn't fit after both. A grid that already fits is
3096                    // untouched — no reduction, no truncation, no warning
3097                    // (review #158's second re-review, LOW, is the
3098                    // original "warn only" version of this check).
3099                    if !is_horizontal {
3100                        let final_layout = PlotLayout::compute_with_extras(
3101                            w as f64,
3102                            h as f64,
3103                            &built.theme,
3104                            has_title,
3105                            has_subtitle,
3106                            has_caption,
3107                            has_legend,
3108                            x_axis_top,
3109                            y_label_width,
3110                            x_label_height,
3111                            extras,
3112                        );
3113                        // `y` is unused here too, but `extras` is final by
3114                        // this point, so pass its real `legend_extra_rows`
3115                        // for consistency with #233's `Top` formula. #225's
3116                        // `Inside` size hint is skipped here for the same
3117                        // reason as the pass above.
3118                        let (legend_x_final, _, _) = crate::guide::legend::legend_position(
3119                            &built.theme,
3120                            &final_layout.plot_area,
3121                            extras.legend_extra_rows,
3122                            (0.0, 0.0),
3123                            &final_layout.total,
3124                        );
3125                        let swatch_and_gap =
3126                            built.theme.legend_key_width + built.theme.legend_spacing;
3127                        let text_size = built.theme.legend_text.size;
3128                        // A plain fn, not a closure: #168's retightening
3129                        // pass below re-runs this same check at a second
3130                        // `legend_x` (the narrower reservation's), so
3131                        // `legend_x` has to be a parameter rather than
3132                        // captured.
3133                        fn right_edge_of(
3134                            legend_x: f64,
3135                            swatch_and_gap: f64,
3136                            text_size: f64,
3137                            plan: &crate::guide::legend::LegendPlan,
3138                            breaks: &[(f64, String)],
3139                        ) -> f64 {
3140                            plan.entries
3141                                .iter()
3142                                .filter_map(|e| breaks.get(e.break_idx).map(|(_, l)| (e, l)))
3143                                .map(|(e, label)| {
3144                                    let text =
3145                                        e.label_override.as_deref().unwrap_or(label.as_str());
3146                                    legend_x
3147                                        + e.x_offset
3148                                        + swatch_and_gap
3149                                        + crate::render::text::text_width(text, text_size)
3150                                })
3151                                .fold(0.0_f64, f64::max)
3152                        }
3153
3154                        let mut updates: Vec<(
3155                            crate::aes::Aesthetic,
3156                            crate::guide::legend::LegendPlan,
3157                        )> = Vec::new();
3158                        // #168: guides narrowed below by *column
3159                        // reduction* (not label truncation) are
3160                        // candidates for the retightening pass after this
3161                        // loop — a truncated guide already picked the
3162                        // narrowest column count, so there's no reserved
3163                        // width left to give back.
3164                        let mut reduced_aes: Vec<crate::aes::Aesthetic> = Vec::new();
3165                        for (aes, plan) in &plans {
3166                            if plan.cols <= 1 {
3167                                continue;
3168                            }
3169                            let Some(scale) = built.scales.get(aes) else {
3170                                continue;
3171                            };
3172                            let breaks = scale.breaks();
3173                            if right_edge_of(
3174                                legend_x_final,
3175                                swatch_and_gap,
3176                                text_size,
3177                                plan,
3178                                &breaks,
3179                            ) <= w as f64
3180                            {
3181                                continue; // already fits — untouched.
3182                            }
3183
3184                            // Step 1: reduce columns toward 1; the first
3185                            // count that fits wins.
3186                            let n = breaks.len();
3187                            let mut reduced: Vec<crate::guide::legend::LegendPlan> = Vec::new();
3188                            for c in (1..plan.cols).rev() {
3189                                let mut candidate_guide = built.guide_legend.clone();
3190                                candidate_guide.ncol = Some(c);
3191                                candidate_guide.nrow = None;
3192                                let mut tmp_warnings = Vec::new();
3193                                let candidate_guide =
3194                                    candidate_guide.validated(n, &mut tmp_warnings);
3195                                if let Some(cand) = crate::guide::legend::plan_legend(
3196                                    &breaks,
3197                                    &candidate_guide,
3198                                    &built.theme,
3199                                    avail_width,
3200                                    layout0.plot_area.height,
3201                                    false,
3202                                ) {
3203                                    reduced.push(cand);
3204                                }
3205                            }
3206                            if let Some(fits) = reduced.iter().find(|c| {
3207                                right_edge_of(legend_x_final, swatch_and_gap, text_size, c, &breaks)
3208                                    <= w as f64
3209                            }) {
3210                                extras.legend_extra_rows =
3211                                    extras.legend_extra_rows.max(fits.rows.saturating_sub(1));
3212                                updates.push((aes.clone(), fits.clone()));
3213                                reduced_aes.push(aes.clone());
3214                                continue;
3215                            }
3216
3217                            // Step 2: no column count fit — truncate
3218                            // labels (starting from the narrowest, 1-
3219                            // column, candidate) with an ellipsis, each
3220                            // to whatever room its own position leaves.
3221                            let base = reduced.last().cloned().unwrap_or_else(|| plan.clone());
3222                            extras.legend_extra_rows =
3223                                extras.legend_extra_rows.max(base.rows.saturating_sub(1));
3224                            let mut truncated = base.clone();
3225                            for e in &mut truncated.entries {
3226                                let Some((_, label)) = breaks.get(e.break_idx) else {
3227                                    continue;
3228                                };
3229                                let avail = (w as f64
3230                                    - (legend_x_final + e.x_offset + swatch_and_gap))
3231                                    .max(0.0);
3232                                if let Some(short) =
3233                                    crate::render::text::truncate_to_width(label, avail, text_size)
3234                                {
3235                                    e.label_override = Some(short);
3236                                }
3237                            }
3238
3239                            // Step 3: only warn if it still doesn't fit
3240                            // after both the reduction and the
3241                            // truncation above.
3242                            let truncated_edge = right_edge_of(
3243                                legend_x_final,
3244                                swatch_and_gap,
3245                                text_size,
3246                                &truncated,
3247                                &breaks,
3248                            );
3249                            if truncated_edge > w as f64 {
3250                                built.warnings.push(format!(
3251                                    "legend: the {}-column grid still doesn't fit the {w}px chart (needs about {} px)",
3252                                    truncated.cols,
3253                                    truncated_edge.ceil() as i64
3254                                ));
3255                            }
3256                            updates.push((aes.clone(), truncated));
3257                        }
3258                        for (aes, new_plan) in &updates {
3259                            plans.insert(aes.clone(), new_plan.clone());
3260                        }
3261
3262                        // #168: `extras.legend_extra_width` above still
3263                        // reserves the *original*, pre-reduction column
3264                        // count's width — that stale, wider reservation
3265                        // is an accident of ordering (reserve → layout →
3266                        // fit? → reduce), not a requirement, so the panel
3267                        // stays narrower than it needs to be once a guide
3268                        // above got fewer columns.
3269                        //
3270                        // Tighten it to the reduced guides' new column
3271                        // counts and re-check fit once more at the
3272                        // narrower reservation (a bounded retry, not an
3273                        // unbounded fixed point: a tighter reservation
3274                        // moves `legend_x_final` right — less headroom
3275                        // before the canvas edge — which could in
3276                        // principle call for yet another round of
3277                        // reduction; rather than chase reserve → layout →
3278                        // fit → reduce → reserve → … indefinitely, this
3279                        // keeps the tighter reservation only if every
3280                        // guide still fits under it, and falls back to
3281                        // the wider, already-proven-safe reservation
3282                        // otherwise — so retightening can only ever
3283                        // help, never regress a plot that fit before
3284                        // this pass existed).
3285                        //
3286                        // Review #167 finding 1 on #171 (HIGH, fixed):
3287                        // the retry must re-check *every* plan in
3288                        // `plans`, not just `reduced_aes`. `legend_x`
3289                        // moves for every guide sharing the row, not
3290                        // only the one(s) that got reduced — a second
3291                        // guide that already fit at the old, wider
3292                        // `legend_x_final` (its own width is a fixed
3293                        // `6·text_size` estimate, unrelated to the
3294                        // reduced guide's labels) or whose labels were
3295                        // truncated against that old `legend_x_final`
3296                        // shifts right by the same amount and was never
3297                        // re-checked, so it could run off the canvas
3298                        // with no warning (the warnings were computed
3299                        // against the old x too). `reduced_aes` is kept
3300                        // only to gate *entering* this block at all —
3301                        // nothing to reclaim if nothing was reduced.
3302                        if !reduced_aes.is_empty() {
3303                            let original_reserve = extras.legend_extra_width;
3304                            let tighter_reserve = plans
3305                                .values()
3306                                .map(|p| p.cols.saturating_sub(1) as f64 * p.col_pitch)
3307                                .fold(0.0_f64, f64::max);
3308                            if tighter_reserve < original_reserve {
3309                                let mut retry_extras = extras;
3310                                retry_extras.legend_extra_width = tighter_reserve;
3311                                let retry_layout = PlotLayout::compute_with_extras(
3312                                    w as f64,
3313                                    h as f64,
3314                                    &built.theme,
3315                                    has_title,
3316                                    has_subtitle,
3317                                    has_caption,
3318                                    has_legend,
3319                                    x_axis_top,
3320                                    y_label_width,
3321                                    x_label_height,
3322                                    retry_extras,
3323                                );
3324                                // `y` is unused here too; `retry_extras`
3325                                // only tightened `legend_extra_width`, so
3326                                // its `legend_extra_rows` is still the same
3327                                // real value. #225's `Inside` size hint is
3328                                // skipped for the same reason as above.
3329                                let (retry_x, _, _) = crate::guide::legend::legend_position(
3330                                    &built.theme,
3331                                    &retry_layout.plot_area,
3332                                    retry_extras.legend_extra_rows,
3333                                    (0.0, 0.0),
3334                                    &retry_layout.total,
3335                                );
3336                                let all_still_fit = plans.iter().all(|(aes, plan)| {
3337                                    let Some(scale) = built.scales.get(aes) else {
3338                                        return true;
3339                                    };
3340                                    let breaks = scale.breaks();
3341                                    right_edge_of(retry_x, swatch_and_gap, text_size, plan, &breaks)
3342                                        <= w as f64
3343                                });
3344                                if all_still_fit {
3345                                    extras.legend_extra_width = tighter_reserve;
3346                                }
3347                            }
3348                        }
3349                    }
3350                    for (aes, plan) in &plans {
3351                        if let Some(ov) = &plan.overflow {
3352                            let n = built
3353                                .scales
3354                                .get(aes)
3355                                .map(|s| s.breaks().len())
3356                                .unwrap_or(ov.dropped);
3357                            let shown = n.saturating_sub(ov.dropped);
3358                            built.warnings.push(format!(
3359                                "legend: {} of {n} entries don't fit the chart; the legend shows {shown} and \"+{} more\"",
3360                                ov.dropped, ov.dropped
3361                            ));
3362                        }
3363                    }
3364                }
3365                // Review #158 finding 2: store these exact plans so
3366                // `draw_legend_planned` draws them verbatim instead of
3367                // recomputing against the (shorter) post-reservation
3368                // panel height, which could warn "+7 more" while
3369                // drawing "+15 more".
3370                built.legend_plans = plans;
3371            }
3372        }
3373
3374        // #211: a continuous colourbar at `Top`/`Bottom` (or
3375        // `legend_direction(Horizontal)`) draws wide × short instead of
3376        // its usual tall × narrow bar (see `draw_continuous_legend_at`),
3377        // with its tick labels below rather than beside it — reserve the
3378        // extra band height that needs beyond the flat discrete-row
3379        // height `legend_band` already assumes. Independent of the
3380        // `has_discrete_guide` block above (a plot can have a continuous
3381        // colourbar with no discrete guide at all), so computed
3382        // unconditionally here, right before the layout that actually
3383        // matters reads `extras`.
3384        if has_legend
3385            && !matches!(
3386                built.theme.legend_position,
3387                crate::theme::LegendPosition::None
3388            )
3389        {
3390            let is_horizontal = match built.theme.legend_direction {
3391                Some(dir) => matches!(dir, crate::theme::LegendDirection::Horizontal),
3392                None => matches!(
3393                    built.theme.legend_position,
3394                    crate::theme::LegendPosition::Top | crate::theme::LegendPosition::Bottom
3395                ),
3396            };
3397            if is_horizontal {
3398                let legend_aes =
3399                    crate::guide::legend::legend_scales_for(&built.scales, &built.suppressed_aes);
3400                let continuous_colourbar = legend_aes.iter().find_map(|aes| {
3401                    if !matches!(
3402                        aes,
3403                        crate::aes::Aesthetic::Color | crate::aes::Aesthetic::Fill
3404                    ) {
3405                        return None;
3406                    }
3407                    let scale = built.scales.get(aes)?;
3408                    if scale.is_discrete() {
3409                        return None;
3410                    }
3411                    Some(scale)
3412                });
3413                if let Some(scale) = continuous_colourbar {
3414                    let title = built
3415                        .guide_legend
3416                        .title
3417                        .as_deref()
3418                        .unwrap_or_else(|| scale.name());
3419                    let bar_height = crate::guide::legend::continuous_horizontal_bar_height(
3420                        &built.theme,
3421                        !title.is_empty(),
3422                    );
3423                    let key_h = built
3424                        .theme
3425                        .legend_key_height
3426                        .max(extras.legend_max_key_size);
3427                    extras.continuous_horizontal_legend_extra_height =
3428                        (bar_height - key_h).max(0.0);
3429                }
3430            }
3431        }
3432
3433        PlotLayout::compute_with_extras(
3434            w as f64,
3435            h as f64,
3436            &built.theme,
3437            has_title,
3438            has_subtitle,
3439            has_caption,
3440            has_legend,
3441            x_axis_top,
3442            y_label_width,
3443            x_label_height,
3444            extras,
3445        )
3446    }
3447
3448    /// Fill the background, render the built plot, and flush — for any plotters
3449    /// backend. (Native only.)
3450    #[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
3451    fn render_into<DB>(
3452        area: plotters::drawing::DrawingArea<DB, plotters::coord::Shift>,
3453        built: &crate::build::BuiltPlot,
3454        layout: &PlotLayout,
3455    ) -> Result<(), GGError>
3456    where
3457        DB: plotters::prelude::DrawingBackend,
3458        DB::ErrorType: 'static,
3459    {
3460        area.fill(&plotters::prelude::WHITE)
3461            .map_err(|e| GGError::Render(RenderError::BackendError(format!("{:?}", e))))?;
3462        let mut adapter = PlottersAdapter::new(&area, layout.plot_area.clone());
3463        PlotRenderer::render(built, &mut adapter).map_err(GGError::Render)?;
3464        area.present()
3465            .map_err(|e| GGError::Render(RenderError::BackendError(format!("{:?}", e))))?;
3466        Ok(())
3467    }
3468
3469    /// Save with physical dimensions (inches) and DPI. (Feature `plotters`;
3470    /// native only.)
3471    #[cfg(all(feature = "plotters", not(target_arch = "wasm32")))]
3472    pub fn ggsave(
3473        self,
3474        path: &str,
3475        width_inches: f64,
3476        height_inches: f64,
3477        dpi: f64,
3478    ) -> Result<(), GGError> {
3479        let w = (width_inches * dpi) as u32;
3480        let h = (height_inches * dpi) as u32;
3481        self.save_with_size(path, w, h)
3482    }
3483
3484    /// Does this plot have a legend to make room for? (#201) A plot-level
3485    /// `.aes(...)` mapping always counts; so does a *layer's own*
3486    /// `.layer_aes(...)` mapping — e.g. `geom_hex`'s usual pattern of
3487    /// mapping `fill` to a raw column only on that one layer, via
3488    /// `StatIdentity` after pre-binning, rather than on the plot as a
3489    /// whole. Checking only `self.mapping` (as before) missed that case
3490    /// entirely: the legend still drew (the render path always draws
3491    /// whatever guides the trained scales produce, regardless of this
3492    /// flag), just with no panel margin ever reserved for it, so it ran
3493    /// straight off the canvas edge. A layer that opts out with
3494    /// `show_legend(false)` doesn't count towards its own mapping, mirroring
3495    /// `PlotBuilder::compute_suppressed_aes`'s later, more precise
3496    /// (post-build, cross-layer) suppression check — this one only needs to
3497    /// decide *whether to reserve space at all*, not exactly how much.
3498    fn has_legend_mapping(&self) -> bool {
3499        // #241: delegates to `guide::legend::is_legend_aesthetic` (the
3500        // same list `draw_legend_planned` itself draws from) rather than
3501        // keeping its own copy — a second, independently-maintained list
3502        // here had drifted out of sync, missing `Linewidth`.
3503        use crate::guide::legend::is_legend_aesthetic;
3504        let plot_level = self
3505            .mapping
3506            .mappings
3507            .iter()
3508            .any(|m| is_legend_aesthetic(&m.aesthetic));
3509        plot_level
3510            || self.layers.iter().any(|layer| {
3511                if layer.show_legend == Some(false) {
3512                    return false;
3513                }
3514                let effective = if layer.geom.inherit_aes() {
3515                    self.mapping.merge(&layer.mapping)
3516                } else {
3517                    layer.mapping.clone()
3518                };
3519                effective
3520                    .mappings
3521                    .iter()
3522                    .any(|m| is_legend_aesthetic(&m.aesthetic))
3523            })
3524    }
3525}
3526
3527/// Size-independent facts about a built plot that its layout needs.
3528#[derive(Clone, Copy, Debug)]
3529pub(crate) struct RenderMeta {
3530    pub(crate) has_title: bool,
3531    pub(crate) has_subtitle: bool,
3532    pub(crate) has_caption: bool,
3533    pub(crate) has_legend: bool,
3534}
3535
3536/// Top-level error type.
3537#[derive(Debug)]
3538#[non_exhaustive]
3539pub enum GGError {
3540    Render(RenderError),
3541    UnsupportedFormat(String),
3542    Io(std::io::Error),
3543    ValidationError(String),
3544}
3545
3546impl std::fmt::Display for GGError {
3547    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3548        match self {
3549            GGError::Render(e) => write!(f, "Render error: {e}"),
3550            GGError::UnsupportedFormat(ext) => write!(f, "Unsupported output format: {ext}"),
3551            GGError::Io(e) => write!(f, "IO error: {e}"),
3552            GGError::ValidationError(msg) => write!(f, "Validation error: {msg}"),
3553        }
3554    }
3555}
3556
3557impl std::error::Error for GGError {}