Skip to main content

ggplot_rs/geom/
mod.rs

1pub mod area;
2pub mod bar;
3pub mod bin2d;
4pub mod blank;
5pub mod boxplot;
6pub mod bracket;
7pub mod col;
8pub mod contour;
9pub mod count;
10pub mod crossbar;
11pub mod curve;
12pub mod density;
13pub mod density2d;
14pub mod dotplot;
15pub mod errorbar;
16pub mod freqpoly;
17pub mod hex;
18pub mod histogram;
19pub mod jitter;
20pub mod line;
21pub mod linerange;
22pub mod path;
23pub mod point;
24pub mod pointrange;
25pub mod polygon;
26pub mod qq;
27pub mod raster;
28pub mod rect;
29pub mod refline;
30pub mod ribbon;
31pub mod rug;
32pub mod segment;
33#[cfg(feature = "sf")]
34pub mod sf;
35pub mod smooth;
36pub mod spoke;
37pub mod step;
38pub mod text;
39pub mod tile;
40pub mod violin;
41
42use std::collections::HashMap;
43
44use crate::aes::Aesthetic;
45use crate::coord::Coord;
46use crate::data::DataFrame;
47use crate::position::Position;
48use crate::render::backend::DrawBackend;
49use crate::render::RenderError;
50use crate::scale::ScaleSet;
51use crate::stat::Stat;
52use crate::theme::Theme;
53
54/// Fixed (non-mapped) visual parameters for a geom.
55#[derive(Clone, Debug, Default)]
56pub struct GeomParams {
57    pub values: HashMap<String, f64>,
58    pub color: Option<(u8, u8, u8)>,
59    pub fill: Option<(u8, u8, u8)>,
60    pub alpha: Option<f64>,
61}
62
63/// Trait for geometric objects that draw data on the plot.
64pub trait Geom: Send + Sync {
65    /// Draw this geometry.
66    fn draw(
67        &self,
68        data: &DataFrame,
69        coord: &dyn Coord,
70        scales: &ScaleSet,
71        theme: &Theme,
72        backend: &mut dyn DrawBackend,
73    ) -> Result<(), RenderError>;
74
75    /// Required aesthetics.
76    fn required_aes(&self) -> Vec<Aesthetic>;
77
78    /// Default stat for this geom.
79    fn default_stat(&self) -> Box<dyn Stat>;
80
81    /// Default position adjustment.
82    fn default_position(&self) -> Box<dyn Position>;
83
84    /// Non-mapped visual defaults.
85    fn default_params(&self) -> GeomParams;
86
87    /// Name for debug/display.
88    fn name(&self) -> &str;
89
90    /// Apply a brand/primary color to this geom's single-series default (its
91    /// `color` or `fill`). The build pipeline calls this only when the layer has
92    /// no color/fill aesthetic mapped, so an explicit mapping always wins. The
93    /// default is a no-op; series geoms override it.
94    fn set_series_color(&mut self, _color: (u8, u8, u8)) {}
95
96    /// Whether this geom draws from a 0 baseline, so the Y scale should include
97    /// 0 even when `y` is explicitly mapped (bars/columns/area/histograms — as
98    /// in ggplot2). Default false.
99    fn include_zero_baseline(&self) -> bool {
100        false
101    }
102}
103
104/// Format a value for a hover tooltip — strings verbatim, numbers rounded short,
105/// `Na` empty.
106pub(crate) fn tip_value(v: &crate::data::Value) -> String {
107    use crate::data::Value;
108    match v {
109        Value::Str(s) => s.clone(),
110        Value::Bool(b) => b.to_string(),
111        Value::Na => String::new(),
112        // A datetime axis reads as a calendar date, not raw epoch seconds.
113        Value::DateTime(secs) => crate::data::format_epoch_secs(*secs),
114        _ => v
115            .as_f64()
116            .map(|f| format!("{}", (f * 1000.0).round() / 1000.0))
117            .unwrap_or_default(),
118    }
119}