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 style = RectStyle {
85 fill: Some(fill_color),
86 stroke: (self.line_width > 0.0).then_some(self.color),
87 stroke_width: self.line_width,
88 alpha: self.alpha,
89 clip: !coord.is_polar(),
90 corner_radius: if coord.is_polar() { 0.0 } else { self.radius },
93 };
94 super::set_row_extras(backend, data, i);
95
96 if coord.is_polar() {
97 let pts = super::col::polar_sector(coord, &plot_area, nxmin, nxmax, nymin, nymax);
99 backend.draw_polygon(&pts, &style)?;
100 continue;
101 }
102
103 let (left, top) = coord.transform((nxmin, nymax), &plot_area);
104 let (right, bottom) = coord.transform((nxmax, nymin), &plot_area);
105 backend.draw_rect((left, top.min(bottom)), (right, top.max(bottom)), &style)?;
106 }
107 super::clear_mark(backend);
108 super::clear_row_extras(backend);
109
110 Ok(())
111 }
112
113 fn required_aes(&self) -> Vec<Aesthetic> {
114 vec![
115 Aesthetic::Xmin,
116 Aesthetic::Xmax,
117 Aesthetic::Ymin,
118 Aesthetic::Ymax,
119 ]
120 }
121
122 fn default_stat(&self) -> Box<dyn Stat> {
123 Box::new(StatIdentity)
124 }
125
126 fn default_position(&self) -> Box<dyn Position> {
127 Box::new(PositionIdentity)
128 }
129
130 fn default_params(&self) -> GeomParams {
131 GeomParams::default()
132 }
133
134 fn name(&self) -> &str {
135 "rect"
136 }
137
138 fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
139 if let (true, Some(ink)) = (self.color == (50, 50, 50), el.ink) {
140 self.color = ink;
141 }
142 }
143
144 fn consumes_tooltips(&self) -> bool {
145 true
146 }
147
148 fn set_series_color(&mut self, color: (u8, u8, u8)) {
149 self.fill = color;
150 }
151
152 fn allows_infinite(&self) -> bool {
155 true
156 }
157}