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
15pub struct GeomRect {
17 pub fill: (u8, u8, u8),
18 pub color: (u8, u8, u8),
19 pub alpha: f64,
20 pub line_width: f64,
21 pub radius: f64,
26}
27
28impl Default for GeomRect {
29 fn default() -> Self {
30 GeomRect {
31 fill: (97, 156, 255),
32 color: (50, 50, 50),
33 alpha: 0.5,
34 line_width: 0.5,
35 radius: 0.0,
36 }
37 }
38}
39
40impl GeomRect {
41 pub fn with_radius(mut self, radius_px: f64) -> Self {
43 self.radius = radius_px;
44 self
45 }
46}
47
48impl Geom for GeomRect {
49 fn draw(
50 &self,
51 data: &DataFrame,
52 coord: &dyn Coord,
53 scales: &ScaleSet,
54 _theme: &Theme,
55 backend: &mut dyn DrawBackend,
56 ) -> Result<(), RenderError> {
57 let xmin_col = data
58 .column("xmin")
59 .ok_or(RenderError::MissingAesthetic("xmin".into()))?;
60 let xmax_col = data
61 .column("xmax")
62 .ok_or(RenderError::MissingAesthetic("xmax".into()))?;
63 let ymin_col = data
64 .column("ymin")
65 .ok_or(RenderError::MissingAesthetic("ymin".into()))?;
66 let ymax_col = data
67 .column("ymax")
68 .ok_or(RenderError::MissingAesthetic("ymax".into()))?;
69 let fill_col = data.column("fill");
70
71 let plot_area = backend.plot_area();
72 let x_scale = scales.get(&Aesthetic::X);
73 let y_scale = scales.get(&Aesthetic::Y);
74
75 for i in 0..data.nrows() {
76 let nxmin = x_scale.map(|s| s.map(&xmin_col[i])).unwrap_or(0.0);
77 let nxmax = x_scale.map(|s| s.map(&xmax_col[i])).unwrap_or(0.0);
78 let nymin = y_scale.map(|s| s.map(&ymin_col[i])).unwrap_or(0.0);
79 let nymax = y_scale.map(|s| s.map(&ymax_col[i])).unwrap_or(0.0);
80
81 let fill_color = fill_col
82 .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[i]))
83 .unwrap_or(self.fill);
84 let pattern = fill_col
87 .filter(|fc| matches!(fc[i], crate::data::Value::Na))
88 .and_then(|_| scales.na_fill_pattern(&Aesthetic::Fill));
89 let style = RectStyle {
90 fill: Some(fill_color),
91 stroke: (self.line_width > 0.0).then_some(self.color),
92 stroke_width: self.line_width,
93 alpha: self.alpha,
94 clip: !coord.is_polar(),
95 corner_radius: if coord.is_polar() { 0.0 } else { self.radius },
98 };
99 super::set_row_extras(backend, data, i);
100
101 if coord.is_polar() {
102 let pts = super::col::polar_sector(coord, &plot_area, nxmin, nxmax, nymin, nymax);
104 match &pattern {
105 Some(p) => backend.draw_polygon_pattern(&pts, p, &style)?,
106 None => backend.draw_polygon(&pts, &style)?,
107 }
108 continue;
109 }
110
111 let (left, top) = coord.transform((nxmin, nymax), &plot_area);
112 let (right, bottom) = coord.transform((nxmax, nymin), &plot_area);
113 match &pattern {
114 Some(p) => backend.draw_rect_pattern(
115 (left, top.min(bottom)),
116 (right, top.max(bottom)),
117 p,
118 &style,
119 )?,
120 None => {
121 backend.draw_rect((left, top.min(bottom)), (right, top.max(bottom)), &style)?
122 }
123 }
124 }
125 super::clear_mark(backend);
126 super::clear_row_extras(backend);
127
128 Ok(())
129 }
130
131 fn required_aes(&self) -> Vec<Aesthetic> {
132 vec![
133 Aesthetic::Xmin,
134 Aesthetic::Xmax,
135 Aesthetic::Ymin,
136 Aesthetic::Ymax,
137 ]
138 }
139
140 fn default_stat(&self) -> Box<dyn Stat> {
141 Box::new(StatIdentity)
142 }
143
144 fn default_position(&self) -> Box<dyn Position> {
145 Box::new(PositionIdentity)
146 }
147
148 fn default_params(&self) -> GeomParams {
149 GeomParams::default()
150 }
151
152 fn name(&self) -> &str {
153 "rect"
154 }
155
156 fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
157 if let (true, Some(ink)) = (self.color == (50, 50, 50), el.ink) {
158 self.color = ink;
159 }
160 }
161
162 fn consumes_tooltips(&self) -> bool {
163 true
164 }
165
166 fn set_series_color(&mut self, color: (u8, u8, u8)) {
167 self.fill = color;
168 }
169
170 fn allows_infinite(&self) -> bool {
173 true
174 }
175}