Skip to main content

ggplot_rs/scale/
mod.rs

1pub mod alpha;
2pub mod color;
3pub mod continuous;
4pub mod datetime;
5pub mod discrete;
6pub mod format;
7pub mod gradient;
8pub mod gradient_n;
9pub mod grey;
10pub mod linetype;
11pub mod linetype_manual;
12pub mod manual;
13pub mod palettes;
14pub mod scale_set;
15pub mod sec_axis;
16pub mod shape;
17pub mod shape_manual;
18pub mod size;
19pub mod steps;
20pub mod transform;
21pub mod util;
22
23pub use scale_set::ScaleSet;
24
25use crate::aes::Aesthetic;
26use crate::data::Value;
27use crate::render::backend::{Linetype, PointShape};
28
29/// Trait for scales that map data values to visual properties.
30pub trait Scale: Send + Sync {
31    /// Which aesthetic this scale is for.
32    fn aesthetic(&self) -> Aesthetic;
33
34    /// Incorporate data values to determine domain.
35    fn train(&mut self, values: &[Value]);
36
37    /// Map a data value to a [0, 1] normalized position (position scales)
38    /// or to a concrete visual value index (color/size scales).
39    fn map(&self, value: &Value) -> f64;
40
41    /// Generate break positions and labels for axis/legend.
42    fn breaks(&self) -> Vec<(f64, String)>;
43
44    /// Human-readable name (axis title).
45    fn name(&self) -> &str;
46
47    /// Set the scale name.
48    fn set_name(&mut self, name: &str);
49
50    /// Apply transformation to raw data (e.g., log10).
51    fn transform(&self, value: &Value) -> Value {
52        value.clone()
53    }
54
55    /// Whether this is a discrete scale.
56    fn is_discrete(&self) -> bool {
57        false
58    }
59
60    /// Map a data value to an RGB color. Default returns None.
61    fn map_to_color(&self, _value: &Value) -> Option<(u8, u8, u8)> {
62        None
63    }
64
65    /// Map a data value to a point shape. Default returns None.
66    fn map_to_shape(&self, _value: &Value) -> Option<PointShape> {
67        None
68    }
69
70    /// Map a data value to a linetype. Default returns None.
71    fn map_to_linetype(&self, _value: &Value) -> Option<Linetype> {
72        None
73    }
74
75    /// Map a data value to a point size (radius in pixels). Default returns None.
76    fn map_to_size(&self, _value: &Value) -> Option<f64> {
77        None
78    }
79
80    /// Map a data value to an alpha (opacity) value. Default returns None.
81    fn map_to_alpha(&self, _value: &Value) -> Option<f64> {
82        None
83    }
84
85    /// Get the secondary axis specification, if any.
86    fn sec_axis(&self) -> Option<&sec_axis::SecAxis> {
87        None
88    }
89
90    /// Override the trained domain limits (used by coord_cartesian zoom).
91    fn set_limits(&mut self, _min: f64, _max: f64) {
92        // Default no-op. Continuous scales override this.
93    }
94
95    /// Return OOB filter limits if this scale was created with explicit limits
96    /// (e.g., via xlim/ylim). Data outside these limits should be removed before stats.
97    fn filter_limits(&self) -> Option<(f64, f64)> {
98        None
99    }
100
101    /// Return the trained data domain (min, max) for continuous scales.
102    /// Used by the colorbar legend to pass data-space values to map_to_color().
103    fn domain(&self) -> Option<(f64, f64)> {
104        None
105    }
106
107    /// Clone this scale into a boxed trait object.
108    fn clone_box(&self) -> Box<dyn Scale>;
109
110    /// Reset training state so the scale can be retrained on new data.
111    fn reset_training(&mut self) {}
112}