Skip to main content

ggplot_rs/geom/
mod.rs

1#[cfg(feature = "sf")]
2pub mod annotation_scale;
3pub mod arcdiagram;
4pub mod area;
5pub mod arrow;
6pub mod bar;
7pub mod bin2d;
8#[cfg(feature = "sf")]
9pub mod bivariate_legend;
10pub mod blank;
11pub mod boxplot;
12pub mod bracket;
13pub mod bullet;
14pub mod candlestick;
15pub mod censor;
16pub mod chord;
17pub mod col;
18pub mod contour;
19pub mod cooks;
20pub mod count;
21pub mod crossbar;
22pub mod curve;
23pub(crate) mod decorative;
24pub mod density;
25pub mod density2d;
26pub mod density_ridges;
27pub mod dotplot;
28pub mod errorbar;
29pub mod errorbarh;
30pub mod flags;
31#[cfg(feature = "sf")]
32pub mod flow;
33pub mod freqpoly;
34pub mod funnel;
35pub mod hex;
36pub mod histogram;
37pub mod jitter;
38pub(crate) mod layer_attrs;
39pub mod line;
40pub mod linerange;
41pub mod lollipop;
42#[cfg(feature = "tiles")]
43pub mod map_tile;
44pub mod marimekko;
45pub mod network;
46pub mod pack;
47pub mod pareto;
48pub mod partition;
49pub mod path;
50pub mod point;
51pub mod pointrange;
52pub mod polygon;
53pub mod qq;
54pub mod raster;
55pub mod rect;
56pub mod refline;
57pub mod renko;
58pub mod repel;
59pub mod ribbon;
60pub mod rug;
61pub mod sankey;
62pub mod segment;
63#[cfg(feature = "sf")]
64pub mod sf;
65#[cfg(feature = "sf")]
66pub mod sf_text;
67pub mod smooth;
68pub mod spoke;
69pub mod step;
70pub(crate) mod support;
71pub mod text;
72pub mod tile;
73pub mod tilemap;
74pub mod timeline;
75pub mod tree;
76pub mod treemap;
77pub mod variablepie;
78pub mod venn;
79pub mod violin;
80pub mod waffle;
81pub mod waterfall;
82pub mod windbarb;
83pub mod wordcloud;
84pub mod xrange;
85
86use std::collections::HashMap;
87
88use crate::aes::Aesthetic;
89use crate::coord::Coord;
90use crate::data::DataFrame;
91use crate::position::Position;
92use crate::render::backend::DrawBackend;
93use crate::render::RenderError;
94use crate::scale::ScaleSet;
95use crate::stat::Stat;
96use crate::theme::Theme;
97
98/// Fixed (non-mapped) visual parameters for a geom.
99#[derive(Clone, Debug, Default)]
100pub struct GeomParams {
101    pub values: HashMap<String, f64>,
102    pub color: Option<(u8, u8, u8)>,
103    pub fill: Option<(u8, u8, u8)>,
104    pub alpha: Option<f64>,
105}
106
107/// Trait for geometric objects that draw data on the plot.
108pub trait Geom: Send + Sync {
109    /// Draw this geometry.
110    fn draw(
111        &self,
112        data: &DataFrame,
113        coord: &dyn Coord,
114        scales: &ScaleSet,
115        theme: &Theme,
116        backend: &mut dyn DrawBackend,
117    ) -> Result<(), RenderError>;
118
119    /// Required aesthetics.
120    fn required_aes(&self) -> Vec<Aesthetic>;
121
122    /// Default stat for this geom.
123    fn default_stat(&self) -> Box<dyn Stat>;
124
125    /// Default position adjustment.
126    fn default_position(&self) -> Box<dyn Position>;
127
128    /// Non-mapped visual defaults.
129    fn default_params(&self) -> GeomParams;
130
131    /// A figure-level layer on a faceted plot: the height of the band it
132    /// needs under the whole panel grid at `width`, when it draws once
133    /// there instead of in every panel (e.g. `GeomAttribution::figure`).
134    /// Default `None` (drawn per panel).
135    fn figure_band_height(&self, _width: f64, _theme: &Theme) -> Option<f64> {
136        None
137    }
138
139    /// Name for debug/display.
140    fn name(&self) -> &str;
141
142    /// Apply a brand/primary color to this geom's single-series default (its
143    /// `color` or `fill`). The build pipeline calls this only when the layer has
144    /// no color/fill aesthetic mapped, so an explicit mapping always wins. The
145    /// default is a no-op; series geoms override it.
146    fn set_series_color(&mut self, _color: (u8, u8, u8)) {}
147
148    /// Recolour this geom's *built-in default* colour/fill from the theme's
149    /// ink/paper/accent palette (B2-7's `element_geom`), called once per
150    /// layer before [`set_series_color`](Self::set_series_color) — and only
151    /// when the layer was *not* configured explicitly (`geom_*_with(...)`;
152    /// the build pipeline gates the call on the same `explicit_style` flag
153    /// `set_series_color`'s brand-colour application is gated on). That
154    /// gate, not a field-value comparison, is what keeps an explicit colour
155    /// untouched: a geom that honours this replaces a field still at its
156    /// own hard-coded default (black/`(50, 50, 50)` → `ink`, white →
157    /// `paper`) by comparing its *value*, which cannot by itself tell an
158    /// explicitly-set colour that happens to equal that default (e.g.
159    /// `GeomPoint { color: (0, 0, 0), .. }` under `geom_point_with`) apart
160    /// from one that was simply never touched — only skipping the call
161    /// entirely for an explicitly-configured layer closes that gap (review
162    /// PR#141 finding 4). Default no-op; `None` fields of `ElementGeom`
163    /// change nothing, so the default theme (no ink/paper/accent set)
164    /// stays byte-identical.
165    fn apply_element_geom(&mut self, _el: &crate::theme::elements::ElementGeom) {}
166
167    /// Whether this geom draws from a 0 baseline, so the Y scale should include
168    /// 0 even when `y` is explicitly mapped (bars/columns/area/histograms — as
169    /// in ggplot2). Default false.
170    fn include_zero_baseline(&self) -> bool {
171        false
172    }
173
174    /// Whether `-Inf`/`Inf` position values are meaningful for this geom
175    /// (ggplot2: "extend to the panel edge"). Rows with infinite positions are
176    /// otherwise dropped with a warning before stats run. `NaN` is always
177    /// dropped — unless [`Geom::keeps_na_as_break`] says otherwise. Default
178    /// false; `geom_rect` returns true.
179    fn allows_infinite(&self) -> bool {
180        false
181    }
182
183    /// Whether a row with a non-finite position is a *break* rather than a
184    /// removal (ggplot2's `GeomPath`/`GeomLine` NA handling): the row still
185    /// counts toward the "removed N rows" warning, but it is **not** deleted
186    /// from the built `DataFrame`, so the geom's own per-group point list
187    /// still contains it — as a non-finite pixel position after the scale
188    /// and coordinate transform, which [`crate::render::backend::DrawBackend::draw_line`]
189    /// splits on into separate polylines (ggplot2's NA-break semantics)
190    /// instead of silently joining the finite points across the gap.
191    /// Default false (the row is dropped, as for every other geom);
192    /// line/path/step return true.
193    fn keeps_na_as_break(&self) -> bool {
194        false
195    }
196
197    /// Whether this geom calls [`set_row_extras`] (so it reads
198    /// [`Aes::tooltip`](crate::aes::Aes::tooltip) and
199    /// [`Aes::data_attr`](crate::aes::Aes::data_attr), and a
200    /// [`GGPlot::tooltip`](crate::plot::GGPlot::tooltip) template on it has
201    /// a visible effect). Default false; the build pipeline warns when a
202    /// `tooltip()` template is set on a layer whose geom returns false
203    /// here (review PR#141 finding 6, LOW: a template silently did nothing
204    /// on e.g. `geom_area`/`geom_boxplot`/`geom_ribbon`). Point, segment,
205    /// curve, path, line, hex, polygon, sf, rect/tile and col/bar/
206    /// histogram return true.
207    fn consumes_tooltips(&self) -> bool {
208        false
209    }
210
211    /// Geom-specific data preparation after the stat and position steps but
212    /// before scale training (ggplot2's `GeomX$setup_data`) — e.g. a tile adds
213    /// its `xmin`/`xmax`/`ymin`/`ymax` extents so continuous scales train on
214    /// them. `warnings` is the layer's own build-warnings sink (B3-5: added
215    /// so `GeomBoxplot` can warn on an unparsable outlier value here, the
216    /// same channel every other build warning uses — never `eprintln!`).
217    /// Default: no-op.
218    fn setup_data(&self, _data: &mut DataFrame, _warnings: &mut Vec<String>) {}
219
220    /// Whether the layer inherits the plot-level aesthetic mapping (ggplot2's
221    /// `inherit.aes`). Reference-line geoms return false, as in ggplot2, so a
222    /// plot-level `color`/`x`/`y` mapping does not leak into a `geom_hline`.
223    /// Default true.
224    fn inherit_aes(&self) -> bool {
225        true
226    }
227
228    /// Canonical column names this geom only reads when the user actually
229    /// mapped the corresponding aesthetic (D3, B3-5) — never from a raw
230    /// column that merely happens to share the name (e.g. a SQL alias
231    /// literally called `lower`). `build_layer` drops any of these columns
232    /// right after `resolve_mappings` unless the aesthetic was mapped.
233    /// Default empty; `GeomBoxplot` returns `["lower", "middle", "upper",
234    /// "outliers"]`.
235    fn mapped_only_columns(&self) -> &'static [&'static str] {
236        &[]
237    }
238
239    /// Stat-dependent layer validation beyond [`required_aes`](Self::required_aes)
240    /// (B3-5): called once per layer with the resolved (pre-stat, post-D3-drop)
241    /// column names and the layer's stat name, before the stat runs. Default
242    /// no-op. `GeomBoxplot` requires `lower`/`middle`/`upper` to be mapped
243    /// under `stat="identity"`, else `y`.
244    fn validate_layer(&self, _pre_stat_columns: &[String], _stat_name: &str) -> Result<(), String> {
245        Ok(())
246    }
247}
248
249/// Format a value for a hover tooltip — strings verbatim, numbers rounded short,
250/// `Na` empty.
251pub(crate) fn tip_value(v: &crate::data::Value) -> String {
252    crate::format::format_value(v)
253}
254
255/// Half-width (normalized panel units) for bars on a continuous / date x axis:
256/// `width` × the smallest gap between distinct mapped x positions, like
257/// ggplot2's `resolution(x)`. A fixed fraction would make 60 daily bars overlap
258/// and 3 bars look like slivers. Falls back to `fallback` with < 2 distinct xs.
259pub(crate) fn continuous_bar_half_width(
260    mapped_xs: impl Iterator<Item = f64>,
261    width: f64,
262    fallback: f64,
263) -> f64 {
264    let mut xs: Vec<f64> = mapped_xs.filter(|x| x.is_finite()).collect();
265    xs.sort_by(|a, b| a.total_cmp(b));
266    xs.dedup_by(|a, b| (*a - *b).abs() < 1e-12);
267    let gap = xs
268        .windows(2)
269        .map(|w| w[1] - w[0])
270        .fold(f64::INFINITY, f64::min);
271    if gap.is_finite() && gap > 0.0 {
272        gap * width / 2.0
273    } else {
274        fallback
275    }
276}
277
278/// Raw (unformatted) value for a `data-value` attribute: numbers in shortest
279/// round-trip form, date-times as epoch seconds, strings verbatim. `None` for
280/// missing / non-finite values.
281pub(crate) fn raw_value(v: &crate::data::Value) -> Option<String> {
282    use crate::data::Value;
283    match v {
284        Value::Float(f) if f.is_finite() => Some(format!("{f}")),
285        Value::Float(_) | Value::Na => None,
286        Value::Integer(i) => Some(i.to_string()),
287        Value::DateTime(s) => Some(s.to_string()),
288        Value::Str(s) => Some(s.clone()),
289        Value::Bool(b) => Some(b.to_string()),
290    }
291}
292
293/// The series key of row `i` — its colour, else fill, else group level — for
294/// a `data-series` attribute.
295pub(crate) fn series_key(data: &DataFrame, i: usize) -> Option<String> {
296    ["color", "fill", "group"].iter().find_map(|c| {
297        data.column(c)
298            .and_then(|col| col.get(i))
299            .filter(|v| !v.is_na())
300            .map(tip_value)
301            .filter(|s| !s.is_empty())
302    })
303}
304
305/// The raw measured y of row `i`. Stacked/filled positions overwrite `y` with
306/// the cumulative top, so prefer the pre-position value they preserve.
307pub(crate) fn measured_value(data: &DataFrame, i: usize) -> Option<String> {
308    measured_raw(data, i).and_then(raw_value)
309}
310
311/// [`measured_value`], but the raw `Value` itself (for `Scale::format_value`,
312/// B2-4) instead of its display string.
313pub(crate) fn measured_raw(data: &DataFrame, i: usize) -> Option<&crate::data::Value> {
314    data.column(crate::position::RAW_Y_COL)
315        .or_else(|| data.column("y"))
316        .and_then(|c| c.get(i))
317}
318
319/// Set all per-mark metadata (tooltip, `data-x`, `data-series`, `data-value`)
320/// for the next drawn mark(s).
321pub(crate) fn set_mark(
322    backend: &mut dyn DrawBackend,
323    tooltip: Option<String>,
324    x: Option<String>,
325    series: Option<String>,
326    value: Option<String>,
327) {
328    backend.set_tooltip(tooltip);
329    backend.set_mark_axis(x);
330    backend.set_mark_series(series);
331    backend.set_mark_value(value);
332}
333
334/// Resolve the font family for a text-drawing geom (text, label, repel,
335/// bracket, annotations) — B2-6/B2-7's three-tier order: `geom_family`
336/// (the geom's own `fontfamily`, e.g. `GeomText::with_fontfamily`) when
337/// set, else `theme.geom.family` (B2-7's `element_geom`), else
338/// `theme.text.family` (read after `Theme::resolve_inheritance`, so an
339/// inherited value is already resolved), else `None` (the SVG backend's
340/// own `"sans-serif"` default). `ElementText::family` defaults to
341/// `"sans-serif"` itself and `theme.geom.family` defaults to `None`, so
342/// with nothing configured anywhere this already returns
343/// `Some("sans-serif")` — the same string the backend would have defaulted
344/// to, so default output stays byte-identical.
345pub(crate) fn resolve_font_family(geom_family: &str, theme: &Theme) -> Option<String> {
346    if !geom_family.is_empty() {
347        Some(geom_family.to_string())
348    } else if let Some(family) = &theme.geom.family {
349        Some(family.clone())
350    } else if theme.text.family.is_empty() {
351        None
352    } else {
353        Some(theme.text.family.clone())
354    }
355}
356
357/// Parse a per-row `Aes::fontface` (#228) string the same way `theme()`'s
358/// `face` is (`src/theme/config.rs::text`): `"plain"`/`"normal"` ->
359/// [`FontFace::Plain`], `"bold"` -> `Bold`, `"italic"`/`"oblique"` ->
360/// `Italic`, case-insensitive. `None` for anything else, so the caller falls
361/// back to the geom's own fixed fontface — a row's unparseable value is
362/// silently ignored rather than a build-time warning, since it's only known
363/// once the data is in hand.
364pub(crate) fn parse_fontface(raw: &str) -> Option<crate::render::backend::FontFace> {
365    use crate::render::backend::FontFace;
366    match raw.to_lowercase().as_str() {
367        "plain" | "normal" => Some(FontFace::Plain),
368        "bold" => Some(FontFace::Bold),
369        "italic" | "oblique" => Some(FontFace::Italic),
370        _ => None,
371    }
372}
373
374/// Clear all per-mark metadata after a geom has drawn its marks.
375pub(crate) fn clear_mark(backend: &mut dyn DrawBackend) {
376    set_mark(backend, None, None, None, None);
377    backend.set_mark_fvalue(None);
378    backend.set_mark_fx(None);
379}
380
381/// Set `data-fvalue`/`data-fx` (B2-4) for the mark `set_mark` just
382/// configured, formatted through the relevant scale — `value_aes`'s (Y for
383/// most geoms, Fill for hex/tile) for `data-fvalue`, X for `data-fx`.
384/// Always sets both (even to `None`), so a row without a formattable value
385/// doesn't inherit an earlier row's — the same per-row reset discipline as
386/// [`set_row_extras`]. A no-op query (`wants_formatted_values`) short-
387/// circuits the scale lookups entirely when
388/// [`crate::plot::GGPlot::svg_formatted_values`] is off (the default), so
389/// this costs nothing and changes nothing when never turned on.
390pub(crate) fn set_mark_formatted(
391    backend: &mut dyn DrawBackend,
392    scales: &crate::scale::ScaleSet,
393    x_raw: Option<&crate::data::Value>,
394    value_aes: &Aesthetic,
395    value_raw: Option<&crate::data::Value>,
396) {
397    if !backend.wants_formatted_values() {
398        backend.set_mark_fvalue(None);
399        backend.set_mark_fx(None);
400        return;
401    }
402    let fvalue = value_raw
403        .filter(|v| !v.is_na())
404        .and_then(|v| scales.get(value_aes).and_then(|s| s.format_value(v)));
405    backend.set_mark_fvalue(fvalue);
406    let fx = x_raw
407        .filter(|v| !v.is_na())
408        .and_then(|v| scales.get(&Aesthetic::X).and_then(|s| s.format_value(v)));
409    backend.set_mark_fx(fx);
410}
411
412/// Apply one row's interactive aesthetics (B1-7: `Aes::tooltip`,
413/// `Aes::data_attr`) to the backend.
414///
415/// A validated `Aes::tooltip` mapping replaces the geom's own `<title>`
416/// (call the geom's own `set_mark(...)` *before* this, so a custom tooltip
417/// wins) for *every* row — including when this row's value is NA, which
418/// explicitly resets the tooltip to `None` (no `<title>` at all, even if
419/// the geom has its own default): once `Aes::tooltip` is mapped, it owns
420/// the tooltip for the whole layer, the same row-by-row way `color` or
421/// `size` would. It never inherits an earlier row's. It's read from
422/// [`crate::aes::TOOLTIP_INTERNAL_NAME`], not a plain `"tooltip"` column
423/// name, so an unrelated raw source column literally called `"tooltip"`
424/// can never drive every mark's hover title (the same class of bug fixed
425/// for `data_attr` below). `set_tooltip` is only called at all when that
426/// internal column exists, so layers that never map `Aes::tooltip` don't
427/// touch whatever the geom already set.
428///
429/// Every validated `data_attr` column becomes a `data-<name>` attribute.
430/// These are stored under [`crate::aes::DATA_ATTR_INTERNAL_PREFIX`] rather
431/// than scanned by a `"data-"` name prefix: a raw source column that merely
432/// happens to be named like a data attribute (e.g. a SQL alias) was never
433/// validated and must never reach `push_mark` (see
434/// `aes::validated_data_attr_internal_name`, and `aes::mapping`'s defensive
435/// drop of any raw column that could forge an internal name).
436/// `set_mark_attrs` is *always* called, even with an empty list, so a row
437/// with no attrs doesn't inherit an earlier row's.
438pub(crate) fn set_row_extras(backend: &mut dyn DrawBackend, data: &DataFrame, row: usize) {
439    if let Some(col) = data.column(crate::aes::TOOLTIP_INTERNAL_NAME) {
440        let tip = col.get(row).filter(|v| !v.is_na()).map(tip_value);
441        backend.set_tooltip(tip);
442    }
443    let mut attrs = Vec::new();
444    for name in data.column_names() {
445        if let Some(target) = name.strip_prefix(crate::aes::DATA_ATTR_INTERNAL_PREFIX) {
446            if let Some(v) = data.column(name).and_then(|c| c.get(row)) {
447                if !v.is_na() {
448                    attrs.push((target.to_string(), tip_value(v)));
449                }
450            }
451        }
452    }
453    backend.set_mark_attrs(attrs);
454    // #221: `Aes::class` tokens (sanitized at build time) and the
455    // `Aes::group_wrap` group — always set, so a row never inherits an
456    // earlier row's.
457    let class = data
458        .column(crate::aes::CLASS_INTERNAL_NAME)
459        .and_then(|c| c.get(row))
460        .filter(|v| !v.is_na())
461        .map(tip_value)
462        .filter(|s| !s.is_empty());
463    backend.set_mark_class(class);
464    let mut group = None;
465    for name in data.column_names() {
466        if let Some(target) = name.strip_prefix(crate::aes::GROUP_WRAP_INTERNAL_PREFIX) {
467            if let Some(v) = data.column(name).and_then(|c| c.get(row)) {
468                if !v.is_na() {
469                    group = Some((target.to_string(), tip_value(v)));
470                    break;
471                }
472            }
473        }
474    }
475    backend.set_mark_group(group);
476}
477
478/// Clear interactive-aesthetic attributes *and* the tooltip after a layer
479/// finishes drawing, so neither leaks into a later layer, an annotation, or
480/// the legend's own background/key marks (which share the same backend
481/// instance for the rest of the render).
482pub(crate) fn clear_row_extras(backend: &mut dyn DrawBackend) {
483    backend.set_tooltip(None);
484    backend.set_mark_attrs(Vec::new());
485    backend.set_mark_class(None);
486    backend.set_mark_group(None);
487}
488
489/// Draw one group's polyline (path/line), honouring a per-point `linewidth`.
490/// A constant width draws one polyline (byte-identical to the pre-linewidth
491/// behaviour); a varying width falls back to drawing segment-by-segment,
492/// since a single stroked polyline can't vary its width along its length.
493/// An optional arrowhead is drawn once per polyline, using the width at its
494/// last point (closest to where a default `ends(Last)` head sits).
495#[allow(clippy::too_many_arguments)]
496pub(crate) fn draw_polyline_with_linewidth(
497    backend: &mut dyn DrawBackend,
498    points: &[(f64, f64)],
499    widths: &[f64],
500    color: (u8, u8, u8),
501    alpha: f64,
502    linetype: crate::render::backend::Linetype,
503    lineend: crate::render::backend::LineEnd,
504    arrow: Option<&crate::geom::arrow::Arrow>,
505    fallback_width: f64,
506) -> Result<(), RenderError> {
507    if points.len() < 2 {
508        return Ok(());
509    }
510    let w0 = widths.first().copied().unwrap_or(fallback_width);
511    let varies = widths.len() == points.len() && widths.iter().any(|&w| (w - w0).abs() > 1e-9);
512    if varies {
513        for i in 0..points.len() - 1 {
514            backend.draw_line(
515                &points[i..=i + 1],
516                &crate::render::backend::LineStyle {
517                    color,
518                    alpha,
519                    width: widths[i],
520                    linetype: linetype.clone(),
521                    lineend,
522                },
523            )?;
524        }
525    } else {
526        backend.draw_line(
527            points,
528            &crate::render::backend::LineStyle {
529                color,
530                alpha,
531                width: w0,
532                linetype,
533                lineend,
534            },
535        )?;
536    }
537    if let Some(arrow) = arrow {
538        let resolved_width = widths.last().copied().unwrap_or(fallback_width);
539        let hs = crate::geom::arrow::heads(points, arrow, resolved_width);
540        crate::geom::arrow::draw_heads(backend, &hs, arrow, color, alpha, resolved_width)?;
541    }
542    Ok(())
543}