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
78            if coord.is_polar() {
79                // Under polar coords a rect is an annulus sector (gauge bands).
80                let pts = super::col::polar_sector(coord, &plot_area, nxmin, nxmax, nymin, nymax);
81                backend.draw_polygon(&pts, &style)?;
82                continue;
83            }
84
85            let (left, top) = coord.transform((nxmin, nymax), &plot_area);
86            let (right, bottom) = coord.transform((nxmax, nymin), &plot_area);
87            backend.draw_rect((left, top.min(bottom)), (right, top.max(bottom)), &style)?;
88        }
89
90        Ok(())
91    }
92
93    fn required_aes(&self) -> Vec<Aesthetic> {
94        vec![
95            Aesthetic::Xmin,
96            Aesthetic::Xmax,
97            Aesthetic::Ymin,
98            Aesthetic::Ymax,
99        ]
100    }
101
102    fn default_stat(&self) -> Box<dyn Stat> {
103        Box::new(StatIdentity)
104    }
105
106    fn default_position(&self) -> Box<dyn Position> {
107        Box::new(PositionIdentity)
108    }
109
110    fn default_params(&self) -> GeomParams {
111        GeomParams::default()
112    }
113
114    fn name(&self) -> &str {
115        "rect"
116    }
117
118    fn set_series_color(&mut self, color: (u8, u8, u8)) {
119        self.fill = color;
120    }
121
122    /// `-Inf`/`Inf` bounds extend the rect to the panel edge (ggplot2), e.g.
123    /// a full-height shaded x-region: `ymin = -Inf, ymax = Inf`.
124    fn allows_infinite(&self) -> bool {
125        true
126    }
127}