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 (left, top) = coord.transform((nxmin, nymax), &plot_area);
68            let (right, bottom) = coord.transform((nxmax, nymin), &plot_area);
69
70            let fill_color = fill_col
71                .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[i]))
72                .unwrap_or(self.fill);
73
74            backend.draw_rect(
75                (left, top.min(bottom)),
76                (right, top.max(bottom)),
77                &RectStyle {
78                    fill: Some(fill_color),
79                    stroke: Some(self.color),
80                    stroke_width: self.line_width,
81                    alpha: self.alpha,
82                    clip: true,
83                },
84            )?;
85        }
86
87        Ok(())
88    }
89
90    fn required_aes(&self) -> Vec<Aesthetic> {
91        vec![
92            Aesthetic::Xmin,
93            Aesthetic::Xmax,
94            Aesthetic::Ymin,
95            Aesthetic::Ymax,
96        ]
97    }
98
99    fn default_stat(&self) -> Box<dyn Stat> {
100        Box::new(StatIdentity)
101    }
102
103    fn default_position(&self) -> Box<dyn Position> {
104        Box::new(PositionIdentity)
105    }
106
107    fn default_params(&self) -> GeomParams {
108        GeomParams::default()
109    }
110
111    fn name(&self) -> &str {
112        "rect"
113    }
114
115    fn set_series_color(&mut self, color: (u8, u8, u8)) {
116        self.fill = color;
117    }
118}