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