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(crate) fn polar_sector(
19 coord: &dyn Coord,
20 area: &crate::render::Rect,
21 t0: f64,
22 t1: f64,
23 r0: f64,
24 r1: f64,
25) -> Vec<(f64, f64)> {
26 const N: usize = 48;
27 let mut pts = Vec::with_capacity(2 * (N + 1));
28 if coord.polar_theta_is_x() {
29 for k in 0..=N {
31 let t = t0 + (t1 - t0) * k as f64 / N as f64;
32 pts.push(coord.transform((t, r1), area));
33 }
34 for k in 0..=N {
35 let t = t1 + (t0 - t1) * k as f64 / N as f64;
36 pts.push(coord.transform((t, r0), area));
37 }
38 } else {
39 for k in 0..=N {
41 let r = r0 + (r1 - r0) * k as f64 / N as f64;
42 pts.push(coord.transform((t1, r), area));
43 }
44 for k in 0..=N {
45 let r = r1 + (r0 - r1) * k as f64 / N as f64;
46 pts.push(coord.transform((t0, r), area));
47 }
48 }
49 pts
50}
51
52pub struct GeomCol {
54 pub fill: (u8, u8, u8),
55 pub color: (u8, u8, u8),
56 pub alpha: f64,
57 pub width: f64,
58}
59
60impl Default for GeomCol {
61 fn default() -> Self {
62 GeomCol {
63 fill: (97, 156, 255),
64 color: (50, 50, 50),
65 alpha: 1.0,
66 width: 0.9,
67 }
68 }
69}
70
71impl Geom for GeomCol {
72 fn draw(
73 &self,
74 data: &DataFrame,
75 coord: &dyn Coord,
76 scales: &ScaleSet,
77 _theme: &Theme,
78 backend: &mut dyn DrawBackend,
79 ) -> Result<(), RenderError> {
80 let x_col = data
81 .column("x")
82 .ok_or(RenderError::MissingAesthetic("x".into()))?;
83 let y_col = data
84 .column("y")
85 .ok_or(RenderError::MissingAesthetic("y".into()))?;
86 let fill_col = data.column("fill");
87
88 let plot_area = backend.plot_area();
89 let x_scale = scales.get(&Aesthetic::X);
90 let y_scale = scales.get(&Aesthetic::Y);
91 let x_is_discrete = x_scale.map(|s| s.is_discrete()).unwrap_or(false);
92
93 let ymin_col = data.column("ymin");
94 let label_col = data.column("label");
96
97 let half_width = if x_is_discrete {
100 let n_breaks = x_scale.map(|s| s.breaks().len()).unwrap_or(1);
101 self.width / (n_breaks.max(1) as f64 * 1.1) / 2.0
102 } else {
103 super::continuous_bar_half_width(
104 x_col.iter().filter_map(|v| x_scale.map(|s| s.map(v))),
105 self.width,
106 0.02,
107 )
108 };
109
110 let x_mapper = super::support::XMapper::new(data, scales);
113 let dodge_n = data.column(crate::position::DODGE_N_COL);
114
115 for i in 0..data.nrows() {
116 let nx = x_mapper.map(&x_col[i], i);
117 let half_width = match dodge_n.and_then(|c| c[i].as_f64()) {
118 Some(n) if n > 1.0 && x_is_discrete => half_width / n,
119 _ => half_width,
120 };
121 let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
122 let ny_base = ymin_col
125 .and_then(|c| c[i].as_f64())
126 .and_then(|v| y_scale.map(|s| s.map(&crate::data::Value::Float(v))))
127 .unwrap_or_else(|| {
128 y_scale
129 .map(|s| s.map(&crate::data::Value::Float(0.0)))
130 .unwrap_or(0.0)
131 });
132
133 let (fr, fg, fb) = if let Some(fc) = fill_col {
134 scales
135 .map_color(&Aesthetic::Fill, &fc[i])
136 .unwrap_or(self.fill)
137 } else {
138 self.fill
139 };
140 let style = RectStyle {
141 fill: Some((fr, fg, fb)),
142 stroke: Some(self.color),
143 stroke_width: 0.5,
144 alpha: self.alpha,
145 clip: !coord.is_polar(),
146 };
147
148 let label = label_col.and_then(|c| match &c[i] {
149 crate::data::Value::Str(s) if !s.is_empty() => Some(s.clone()),
150 v => v
151 .as_f64()
152 .map(|f| format!("{}", (f * 1000.0).round() / 1000.0)),
153 });
154 let raw = super::measured_value(data, i);
157 let yval = raw
158 .as_deref()
159 .and_then(|r| r.parse::<f64>().ok())
160 .map(|f| format!("{}", (f * 1000.0).round() / 1000.0));
161 let tip = match (label, yval) {
162 (Some(l), Some(y)) => Some(format!("{l}: {y}")),
163 (Some(l), None) => Some(l),
164 (None, Some(y)) => Some(y),
165 (None, None) => None,
166 };
167 super::set_mark(
168 backend,
169 tip,
170 Some(super::tip_value(&x_col[i])),
171 super::series_key(data, i),
172 raw,
173 );
174
175 if coord.is_polar() {
176 let points = polar_sector(
180 coord,
181 &plot_area,
182 nx - half_width,
183 nx + half_width,
184 ny_base,
185 ny,
186 );
187 backend.draw_polygon(&points, &style)?;
188 } else {
189 let (left_px, top_px) = coord.transform((nx - half_width, ny), &plot_area);
190 let (right_px, bottom_px) = coord.transform((nx + half_width, ny_base), &plot_area);
191 backend.draw_rect(
192 (left_px, top_px.min(bottom_px)),
193 (right_px, top_px.max(bottom_px)),
194 &style,
195 )?;
196 }
197 }
198 super::clear_mark(backend);
199
200 Ok(())
201 }
202
203 fn required_aes(&self) -> Vec<Aesthetic> {
204 vec![Aesthetic::X, Aesthetic::Y]
205 }
206
207 fn default_stat(&self) -> Box<dyn Stat> {
208 Box::new(StatIdentity)
209 }
210
211 fn default_position(&self) -> Box<dyn Position> {
212 Box::new(PositionIdentity)
213 }
214
215 fn default_params(&self) -> GeomParams {
216 GeomParams::default()
217 }
218
219 fn name(&self) -> &str {
220 "col"
221 }
222
223 fn include_zero_baseline(&self) -> bool {
224 true
225 }
226
227 fn set_series_color(&mut self, color: (u8, u8, u8)) {
228 self.fill = color;
229 }
230}