Skip to main content

ggplot_rs/scale/
scale_set.rs

1use crate::aes::Aesthetic;
2use crate::data::{DataFrame, Value};
3use crate::render::backend::{Linetype, PointShape};
4use crate::scale::alpha::ScaleAlphaContinuous;
5use crate::scale::color::{ScaleColorContinuous, ScaleColorDiscrete};
6use crate::scale::continuous::ScaleContinuous;
7use crate::scale::datetime::ScaleDateTime;
8use crate::scale::discrete::ScaleDiscrete;
9use crate::scale::linetype::ScaleLinetypeDiscrete;
10use crate::scale::linewidth::ScaleLinewidthContinuous;
11use crate::scale::shape::ScaleShapeDiscrete;
12use crate::scale::size::ScaleSizeContinuous;
13
14use super::Scale;
15
16/// Registry of all scales for a plot. Handles auto-detection and training.
17pub struct ScaleSet {
18    scales: Vec<Box<dyn Scale>>,
19}
20
21impl ScaleSet {
22    pub fn new() -> Self {
23        ScaleSet { scales: Vec::new() }
24    }
25
26    /// Add a user-specified scale.
27    pub fn add(&mut self, scale: Box<dyn Scale>) {
28        // Replace existing scale for same aesthetic
29        let aes = scale.aesthetic();
30        self.scales.retain(|s| s.aesthetic() != aes);
31        self.scales.push(scale);
32    }
33
34    /// Give every scale the plot-level default locale (scales with their own
35    /// locale keep it; scales without numeric/date labels ignore it).
36    pub fn set_default_locale(&mut self, locale: &crate::locale::Locale) {
37        for s in &mut self.scales {
38            s.set_default_locale(locale);
39        }
40    }
41
42    /// Get a scale for a specific aesthetic.
43    pub fn get(&self, aes: &Aesthetic) -> Option<&dyn Scale> {
44        self.scales
45            .iter()
46            .find(|s| s.aesthetic() == *aes)
47            .map(|s| s.as_ref())
48    }
49
50    /// Get mutable scale for a specific aesthetic.
51    pub fn get_mut(&mut self, aes: &Aesthetic) -> Option<&mut Box<dyn Scale>> {
52        self.scales.iter_mut().find(|s| s.aesthetic() == *aes)
53    }
54
55    /// Ensure a scale exists for a given aesthetic. Auto-detect type from data.
56    pub fn ensure_scale(&mut self, aes: &Aesthetic, data: &DataFrame) {
57        if self.get(aes).is_some() {
58            return;
59        }
60        // OHLC values live on the y scale (via the geom's ymin/ymax extents);
61        // they get no scale of their own.
62        if matches!(
63            aes,
64            Aesthetic::Open
65                | Aesthetic::High
66                | Aesthetic::Low
67                | Aesthetic::Close
68                | Aesthetic::Slope
69                | Aesthetic::Intercept
70                // Reference-line intercepts live on the x / y scale; the
71                // build creates that scale from them only if no layer did
72                // (so a `geom_vline(0)` added first can't force a
73                // continuous scale onto a discrete axis).
74                | Aesthetic::Xintercept
75                | Aesthetic::Yintercept
76                // Interactive aesthetics carry their value straight through
77                // to the mark's attributes — no visual mapping, no legend.
78                | Aesthetic::Tooltip
79                | Aesthetic::DataAttr(_)
80                | Aesthetic::Class
81                | Aesthetic::GroupWrap(_)
82                // Per-row fontface (#228) is parsed straight into
83                // `FontFace` — no visual mapping, no legend.
84                | Aesthetic::Fontface
85                // Per-row text justification: numbers used as they are.
86                | Aesthetic::Hjust
87                | Aesthetic::Vjust
88                // Boxplot stat="identity" hinges/outliers (B3-5) live on
89                // the y scale (via `column_aesthetic`/`Y_FAMILY`/the
90                // build's stat_aes training list), not a scale of their
91                // own.
92                | Aesthetic::Lower
93                | Aesthetic::Middle
94                | Aesthetic::Upper
95                | Aesthetic::Outliers
96                // Edge-list node names (flow charts) — no scale.
97                | Aesthetic::From
98                | Aesthetic::To
99                | Aesthetic::Subgroup
100                | Aesthetic::Sets
101        ) {
102            return;
103        }
104        // A position scale created from extent columns alone (`xmin`/`xmax`
105        // with no `x`, e.g. a Gantt bar) detects its type from them, so a
106        // date range still gets a date scale.
107        let fallback: &[&str] = match aes {
108            Aesthetic::X => &["xmin", "xmax", "xend"],
109            Aesthetic::Y => &["ymin", "ymax", "yend"],
110            _ => &[],
111        };
112        let values = data
113            .column(aes.col_name())
114            .or_else(|| fallback.iter().find_map(|c| data.column(c)));
115
116        let is_discrete = match values {
117            Some(vals) => vals
118                .iter()
119                .any(|v| matches!(v, Value::Str(_) | Value::Bool(_))),
120            None => false,
121        };
122
123        let is_datetime = match values {
124            Some(vals) => vals.iter().any(|v| v.is_datetime()),
125            None => false,
126        };
127
128        match aes {
129            Aesthetic::Color | Aesthetic::Fill => {
130                if is_discrete {
131                    let scale = ScaleColorDiscrete::new(aes.clone());
132                    self.scales.push(Box::new(scale));
133                } else {
134                    let scale = ScaleColorContinuous::new(aes.clone());
135                    self.scales.push(Box::new(scale));
136                }
137            }
138            Aesthetic::Shape => {
139                let scale = ScaleShapeDiscrete::new();
140                self.scales.push(Box::new(scale));
141            }
142            Aesthetic::Linetype => {
143                let scale = ScaleLinetypeDiscrete::new();
144                self.scales.push(Box::new(scale));
145            }
146            Aesthetic::Size => {
147                let scale = ScaleSizeContinuous::new();
148                self.scales.push(Box::new(scale));
149            }
150            Aesthetic::Linewidth => {
151                let scale = ScaleLinewidthContinuous::new();
152                self.scales.push(Box::new(scale));
153            }
154            Aesthetic::Alpha => {
155                let scale = ScaleAlphaContinuous::new();
156                self.scales.push(Box::new(scale));
157            }
158            _ => {
159                if is_discrete {
160                    let scale = ScaleDiscrete::new().for_aesthetic(aes.clone());
161                    self.scales.push(Box::new(scale));
162                } else if is_datetime {
163                    let scale = ScaleDateTime::new().for_aesthetic(aes.clone());
164                    self.scales.push(Box::new(scale));
165                } else {
166                    let scale = ScaleContinuous::new().for_aesthetic(aes.clone());
167                    self.scales.push(Box::new(scale));
168                }
169            }
170        }
171    }
172
173    /// Train all scales on data from one layer.
174    pub fn train_layer(&mut self, data: &DataFrame) {
175        for scale in &mut self.scales {
176            let col_name = scale.aesthetic().col_name().to_string();
177            if let Some(values) = data.column(&col_name) {
178                scale.train(values);
179            }
180        }
181    }
182
183    /// Map a single value through the appropriate scale.
184    pub fn map_value(&self, aes: &Aesthetic, value: &Value) -> f64 {
185        self.get(aes).map(|s| s.map(value)).unwrap_or(0.0)
186    }
187
188    /// Map a value to an RGB color through the appropriate color/fill scale.
189    pub fn map_color(&self, aes: &Aesthetic, value: &Value) -> Option<(u8, u8, u8)> {
190        self.get(aes).and_then(|s| s.map_to_color(value))
191    }
192
193    /// The `aes` scale's configured na-pattern, if any (`ScaleFillBinned::
194    /// na_pattern`, #206) — a geom checks this for a row/key it would
195    /// otherwise draw with the scale's flat na colour, before falling
196    /// back to [`map_color`](Self::map_color)'s plain result.
197    pub fn na_fill_pattern(&self, aes: &Aesthetic) -> Option<crate::render::backend::FillPattern> {
198        self.get(aes).and_then(|s| s.na_fill_pattern())
199    }
200
201    /// Map a value to a point shape through the shape scale.
202    pub fn map_shape(&self, value: &Value) -> Option<PointShape> {
203        self.get(&Aesthetic::Shape)
204            .and_then(|s| s.map_to_shape(value))
205    }
206
207    /// Map a value to a linetype through the linetype scale.
208    pub fn map_linetype(&self, value: &Value) -> Option<Linetype> {
209        self.get(&Aesthetic::Linetype)
210            .and_then(|s| s.map_to_linetype(value))
211    }
212
213    /// Map a value to a point size through the size scale.
214    pub fn map_size(&self, value: &Value) -> Option<f64> {
215        self.get(&Aesthetic::Size)
216            .and_then(|s| s.map_to_size(value))
217    }
218
219    /// Map a value to an alpha (opacity) through the alpha scale.
220    pub fn map_alpha(&self, value: &Value) -> Option<f64> {
221        self.get(&Aesthetic::Alpha)
222            .and_then(|s| s.map_to_alpha(value))
223    }
224
225    /// Map a value to a stroke width through the linewidth scale.
226    pub fn map_linewidth(&self, value: &Value) -> Option<f64> {
227        self.get(&Aesthetic::Linewidth)
228            .and_then(|s| s.map_to_size(value))
229    }
230
231    /// Override the domain limits for a scale (used by coord_cartesian zoom).
232    pub fn set_limits(&mut self, aes: &Aesthetic, min: f64, max: f64) {
233        if let Some(scale) = self.get_mut(aes) {
234            scale.set_limits(min, max);
235        }
236    }
237
238    /// Get the secondary axis for an aesthetic, if one exists.
239    pub fn sec_axis(&self, aes: &Aesthetic) -> Option<&crate::scale::sec_axis::SecAxis> {
240        self.get(aes).and_then(|s| s.sec_axis())
241    }
242
243    /// Get all scales.
244    pub fn iter(&self) -> impl Iterator<Item = &dyn Scale> {
245        self.scales.iter().map(|s| s.as_ref())
246    }
247}
248
249impl Clone for ScaleSet {
250    fn clone(&self) -> Self {
251        ScaleSet {
252            scales: self.scales.iter().map(|s| s.clone_box()).collect(),
253        }
254    }
255}
256
257impl Default for ScaleSet {
258    fn default() -> Self {
259        Self::new()
260    }
261}