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 for i in 0..data.nrows() {
111 let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.0);
112 let ny = y_scale.map(|s| s.map(&y_col[i])).unwrap_or(0.0);
113 let ny_base = ymin_col
116 .and_then(|c| c[i].as_f64())
117 .and_then(|v| y_scale.map(|s| s.map(&crate::data::Value::Float(v))))
118 .unwrap_or_else(|| {
119 y_scale
120 .map(|s| s.map(&crate::data::Value::Float(0.0)))
121 .unwrap_or(0.0)
122 });
123
124 let (fr, fg, fb) = if let Some(fc) = fill_col {
125 scales
126 .map_color(&Aesthetic::Fill, &fc[i])
127 .unwrap_or(self.fill)
128 } else {
129 self.fill
130 };
131 let style = RectStyle {
132 fill: Some((fr, fg, fb)),
133 stroke: Some(self.color),
134 stroke_width: 0.5,
135 alpha: self.alpha,
136 clip: !coord.is_polar(),
137 };
138
139 let label = label_col.and_then(|c| match &c[i] {
140 crate::data::Value::Str(s) if !s.is_empty() => Some(s.clone()),
141 v => v
142 .as_f64()
143 .map(|f| format!("{}", (f * 1000.0).round() / 1000.0)),
144 });
145 let raw = super::measured_value(data, i);
148 let yval = raw
149 .as_deref()
150 .and_then(|r| r.parse::<f64>().ok())
151 .map(|f| format!("{}", (f * 1000.0).round() / 1000.0));
152 let tip = match (label, yval) {
153 (Some(l), Some(y)) => Some(format!("{l}: {y}")),
154 (Some(l), None) => Some(l),
155 (None, Some(y)) => Some(y),
156 (None, None) => None,
157 };
158 super::set_mark(
159 backend,
160 tip,
161 Some(super::tip_value(&x_col[i])),
162 super::series_key(data, i),
163 raw,
164 );
165
166 if coord.is_polar() {
167 let points = polar_sector(
171 coord,
172 &plot_area,
173 nx - half_width,
174 nx + half_width,
175 ny_base,
176 ny,
177 );
178 backend.draw_polygon(&points, &style)?;
179 } else {
180 let (left_px, top_px) = coord.transform((nx - half_width, ny), &plot_area);
181 let (right_px, bottom_px) = coord.transform((nx + half_width, ny_base), &plot_area);
182 backend.draw_rect(
183 (left_px, top_px.min(bottom_px)),
184 (right_px, top_px.max(bottom_px)),
185 &style,
186 )?;
187 }
188 }
189 super::clear_mark(backend);
190
191 Ok(())
192 }
193
194 fn required_aes(&self) -> Vec<Aesthetic> {
195 vec![Aesthetic::X, Aesthetic::Y]
196 }
197
198 fn default_stat(&self) -> Box<dyn Stat> {
199 Box::new(StatIdentity)
200 }
201
202 fn default_position(&self) -> Box<dyn Position> {
203 Box::new(PositionIdentity)
204 }
205
206 fn default_params(&self) -> GeomParams {
207 GeomParams::default()
208 }
209
210 fn name(&self) -> &str {
211 "col"
212 }
213
214 fn include_zero_baseline(&self) -> bool {
215 true
216 }
217
218 fn set_series_color(&mut self, color: (u8, u8, u8)) {
219 self.fill = color;
220 }
221}