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