ggplot_rs/coord/
cartesian.rs1use crate::render::Rect;
2
3use super::Coord;
4
5pub struct CoordCartesian {
11 xlim: Option<(f64, f64)>,
12 ylim: Option<(f64, f64)>,
13 reverse_x: bool,
14 reverse_y: bool,
15 expand: [bool; 4],
18}
19
20impl CoordCartesian {
21 pub fn new() -> Self {
22 CoordCartesian {
23 xlim: None,
24 ylim: None,
25 reverse_x: false,
26 reverse_y: false,
27 expand: [true; 4],
28 }
29 }
30
31 pub fn expand(mut self, top: bool, right: bool, bottom: bool, left: bool) -> Self {
37 self.expand = [top, right, bottom, left];
38 self
39 }
40
41 pub fn reverse(mut self, x: bool, y: bool) -> Self {
45 self.reverse_x = x;
46 self.reverse_y = y;
47 self
48 }
49
50 pub fn xlim(mut self, min: f64, max: f64) -> Self {
52 self.xlim = Some((min, max));
53 self
54 }
55
56 pub fn ylim(mut self, min: f64, max: f64) -> Self {
58 self.ylim = Some((min, max));
59 self
60 }
61
62 pub fn get_xlim(&self) -> Option<(f64, f64)> {
64 self.xlim
65 }
66
67 pub fn get_ylim(&self) -> Option<(f64, f64)> {
69 self.ylim
70 }
71}
72
73impl Default for CoordCartesian {
74 fn default() -> Self {
75 Self::new()
76 }
77}
78
79impl Coord for CoordCartesian {
80 fn transform(&self, point: (f64, f64), plot_area: &Rect) -> (f64, f64) {
81 let (nx, ny) = point;
82 let nx = if self.reverse_x { 1.0 - nx } else { nx };
83 let ny = if self.reverse_y { 1.0 - ny } else { ny };
84 let px = plot_area.x + nx * plot_area.width;
85 let py = plot_area.y + (1.0 - ny) * plot_area.height;
87 (px, py)
88 }
89
90 fn zoom_x(&self) -> Option<(f64, f64)> {
91 self.xlim
92 }
93
94 fn zoom_y(&self) -> Option<(f64, f64)> {
95 self.ylim
96 }
97
98 fn train_scales(&mut self, scales: &mut crate::scale::ScaleSet) {
99 let [top, right, bottom, left] = self.expand.map(|e| !e);
100 let x = if self.reverse_x {
102 (right, left)
103 } else {
104 (left, right)
105 };
106 let y = if self.reverse_y {
107 (top, bottom)
108 } else {
109 (bottom, top)
110 };
111 for (aes, (lo, hi)) in [(crate::aes::Aesthetic::X, x), (crate::aes::Aesthetic::Y, y)] {
112 if lo || hi {
113 if let Some(s) = scales.get_mut(&aes) {
114 s.clear_expand(lo, hi);
115 }
116 }
117 }
118 }
119}