Skip to main content

ggplot_rs/scale/
continuous.rs

1use crate::aes::Aesthetic;
2use crate::data::Value;
3
4use super::format::LabelFormatter;
5use super::sec_axis::SecAxis;
6use super::transform::ScaleTransform;
7use super::util::{extended_breaks, format_number};
8use super::Scale;
9
10/// Continuous linear scale.
11#[derive(Clone)]
12pub struct ScaleContinuous {
13    aesthetic: Aesthetic,
14    name: String,
15    min: f64,
16    max: f64,
17    trained: bool,
18    filter_oob: bool,
19    // Per-side expansion: (mult_lower, add_lower, mult_upper, add_upper).
20    expand: (f64, f64, f64, f64),
21    // Axis position: false = default (bottom for x, left for y), true = opposite
22    // side (top for x, right for y).
23    position_opposite: bool,
24    pub(crate) scale_transform: ScaleTransform,
25    custom_breaks: Option<Vec<f64>>,
26    custom_labels: Option<Vec<String>>,
27    pub(crate) sec_axis: Option<SecAxis>,
28    label_formatter: Option<LabelFormatter>,
29}
30
31impl ScaleContinuous {
32    pub fn new() -> Self {
33        ScaleContinuous {
34            aesthetic: Aesthetic::X,
35            name: String::new(),
36            min: f64::INFINITY,
37            max: f64::NEG_INFINITY,
38            trained: false,
39            filter_oob: false,
40            expand: (0.05, 0.0, 0.05, 0.0),
41            position_opposite: false,
42            scale_transform: ScaleTransform::Identity,
43            custom_breaks: None,
44            custom_labels: None,
45            sec_axis: None,
46            label_formatter: None,
47        }
48    }
49
50    pub fn for_aesthetic(mut self, aes: Aesthetic) -> Self {
51        self.aesthetic = aes;
52        self
53    }
54
55    pub fn with_name(mut self, name: &str) -> Self {
56        self.name = name.to_string();
57        self
58    }
59
60    pub fn with_limits(mut self, min: f64, max: f64) -> Self {
61        self.min = min;
62        self.max = max;
63        self.trained = true;
64        self.filter_oob = true;
65        self
66    }
67
68    pub fn with_transform(mut self, transform: ScaleTransform) -> Self {
69        self.scale_transform = transform;
70        self
71    }
72
73    /// Set custom break positions (data values where ticks appear).
74    pub fn with_breaks(mut self, breaks: Vec<f64>) -> Self {
75        self.custom_breaks = Some(breaks);
76        self
77    }
78
79    /// Set custom labels for breaks. Must match the number of breaks.
80    pub fn with_labels(mut self, labels: Vec<String>) -> Self {
81        self.custom_labels = Some(labels);
82        self
83    }
84
85    /// Set the expansion multiplier and additive constant.
86    /// Like R's `expand = c(mult, add)`, applied symmetrically. Default `(0.05, 0.0)`.
87    pub fn with_expand(mut self, mult: f64, add: f64) -> Self {
88        self.expand = (mult, add, mult, add);
89        self
90    }
91
92    /// Per-side expansion (R's `expansion(mult = c(l, u), add = c(l, u))`):
93    /// separate multiplicative/additive expansion for the lower and upper ends.
94    pub fn with_expand_sides(
95        mut self,
96        mult_lower: f64,
97        add_lower: f64,
98        mult_upper: f64,
99        add_upper: f64,
100    ) -> Self {
101        self.expand = (mult_lower, add_lower, mult_upper, add_upper);
102        self
103    }
104
105    /// Place this axis on the opposite side (x → top, y → right).
106    pub fn with_position_opposite(mut self) -> Self {
107        self.position_opposite = true;
108        self
109    }
110
111    /// Set a label formatter. Accepts a plain `fn` (e.g. `label_comma`) or a
112    /// configurable formatter such as `label_si()` / `label_number(...)`.
113    pub fn with_label_formatter<F>(mut self, f: F) -> Self
114    where
115        F: Fn(f64) -> String + Send + Sync + 'static,
116    {
117        self.label_formatter = Some(std::sync::Arc::new(f));
118        self
119    }
120
121    /// Add a secondary axis with a transformation function.
122    pub fn with_sec_axis(mut self, sec: SecAxis) -> Self {
123        self.sec_axis = Some(sec);
124        self
125    }
126
127    /// Get the secondary axis, if any.
128    pub fn sec_axis(&self) -> Option<&SecAxis> {
129        self.sec_axis.as_ref()
130    }
131
132    fn format_label(&self, v: f64) -> String {
133        if let Some(f) = &self.label_formatter {
134            f(v)
135        } else {
136            format_number(v)
137        }
138    }
139
140    fn expanded_range(&self) -> (f64, f64) {
141        let range = self.max - self.min;
142        let (ml, al, mu, au) = self.expand;
143        (self.min - range * ml - al, self.max + range * mu + au)
144    }
145
146    /// Whether this axis should be drawn on the opposite side (x → top, y → right).
147    pub fn is_opposite(&self) -> bool {
148        self.position_opposite
149    }
150}
151
152impl Default for ScaleContinuous {
153    fn default() -> Self {
154        Self::new()
155    }
156}
157
158impl Scale for ScaleContinuous {
159    fn aesthetic(&self) -> Aesthetic {
160        self.aesthetic.clone()
161    }
162
163    fn train(&mut self, values: &[Value]) {
164        for v in values {
165            if let Some(f) = v.as_f64() {
166                if f.is_finite() {
167                    if f < self.min {
168                        self.min = f;
169                    }
170                    if f > self.max {
171                        self.max = f;
172                    }
173                }
174            }
175        }
176        self.trained = true;
177    }
178
179    fn map(&self, value: &Value) -> f64 {
180        let f = match value.as_f64() {
181            Some(f) => f,
182            None => return 0.0,
183        };
184        let (emin, emax) = self.expanded_range();
185        let range = emax - emin;
186        if range.abs() < f64::EPSILON {
187            0.5
188        } else {
189            (f - emin) / range
190        }
191    }
192
193    fn breaks(&self) -> Vec<(f64, String)> {
194        if !self.trained || self.min > self.max {
195            return vec![];
196        }
197
198        // Use custom breaks if provided
199        if let Some(ref custom) = self.custom_breaks {
200            return custom
201                .iter()
202                .enumerate()
203                .map(|(i, &v)| {
204                    let pos = self.map(&Value::Float(v));
205                    let label = if let Some(ref labels) = self.custom_labels {
206                        labels
207                            .get(i)
208                            .cloned()
209                            .unwrap_or_else(|| self.format_label(v))
210                    } else {
211                        self.format_label(self.scale_transform.inverse(v))
212                    };
213                    (pos, label)
214                })
215                .collect();
216        }
217
218        let range = self.max - self.min;
219        if range.abs() < f64::EPSILON {
220            let label = self.format_label(self.scale_transform.inverse(self.min));
221            return vec![(0.5, label)];
222        }
223
224        // Extended-Wilkinson breaks over the data range (matching ggplot2's
225        // scales::extended_breaks), keeping those within the expanded (visible)
226        // range. Labels show the original (inverse-transformed) value.
227        let (emin, emax) = self.expanded_range();
228        let tol = (emax - emin).abs() * 1e-9;
229        extended_breaks(self.min, self.max, 5)
230            .into_iter()
231            .filter(|&v| v >= emin - tol && v <= emax + tol)
232            .map(|v| {
233                let pos = self.map(&Value::Float(v));
234                let label = self.format_label(self.scale_transform.inverse(v));
235                (pos, label)
236            })
237            .collect()
238    }
239
240    fn name(&self) -> &str {
241        &self.name
242    }
243
244    fn set_name(&mut self, name: &str) {
245        self.name = name.to_string();
246    }
247
248    fn transform(&self, value: &Value) -> Value {
249        self.scale_transform.transform_value(value)
250    }
251
252    fn sec_axis(&self) -> Option<&SecAxis> {
253        self.sec_axis.as_ref()
254    }
255
256    fn set_limits(&mut self, min: f64, max: f64) {
257        self.min = min;
258        self.max = max;
259        self.trained = true;
260    }
261
262    fn filter_limits(&self) -> Option<(f64, f64)> {
263        if self.filter_oob && self.trained {
264            Some((self.min, self.max))
265        } else {
266            None
267        }
268    }
269
270    fn domain(&self) -> Option<(f64, f64)> {
271        if self.trained {
272            Some((self.min, self.max))
273        } else {
274            None
275        }
276    }
277
278    fn axis_position_opposite(&self) -> bool {
279        self.position_opposite
280    }
281
282    fn clone_box(&self) -> Box<dyn Scale> {
283        Box::new(self.clone())
284    }
285
286    fn reset_training(&mut self) {
287        self.min = f64::INFINITY;
288        self.max = f64::NEG_INFINITY;
289        self.trained = false;
290    }
291}