1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// A secondary axis specification — defines a transformation from the primary axis domain.
///
/// # Example
/// ```
/// use ggplot_rs::prelude::*;
///
/// // Temperature in Celsius on primary, Fahrenheit on secondary
/// let sec = SecAxis::new(|c| c * 9.0 / 5.0 + 32.0).with_name("Fahrenheit");
/// ```
#[derive(Clone)]
pub struct SecAxis {
/// Transformation from primary axis values to secondary axis values.
pub transform: fn(f64) -> f64,
/// Name / title for the secondary axis.
pub name: String,
/// Custom break positions in primary axis coordinates.
pub breaks: Option<Vec<f64>>,
}
impl SecAxis {
/// Create a secondary axis with the given transformation function.
pub fn new(transform: fn(f64) -> f64) -> Self {
SecAxis {
transform,
name: String::new(),
breaks: None,
}
}
pub fn with_name(mut self, name: &str) -> Self {
self.name = name.to_string();
self
}
pub fn with_breaks(mut self, breaks: Vec<f64>) -> Self {
self.breaks = Some(breaks);
self
}
/// Transform a primary axis value to the secondary axis value.
pub fn transform_value(&self, v: f64) -> f64 {
(self.transform)(v)
}
}
impl std::fmt::Debug for SecAxis {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SecAxis")
.field("name", &self.name)
.field("breaks", &self.breaks)
.finish()
}
}