Skip to main content

ggplot_rs/geom/
rect.rs

1use crate::aes::Aesthetic;
2use crate::coord::Coord;
3use crate::data::DataFrame;
4use crate::position::identity::PositionIdentity;
5use crate::position::Position;
6use crate::render::backend::{DrawBackend, RectStyle};
7use crate::render::RenderError;
8use crate::scale::ScaleSet;
9use crate::stat::identity::StatIdentity;
10use crate::stat::Stat;
11use crate::theme::Theme;
12
13use super::{Geom, GeomParams};
14
15/// Rect geometry — rectangles from (xmin, ymin) to (xmax, ymax).
16pub struct GeomRect {
17    pub fill: (u8, u8, u8),
18    pub color: (u8, u8, u8),
19    pub alpha: f64,
20    pub line_width: f64,
21}
22
23impl Default for GeomRect {
24    fn default() -> Self {
25        GeomRect {
26            fill: (97, 156, 255),
27            color: (50, 50, 50),
28            alpha: 0.5,
29            line_width: 0.5,
30        }
31    }
32}
33
34impl Geom for GeomRect {
35    fn draw(
36        &self,
37        data: &DataFrame,
38        coord: &dyn Coord,
39        scales: &ScaleSet,
40        _theme: &Theme,
41        backend: &mut dyn DrawBackend,
42    ) -> Result<(), RenderError> {
43        let xmin_col = data
44            .column("xmin")
45            .ok_or(RenderError::MissingAesthetic("xmin".into()))?;
46        let xmax_col = data
47            .column("xmax")
48            .ok_or(RenderError::MissingAesthetic("xmax".into()))?;
49        let ymin_col = data
50            .column("ymin")
51            .ok_or(RenderError::MissingAesthetic("ymin".into()))?;
52        let ymax_col = data
53            .column("ymax")
54            .ok_or(RenderError::MissingAesthetic("ymax".into()))?;
55        let fill_col = data.column("fill");
56
57        let plot_area = backend.plot_area();
58        let x_scale = scales.get(&Aesthetic::X);
59        let y_scale = scales.get(&Aesthetic::Y);
60
61        for i in 0..data.nrows() {
62            let nxmin = x_scale.map(|s| s.map(&xmin_col[i])).unwrap_or(0.0);
63            let nxmax = x_scale.map(|s| s.map(&xmax_col[i])).unwrap_or(0.0);
64            let nymin = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
65            let nymax = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
66
67            let fill_color = fill_col
68                .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[i]))
69                .unwrap_or(self.fill);
70            let style = RectStyle {
71                fill: Some(fill_color),
72                stroke: (self.line_width > 0.0).then_some(self.color),
73                stroke_width: self.line_width,
74                alpha: self.alpha,
75                clip: !coord.is_polar(),
76            };
77            super::set_row_extras(backend, data, i);
78
79            if coord.is_polar() {
80                // Under polar coords a rect is an annulus sector (gauge bands).
81                let pts = super::col::polar_sector(coord, &plot_area, nxmin, nxmax, nymin, nymax);
82                backend.draw_polygon(&pts, &style)?;
83                continue;
84            }
85
86            let (left, top) = coord.transform((nxmin, nymax), &plot_area);
87            let (right, bottom) = coord.transform((nxmax, nymin), &plot_area);
88            backend.draw_rect((left, top.min(bottom)), (right, top.max(bottom)), &style)?;
89        }
90        super::clear_mark(backend);
91        super::clear_row_extras(backend);
92
93        Ok(())
94    }
95
96    fn required_aes(&self) -> Vec<Aesthetic> {
97        vec![
98            Aesthetic::Xmin,
99            Aesthetic::Xmax,
100            Aesthetic::Ymin,
101            Aesthetic::Ymax,
102        ]
103    }
104
105    fn default_stat(&self) -> Box<dyn Stat> {
106        Box::new(StatIdentity)
107    }
108
109    fn default_position(&self) -> Box<dyn Position> {
110        Box::new(PositionIdentity)
111    }
112
113    fn default_params(&self) -> GeomParams {
114        GeomParams::default()
115    }
116
117    fn name(&self) -> &str {
118        "rect"
119    }
120
121    fn set_series_color(&mut self, color: (u8, u8, u8)) {
122        self.fill = color;
123    }
124
125    /// `-Inf`/`Inf` bounds extend the rect to the panel edge (ggplot2), e.g.
126    /// a full-height shaded x-region: `ymin = -Inf, ymax = Inf`.
127    fn allows_infinite(&self) -> bool {
128        true
129    }
130}